Skip to content

apollo-server-integrations/apollo-server-integration-azure-functions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

67 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Apollo Server Integration for Azure Functions

Build Release npm (scoped)

Introduction

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.

Requirements

Installation

npm install @as-integrations/azure-functions @apollo/server graphql @azure/functions

Usage

Basic Setup

  1. Setup an Azure Function with TypeScript (or JavaScript) using the v4 programming model
  2. Create a new HTTP Trigger
  3. 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),
});
  1. Run the Azure Functions app and navigate to the function endpoint (e.g., http://localhost:7071/api/graphql)

Using Context

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),
  }),
});

Deployment

To deploy your GraphQL API to Azure:

  1. Using VS Code:

    • Install the Azure Functions extension
    • Right-click your function app in the Azure Functions panel and select "Deploy to Function App"
  2. Using Azure CLI:

    # Build your app
    npm run build
    
    # Deploy to Azure
    func azure functionapp publish <YOUR_FUNCTION_APP_NAME>
  3. Using CI/CD:

For more details, see the Azure Functions deployment documentation.

Examples

Several working examples are available in the samples folder, including:

  • Simple GraphQL setup
  • GraphQL with context
  • GraphQL with error handling

Contributing

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.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Support

Contributors

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors