Skip to content

Commit 9ae96c0

Browse files
docs(sample): add guide for integrating with azure functions (#1637)
* docs(examples): add link to article integrating azure functions * docs(sample): corrected location of doc to be added * doc(sample): add compact guide for azure function integration * docs(example): add azure functions integration example * Move azure function to `docs` folder * Update generated en.json * Remove azure example * Fix typo * Remove link * Fix typos --------- Co-authored-by: Michał Lytek <[email protected]>
1 parent 3a31dbe commit 9ae96c0

File tree

3 files changed

+122
-1
lines changed

3 files changed

+122
-1
lines changed

docs/azure-functions.md

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
---
2+
title: Azure Functions Integration
3+
---
4+
5+
## Using TypeGraphQL in Microsoft Azure Functions
6+
7+
Integrating TypeGraphQL with Azure Functions involves the following key steps:
8+
9+
1. Generate GraphQL schema based on your resolvers
10+
2. Notify Apollo Server about your schema
11+
12+
Below is how you can implement the azure function entry point (with explanations in-line):
13+
14+
```ts
15+
// index.ts
16+
17+
import "reflect-metadata";
18+
import path from "path";
19+
import { ApolloServer } from "@apollo/server";
20+
import { startServerAndCreateHandler } from "@as-integrations/azure-functions";
21+
import { buildSchemaSync } from "type-graphql";
22+
import { Container } from "typedi";
23+
import { GraphQLFormattedError } from "graphql";
24+
import { UserResolver } from "YOUR_IMPORT_PATH"; // TypeGraphQL Resolver
25+
import { AccountResolver } from "YOUR_IMPORT_PATH"; // TypeGraphQL Resolver
26+
27+
// Bundle resolvers to build the schema
28+
const schema = buildSchemaSync({
29+
// Include resolvers you'd like to expose to the API
30+
// Deployment to Azure functions might fail if
31+
// you include too much resolvers (means your app is too big)
32+
resolvers: [
33+
UserResolver,
34+
AccountResolver,
35+
// your other resolvers
36+
],
37+
38+
// Only build the GraphQL schema locally
39+
// The resulting schema.graphql will be generated to the following path:
40+
// Path: /YOUR_PROJECT/src/schema.graphql
41+
emitSchemaFile: process.env.NODE_ENV === "local" ? path.resolve("./src/schema.graphql") : false,
42+
container: Container,
43+
validate: true,
44+
});
45+
46+
// Add schema into Apollo Server
47+
const server = new ApolloServer({
48+
// include your schema
49+
schema,
50+
51+
// only allow introspection in non-prod environments
52+
introspection: process.env.NODE_ENV !== "production",
53+
54+
// you can handle errors in your own styles
55+
formatError: (err: GraphQLFormattedError) => err,
56+
});
57+
58+
// Start the server(less handler/function)
59+
export default startServerAndCreateHandler(server);
60+
```
61+
62+
Each Azure Function needs to have an equivalent configuration file called `function.json`, here's how you can configure it:
63+
64+
```json
65+
// function.json
66+
67+
{
68+
"bindings": [
69+
{
70+
"authLevel": "anonymous",
71+
"type": "httpTrigger",
72+
"direction": "in",
73+
"name": "req",
74+
"route": "graphql",
75+
"methods": ["get", "post", "options"]
76+
},
77+
{
78+
"type": "http",
79+
"direction": "out",
80+
"name": "$return"
81+
}
82+
],
83+
"scriptFile": "../dist/handler-graphql/index.js"
84+
}
85+
```
86+
87+
For better maintainability of your codebase, we recommend separate your Azure Functions into its own folders, away from the actual GraphQL Resolvers. Here's an example:
88+
89+
```text
90+
/YOUR_PROJECT
91+
/handlers
92+
/handler-graphql
93+
index.ts
94+
function.json
95+
/handler-SOME-OTHER-FUNCTION-1
96+
index.ts
97+
function.json
98+
/handler-SOME-OTHER-FUNCTION-2
99+
index.ts
100+
function.json
101+
102+
/src
103+
/resolvers
104+
user.resolver.ts
105+
account.resolver.ts
106+
/services
107+
user.service.ts
108+
account.service.ts
109+
110+
package.json
111+
host.json
112+
.eslintrc.js
113+
.prettierrc
114+
.eslintignore
115+
.prettierignore
116+
117+
etc etc etc...
118+
```

website/i18n/en.json

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
"aws-lambda": {
1212
"title": "AWS Lambda integration"
1313
},
14+
"azure-functions": {
15+
"title": "Azure Functions Integration"
16+
},
1417
"bootstrap": {
1518
"title": "Bootstrapping"
1619
},

website/sidebars.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
],
3232
"Integrations": ["prisma", "nestjs"],
3333
"Others": ["emit-schema", "performance"],
34-
"Recipes": ["browser-usage", "aws-lambda"]
34+
"Recipes": ["browser-usage", "aws-lambda", "azure-functions"]
3535
},
3636
"examples": {
3737
"Examples": ["examples"]

0 commit comments

Comments
 (0)