Skip to content

Instantly share code, notes, and snippets.

@colinfwren
Created January 9, 2022 00:09
Show Gist options
  • Save colinfwren/aee56197750196184a42c4778565afd0 to your computer and use it in GitHub Desktop.
Save colinfwren/aee56197750196184a42c4778565afd0 to your computer and use it in GitHub Desktop.
Run a Apollo Server on Firebase Functions
/**
* Works with [email protected] [email protected] [email protected] [email protected]
* You'll need to add `skipLibCheck` for this to work as cors middleware types seems to have an issue in apollo-server-express
*/
import {ExpressContext, gql} from "apollo-server-express";
import {ApolloServer,Config} from "apollo-server-cloud-functions";
import * as functions from "firebase-functions";
const books = [
{
title: "The Awakening",
author: "Kate Chopin",
},
{
title: "City of Glass",
author: "Paul Auster",
},
];
const typeDefs = gql`
type Book {
title: String
author: String
}
type Query {
books: [Book]
}
`;
const resolvers = {
Query: {
books: () => books,
},
};
const graphqlConfig: Config<ExpressContext> = {
typeDefs,
resolvers,
};
const server = new ApolloServer(graphqlConfig);
const handler = server.createHandler();
// Have to cast to any as although the createHandler call will return a function with req, res args TS throws a wobbly
exports.graphql = functions.https.onRequest(handler as any);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment