Skip to content

Commit

Permalink
docs(sample): add guide for integrating with azure functions (#1637)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
Mingyang-Li and MichalLytek authored May 30, 2024
1 parent 3a31dbe commit 9ae96c0
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 1 deletion.
118 changes: 118 additions & 0 deletions docs/azure-functions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
---
title: Azure Functions Integration
---

## Using TypeGraphQL in Microsoft Azure Functions

Integrating TypeGraphQL with Azure Functions involves the following key steps:

1. Generate GraphQL schema based on your resolvers
2. Notify Apollo Server about your schema

Below is how you can implement the azure function entry point (with explanations in-line):

```ts
// index.ts

import "reflect-metadata";
import path from "path";
import { ApolloServer } from "@apollo/server";
import { startServerAndCreateHandler } from "@as-integrations/azure-functions";
import { buildSchemaSync } from "type-graphql";
import { Container } from "typedi";
import { GraphQLFormattedError } from "graphql";
import { UserResolver } from "YOUR_IMPORT_PATH"; // TypeGraphQL Resolver
import { AccountResolver } from "YOUR_IMPORT_PATH"; // TypeGraphQL Resolver

// Bundle resolvers to build the schema
const schema = buildSchemaSync({
// Include resolvers you'd like to expose to the API
// Deployment to Azure functions might fail if
// you include too much resolvers (means your app is too big)
resolvers: [
UserResolver,
AccountResolver,
// your other resolvers
],

// Only build the GraphQL schema locally
// The resulting schema.graphql will be generated to the following path:
// Path: /YOUR_PROJECT/src/schema.graphql
emitSchemaFile: process.env.NODE_ENV === "local" ? path.resolve("./src/schema.graphql") : false,
container: Container,
validate: true,
});

// Add schema into Apollo Server
const server = new ApolloServer({
// include your schema
schema,

// only allow introspection in non-prod environments
introspection: process.env.NODE_ENV !== "production",

// you can handle errors in your own styles
formatError: (err: GraphQLFormattedError) => err,
});

// Start the server(less handler/function)
export default startServerAndCreateHandler(server);
```

Each Azure Function needs to have an equivalent configuration file called `function.json`, here's how you can configure it:

```json
// function.json

{
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"route": "graphql",
"methods": ["get", "post", "options"]
},
{
"type": "http",
"direction": "out",
"name": "$return"
}
],
"scriptFile": "../dist/handler-graphql/index.js"
}
```

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:

```text
/YOUR_PROJECT
/handlers
/handler-graphql
index.ts
function.json
/handler-SOME-OTHER-FUNCTION-1
index.ts
function.json
/handler-SOME-OTHER-FUNCTION-2
index.ts
function.json
/src
/resolvers
user.resolver.ts
account.resolver.ts
/services
user.service.ts
account.service.ts
package.json
host.json
.eslintrc.js
.prettierrc
.eslintignore
.prettierignore
etc etc etc...
```
3 changes: 3 additions & 0 deletions website/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
"aws-lambda": {
"title": "AWS Lambda integration"
},
"azure-functions": {
"title": "Azure Functions Integration"
},
"bootstrap": {
"title": "Bootstrapping"
},
Expand Down
2 changes: 1 addition & 1 deletion website/sidebars.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
],
"Integrations": ["prisma", "nestjs"],
"Others": ["emit-schema", "performance"],
"Recipes": ["browser-usage", "aws-lambda"]
"Recipes": ["browser-usage", "aws-lambda", "azure-functions"]
},
"examples": {
"Examples": ["examples"]
Expand Down

0 comments on commit 9ae96c0

Please sign in to comment.