Created
December 2, 2018 14:29
-
-
Save lukepighetti/efca1b41766d873f14cc4e0505602616 to your computer and use it in GitHub Desktop.
Apollo-server with Firebase
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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