Skip to content

Instantly share code, notes, and snippets.

@CreatiCoding
Created April 4, 2020 16:42
Show Gist options
  • Select an option

  • Save CreatiCoding/33d062f4e05398d5cbadba3956c8a95d to your computer and use it in GitHub Desktop.

Select an option

Save CreatiCoding/33d062f4e05398d5cbadba3956c8a95d to your computer and use it in GitHub Desktop.
Firebase Cloud Functions GraphQL
import { https } from "firebase-functions";
import * as express from "express";
import { ApolloServer, gql } from "apollo-server-express";
const typeDefs = gql`
# Comments in GraphQL strings (such as this one) start with the hash (#) symbol.
# This "Book" type defines the queryable fields for every book in our data source.
type Book {
title: String
author: String
}
# The "Query" type is special: it lists all of the available queries that
# clients can execute, along with the return type for each. In this
# case, the "books" query returns an array of zero or more Books (defined above).
type Query {
books: [Book]
}
`;
const books = [
{
title: "Harry Potter and the Chamber of Secrets",
author: "J.K. Rowling",
},
{
title: "Jurassic Park",
author: "Michael Crichton",
},
];
const resolvers = {
Query: {
books: () => books,
},
};
const server = new ApolloServer({ typeDefs, resolvers });
const app = express();
server.applyMiddleware({ app });
// https://github.com/arjunyel/firestore-apollo-graphql
// ex. /api/graphql?query={books{title%20author}}
export const api = https.onRequest(app);
export const helloWorld = https.onRequest((request, response) => {
request;
response.send("Hello from Firebase!");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment