Skip to content

Commit 1f543bd

Browse files
Add Data Connect resolver snippets (#422)
* Add Data Connect resolver snippets * Update lock file
1 parent 820af91 commit 1f543bd

File tree

5 files changed

+237
-0
lines changed

5 files changed

+237
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Stub Data Connect function. Normally, this would be generated by the Firebase CLI.
2+
async function getChatMessage({ chatMessageId }: { chatMessageId: string }): Promise<{ data: { chatMessage?: { content?: string } } }> {
3+
return {
4+
data: {
5+
chatMessage: {
6+
content: ""
7+
}
8+
}
9+
};
10+
}
11+
12+
// [START fdc_forward_to_email_resolver]
13+
import { getAuth } from "firebase-admin/auth";
14+
import {
15+
FirebaseContext,
16+
GraphqlServerOptions,
17+
onGraphRequest
18+
} from "firebase-functions/dataconnect/graphql";
19+
20+
const opts: GraphqlServerOptions = {
21+
schemaFilePath: "dataconnect/schema_resolver/schema.gql",
22+
resolvers: {
23+
query: {
24+
async forwardToEmail(
25+
_parent: unknown,
26+
args: Record<string, unknown>,
27+
_contextValue: FirebaseContext,
28+
_info: unknown
29+
) {
30+
const chatMessageId = args.chatMessageId as string;
31+
32+
let decodedToken;
33+
try {
34+
decodedToken = await getAuth().verifyIdToken(_contextValue.auth.token ?? "");
35+
} catch (error) {
36+
return false;
37+
}
38+
39+
const email = decodedToken.email;
40+
if (!email) {
41+
return false;
42+
}
43+
44+
// Call generated admin SDK.
45+
const response = await getChatMessage({chatMessageId});
46+
const messageContent = response.data.chatMessage?.content;
47+
48+
// Here you call the cloud service of your choice to send the email with
49+
// the message content.
50+
51+
return true;
52+
}
53+
},
54+
},
55+
};
56+
57+
export const resolver = onGraphRequest(opts);
58+
// [END fdc_forward_to_email_resolver]
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// [START fdc_resolver_skeleton]
2+
import {
3+
FirebaseContext,
4+
GraphqlServerOptions,
5+
onGraphRequest,
6+
} from "firebase-functions/dataconnect/graphql";
7+
8+
const opts: GraphqlServerOptions = {
9+
// Points to the schema you defined earlier, relative to the root of your
10+
// Firebase project.
11+
schemaFilePath: "dataconnect/schema_resolver/schema.gql",
12+
resolvers: {
13+
query: {
14+
// This resolver function populates the data for the "publicProfile" field
15+
// defined in your GraphQL schema located at schemaFilePath.
16+
publicProfile(
17+
_parent: unknown,
18+
args: Record<string, unknown>,
19+
_contextValue: FirebaseContext,
20+
_info: unknown
21+
) {
22+
const userId = args.userId;
23+
24+
// Here you would use the user ID to retrieve the user profile from your data
25+
// store. In this example, we just return a hard-coded value.
26+
27+
return {
28+
name: "Ulysses von Userberg",
29+
photoUrl: "https://example.com/profiles/12345/photo.jpg",
30+
bioLine: "Just a guy on a mountain. Ski fanatic.",
31+
};
32+
},
33+
},
34+
mutation: {
35+
// This resolver function updates data for the "updatePublicProfile" field
36+
// defined in your GraphQL schema located at schemaFilePath.
37+
updatePublicProfile(
38+
_parent: unknown,
39+
args: Record<string, unknown>,
40+
_contextValue: FirebaseContext,
41+
_info: unknown
42+
) {
43+
const { userId, name, photoUrl, bioLine } = args;
44+
45+
// Here you would update in your datastore the user's profile using the
46+
// arguments that were passed. In this example, we just return the profile
47+
// as though the operation had been successful.
48+
49+
return { name, photoUrl, bioLine };
50+
},
51+
},
52+
},
53+
};
54+
55+
export const resolver = onGraphRequest(opts);
56+
// [END fdc_resolver_skeleton]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "dataconnect-resolver",
3+
"version": "1.0.0",
4+
"description": "Skeleton implementations of Data Connect custom resolvers",
5+
"main": "index.js",
6+
"license": "Apache-2.0",
7+
"scripts": {
8+
"compile": "cp ../../tsconfig.template.json ./tsconfig.json && tsc"
9+
},
10+
"dependencies": {
11+
"firebase-admin": "^11.11.1",
12+
"firebase-functions": "^7.1.0"
13+
}
14+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// [START fdc_send_email_resolver]
2+
import {
3+
FirebaseContext,
4+
GraphqlServerOptions,
5+
onGraphRequest,
6+
} from "firebase-functions/dataconnect/graphql";
7+
8+
const opts: GraphqlServerOptions = {
9+
schemaFilePath: "dataconnect/schema_resolver/schema.gql",
10+
resolvers: {
11+
mutation: {
12+
sendEmail(
13+
_parent: unknown,
14+
args: Record<string, unknown>,
15+
_contextValue: FirebaseContext,
16+
_info: unknown
17+
) {
18+
const { friendId, content } = args;
19+
20+
// Look up the friend's email address and call the cloud service of your
21+
// choice to send the friend an email with the given content.
22+
23+
return true;
24+
},
25+
},
26+
},
27+
};
28+
29+
export const resolver = onGraphRequest(opts);
30+
// [END fdc_send_email_resolver]

pnpm-lock.yaml

Lines changed: 79 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)