Skip to content

Instantly share code, notes, and snippets.

@lukepighetti
Created December 2, 2018 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukepighetti/efca1b41766d873f14cc4e0505602616 to your computer and use it in GitHub Desktop.
Save lukepighetti/efca1b41766d873f14cc4e0505602616 to your computer and use it in GitHub Desktop.
Apollo-server with Firebase
// index.js
const admin = require("firebase-admin");
const functions = require("firebase-functions");
const express = require("express");
admin.initializeApp();
const { ApolloServer, gql } = require("apollo-server-express");
// Construct a schema, using GraphQL schema language
const typeDefs = gql`
type Hotdog {
isKosher: Boolean
location: String
name: String
style: String
website: String
}
type Query {
hotdogs: [Hotdog]
}
`;
// Provide resolver functions for your schema fields
const resolvers = {
Query: {
hotdogs: () =>
admin
.database()
.ref("hotdogs")
.once("value")
.then(snap => snap.val())
.then(val => Object.keys(val).map(key => val[key]))
}
};
// setup express cloud function
const app = express();
const server = new ApolloServer({ typeDefs, resolvers });
server.applyMiddleware({ app, path: "/", cors: true });
exports.graphql = functions.https.onRequest(app);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment