Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(sample): add guide for integrating with azure functions #1637

Merged
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...
```
54 changes: 54 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 Expand Up @@ -757,6 +760,57 @@
"version-2.0.0-beta.6/version-2.0.0-beta.6-validation": {
"title": "Argument and Input validation",
"sidebar_label": "Validation"
},
"version-2.0.0-rc.1/version-2.0.0-rc.1-authorization": {
"title": "Authorization"
},
"version-2.0.0-rc.1/version-2.0.0-rc.1-complexity": {
"title": "Query complexity"
},
"version-2.0.0-rc.1/version-2.0.0-rc.1-custom-decorators": {
"title": "Custom decorators"
},
"version-2.0.0-rc.1/version-2.0.0-rc.1-dependency-injection": {
"title": "Dependency injection"
},
"version-2.0.0-rc.1/version-2.0.0-rc.1-examples": {
"title": "Examples",
"sidebar_label": "List of examples"
},
"version-2.0.0-rc.1/version-2.0.0-rc.1-extensions": {
"title": "Extensions"
},
"version-2.0.0-rc.1/version-2.0.0-rc.1-generic-types": {
"title": "Generic Types"
},
"version-2.0.0-rc.1/version-2.0.0-rc.1-inheritance": {
"title": "Inheritance"
},
"version-2.0.0-rc.1/version-2.0.0-rc.1-installation": {
"title": "Installation"
},
"version-2.0.0-rc.1/version-2.0.0-rc.1-interfaces": {
"title": "Interfaces"
},
"version-2.0.0-rc.1/version-2.0.0-rc.1-middlewares": {
"title": "Middleware and guards"
},
"version-2.0.0-rc.1/version-2.0.0-rc.1-migration-guide": {
"title": "Migration Guide",
"sidebar_label": "v1.x -> v2.0"
},
"version-2.0.0-rc.1/version-2.0.0-rc.1-resolvers": {
"title": "Resolvers"
},
"version-2.0.0-rc.1/version-2.0.0-rc.1-subscriptions": {
"title": "Subscriptions"
},
"version-2.0.0-rc.1/version-2.0.0-rc.1-unions": {
"title": "Unions"
},
"version-2.0.0-rc.1/version-2.0.0-rc.1-validation": {
"title": "Argument and Input validation",
"sidebar_label": "Validation"
}
},
"links": {
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