Skip to content

Instantly share code, notes, and snippets.

@brygrill
Last active March 3, 2021 23:00
Show Gist options
  • Save brygrill/287debab77de55790fab37c77580d6e4 to your computer and use it in GitHub Desktop.
Save brygrill/287debab77de55790fab37c77580d6e4 to your computer and use it in GitHub Desktop.
Deploying a GraphQL Server with Firebase Functions
// sample graphql server deployed with firebase functions
// pulling some basic stockmarket data from a firebase db
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const express = require('express');
const graphqlHTTP = require('express-graphql');
const values = require('lodash.values');
const { GraphQLSchema, GraphQLObjectType, GraphQLList, GraphQLString } = require('graphql');
// Init express
const app = express();
// Init Firebase admin
admin.initializeApp(functions.config().firebase);
// connect to db
const db = admin.database();
const ref = db.ref('poll');
// Construct Securities Type
const SecurityType = new GraphQLObjectType({
name: 'Security',
description: 'A stock market security',
fields: () => ({
symbol: { type: GraphQLString },
name: { type: GraphQLString },
open: { type: GraphQLString },
last: { type: GraphQLString },
percDay: { type: GraphQLString },
status: { type: GraphQLString },
volume: { type: GraphQLString },
lastUpdatedAt: { type: GraphQLString },
}),
});
// Construct root query
const query = new GraphQLObjectType({
name: 'Query',
description: 'The Root Query',
fields: () => ({
securities: {
type: new GraphQLList(SecurityType),
description: 'A list of all securities',
resolve() {
return ref.child('securities').once('value').then(data => {
return values(data.val());
});
},
},
}),
});
// Construct the schema
const schema = new GraphQLSchema({
query,
});
app.use('/', graphqlHTTP({
schema,
graphiql: true,
}));
exports.graphql = functions.https.onRequest(app);
@Amzd
Copy link

Amzd commented Jan 30, 2021

I know you've uploaded this a long time ago but I'm trying to use this now and get an error. If you've ever encountered this I'd love to hear how you worked around it.

Error: listen EADDRINUSE: address already in use :::8080 
at Server.setupListenHandle [as _listen2] (net.js:1280:14) 
at listenInCluster (net.js:1328:12) 
at Server.listen (net.js:1415:7) 
at Object.<anonymous> (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/index.js:85:8) 
at Module._compile (internal/modules/cjs/loader.js:778:30) 
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10) 
at Module.load (internal/modules/cjs/loader.js:653:32) 
at tryModuleLoad (internal/modules/cjs/loader.js:593:12) 
at Function.Module._load (internal/modules/cjs/loader.js:585:3) 
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment