Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions functions/dataconnect-resolver/fwdChatToEmail.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Stub Data Connect function. Normally, this would be generated by the Firebase CLI.
async function getChatMessage({ chatMessageId }: { chatMessageId: string }): Promise<{ data: { chatMessage?: { content?: string } } }> {
return {
data: {
chatMessage: {
content: ""
}
}
};
}

// [START fdc_forward_to_email_resolver]
import { getAuth } from "firebase-admin/auth";
import {
FirebaseContext,
GraphqlServerOptions,
onGraphRequest
} from "firebase-functions/dataconnect/graphql";

const opts: GraphqlServerOptions = {
schemaFilePath: "dataconnect/schema_resolver/schema.gql",
resolvers: {
query: {
async forwardToEmail(
_parent: unknown,
args: Record<string, unknown>,
_contextValue: FirebaseContext,
_info: unknown
) {
const chatMessageId = args.chatMessageId as string;

let decodedToken;
try {
decodedToken = await getAuth().verifyIdToken(_contextValue.auth.token ?? "");
} catch (error) {
return false;
}

const email = decodedToken.email;
if (!email) {
return false;
}

// Call generated admin SDK.
const response = await getChatMessage({chatMessageId});
const messageContent = response.data.chatMessage?.content;

// Here you call the cloud service of your choice to send the email with
// the message content.

return true;
}
},
},
};

export const resolver = onGraphRequest(opts);
// [END fdc_forward_to_email_resolver]
56 changes: 56 additions & 0 deletions functions/dataconnect-resolver/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// [START fdc_resolver_skeleton]
import {
FirebaseContext,
GraphqlServerOptions,
onGraphRequest,
} from "firebase-functions/dataconnect/graphql";

const opts: GraphqlServerOptions = {
// Points to the schema you defined earlier, relative to the root of your
// Firebase project.
schemaFilePath: "dataconnect/schema_resolver/schema.gql",
resolvers: {
query: {
// This resolver function populates the data for the "publicProfile" field
// defined in your GraphQL schema located at schemaFilePath.
publicProfile(
_parent: unknown,
args: Record<string, unknown>,
_contextValue: FirebaseContext,
_info: unknown
) {
const userId = args.userId;

// Here you would use the user ID to retrieve the user profile from your data
// store. In this example, we just return a hard-coded value.

return {
name: "Ulysses von Userberg",
photoUrl: "https://example.com/profiles/12345/photo.jpg",
bioLine: "Just a guy on a mountain. Ski fanatic.",
};
},
},
mutation: {
// This resolver function updates data for the "updatePublicProfile" field
// defined in your GraphQL schema located at schemaFilePath.
updatePublicProfile(
_parent: unknown,
args: Record<string, unknown>,
_contextValue: FirebaseContext,
_info: unknown
) {
const { userId, name, photoUrl, bioLine } = args;

// Here you would update in your datastore the user's profile using the
// arguments that were passed. In this example, we just return the profile
// as though the operation had been successful.

return { name, photoUrl, bioLine };
},
},
},
};

export const resolver = onGraphRequest(opts);
// [END fdc_resolver_skeleton]
14 changes: 14 additions & 0 deletions functions/dataconnect-resolver/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "dataconnect-resolver",
"version": "1.0.0",
"description": "Skeleton implementations of Data Connect custom resolvers",
"main": "index.js",
"license": "Apache-2.0",
"scripts": {
"compile": "cp ../../tsconfig.template.json ./tsconfig.json && tsc"
},
"dependencies": {
"firebase-admin": "^11.11.1",
"firebase-functions": "^7.1.0"
}
}
30 changes: 30 additions & 0 deletions functions/dataconnect-resolver/sendEmail.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// [START fdc_send_email_resolver]
import {
FirebaseContext,
GraphqlServerOptions,
onGraphRequest,
} from "firebase-functions/dataconnect/graphql";

const opts: GraphqlServerOptions = {
schemaFilePath: "dataconnect/schema_resolver/schema.gql",
resolvers: {
mutation: {
sendEmail(
_parent: unknown,
args: Record<string, unknown>,
_contextValue: FirebaseContext,
_info: unknown
) {
const { friendId, content } = args;

// Look up the friend's email address and call the cloud service of your
// choice to send the friend an email with the given content.

return true;
},
},
},
};

export const resolver = onGraphRequest(opts);
// [END fdc_send_email_resolver]
79 changes: 79 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading