Skip to content

Instantly share code, notes, and snippets.

@piuccio
Last active November 16, 2020 15:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save piuccio/f01c4a7ccc09ca35abdbd1652c820203 to your computer and use it in GitHub Desktop.
Save piuccio/f01c4a7ccc09ca35abdbd1652c820203 to your computer and use it in GitHub Desktop.
Shared code to run local apollo server next to firebase functions
const gql = require('graphql-tag');
const { makeExecutableSchema } = require('graphql-tools');
const books = [
// ...
];
const typeDefs = gql`
# ...
`;
const resolvers = {
Query: {
books: () => books,
},
};
module.exports = {
schema: makeExecutableSchema({
typeDefs,
resolvers,
}),
// Bonus function, to remove stack trace on production
// https://www.apollographql.com/docs/apollo-server/features/errors
formatError: (error) => {
if (process.env.NODE_ENV === 'production') {
delete error.extensions.exception;
}
return error;
},
}
const { ApolloServer } = require('apollo-server-cloud-functions');
const functions = require('firebase-functions');
const config = require('./lib/config');
const server = new ApolloServer(config);
const handler = server.createHandler({
cors: {
origin: true,
credentials: true,
},
});
exports.handler = functions.https.onRequest(handler);
const { ApolloServer } = require('apollo-server');
const config = require('./config');
const server = new ApolloServer(config);
// This `listen` method launches a web-server. Existing apps
// can utilize middleware options, which we'll discuss later.
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment