An Apollo Server integration for use with Azure Functions.
This is a simple package allows you to integrate Apollo Server into an Azure Functions app.
- Node.js v22 or later
- Azure Functions v4 or later
- GraphQL.js v16 or later
- Apollo Server v4 or later
npm install @as-integrations/azure-functions @apollo/server graphql @azure/functions- Setup an Azure Function with TypeScript (or JavaScript) using the v4 programming model
- Create a new HTTP Trigger
- Update your function file (e.g.,
src/functions/graphql.ts) to use the Apollo integration:
import { ApolloServer } from '@apollo/server';
import { app } from '@azure/functions';
import { startServerAndCreateHandler } from '@as-integrations/azure-functions';
// The GraphQL schema
const typeDefs = `#graphql
type Query {
hello: String
}
`;
const resolvers = {
Query: {
hello: () => 'world',
},
};
// Set up Apollo Server
const server = new ApolloServer({
typeDefs,
resolvers,
});
app.http('graphql', {
handler: startServerAndCreateHandler(server),
});- Run the Azure Functions app and navigate to the function endpoint (e.g.,
http://localhost:7071/api/graphql)
You can pass custom context to your resolvers:
import { ApolloServer } from '@apollo/server';
import { app, HttpRequest, InvocationContext } from '@azure/functions';
import { startServerAndCreateHandler } from '@as-integrations/azure-functions';
// Define the context type
type MyContext = {
user: string | null;
isAuthenticated: boolean;
};
// Context creation function
async function createContext(
req: HttpRequest,
_context: InvocationContext,
_body: any,
): Promise<MyContext> {
const authHeader = req.headers.get('authorization');
return {
user: authHeader ? authHeader : null,
isAuthenticated: authHeader !== null,
};
}
const server = new ApolloServer<MyContext>({
typeDefs,
resolvers,
});
app.http('graphql', {
handler: startServerAndCreateHandler(server, {
context: async (args) => createContext(args.req, args.context, args.body),
}),
});To deploy your GraphQL API to Azure:
-
Using VS Code:
- Install the Azure Functions extension
- Right-click your function app in the Azure Functions panel and select "Deploy to Function App"
-
Using Azure CLI:
# Build your app npm run build # Deploy to Azure func azure functionapp publish <YOUR_FUNCTION_APP_NAME>
-
Using CI/CD:
- Configure GitHub Actions or Azure DevOps for automated deployments
For more details, see the Azure Functions deployment documentation.
Several working examples are available in the samples folder, including:
- Simple GraphQL setup
- GraphQL with context
- GraphQL with error handling
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Aaron Powell (aaronpowell)
- Charles Fonseca (barddoo)