Apollo-server: Google cloud functions?

Created on 12 Apr 2017  路  16Comments  路  Source: apollographql/apollo-server

Can I use the lambda package and run it on google cloud? I'm looking into using google cloud functions with the project. Sorry I didn't investigate first. Just wanted to see if anyone else is interested as well!

Most helpful comment

This graphql-subscription issue is tracking that. I'm looking into managed PubSub services, particularly Google Cloud PubSub at the moment.

All 16 comments

The AWS Lambda integration is designed to export a function for API Gateway proxy integration not Google Cloud. You would be better off using the graphql-server-core package and converting the request object into one that can be read by that package.

Is google cloud functions still in beta? It might be too early to write an integration for it.

Thanks for the info. Yes it is still in beta

I'd also be happy to accept a PR that adds a separate package for google cloud functions, just like we have one for lambda. :wink:

Is there anywhere I should look to better understand the internals? Or just look over the current code to make my own :)

Yeah, I would just look at what the lambda or micro package does and then roll your own! It's pretty straightforward.

@zackify the http logic itself is pretty abstract in graphql-core
you can take a quick look at the integrations themselves and see that they are just "adapters".
should be pretty easy to add support..

I have also been looking into hosting GraphQL on GCP Cloud Functions / Cloud Functions for Firebase. I've come across a number of resources that have confused me with the use of ExpressJS in the context of Cloud Functions:

Here I see people passing an ExpressJS server into the Firebase Cloud Functions functions.https.onRequest with a Firebase-Samples example using the same method here.

But then I came across a single person here who has concerns about creating an express app in a Cloud Function. This makes me question whether I should simply use graphql-server-express instead of working on a custom integration.

So I'm not sure which method here is correct in the context of ExpressJS in Cloud Functions for Firebase. I've posted on SO with the following questions:

  • How does Cloud Functions for Firebase or GCP Cloud Functions handle the lifetime of objects initialised outside of the function definition?
  • How does the above effect the lifetime of the function? Does it run until timeout or function similarly to AWS Lambda?
  • The functions.https.onRequest documentation doesn't specify you can pass an ExpressJS server object into it, so how is this working? Are there then two endpoints? Can someone explain what is happening here?

TLDR; if you can use ExpressJS server as described here in Google/Firebase Cloud Functions could we then not just rely on using graphql-server-express in the same manner?

It does seem like the google cloud function takes in an express Request/Response object.
https://cloud.google.com/functions/docs/writing/http

It also seems like the firebase google cloud function has several triggers. The http trigger is created using this function functions.https.onRequest. Which takes in the same express Request and Response objects.
https://firebase.google.com/docs/functions/http-events

Does seem like the implementation of graphql-server-express might work without modification.

Not sure how to answer the other questions about function lifetime still learning about GCP.

I can confirm that the graphql-server-express is easily setup in Cloud Functions. It's more stable than the express-graphql implementation as it doesn't appear to error on the absent trailing '/'.

Sample code:

// graphql-server-express
var functions = require("firebase-functions");
const bodyParser = require("body-parser");
const express = require("express");
const graphqlExpress = require("graphql-server-express").graphqlExpress;
const mySchema = ...;
var graphQLServer = express();
graphQLServer.use("*", bodyParser.json(), graphqlExpress({ schema: mySchema }));

// CF for Firebase with graphql-server-express
exports.graphql = functions.https.onRequest(graphQLServer);

called using https://us-central1-<project-name>.cloudfunctions.net/graphql.

however to use GraphiQL as well you must do the following:

// import graphiql
const graphiqlExpress = require("graphql-server-express").graphiqlExpress;

// assign specific URLs - in place of the "*" catchall
graphQLServer.use("/graphql", bodyParser.json(), graphqlExpress({ schema: mySchema }));
graphQLServer.use("/graphiql", graphiqlExpress({ endpointURL: "/graphql" }));

and then call the method with https://us-central1-<project-name>.cloudfunctions.net/graphql/graphql and https://us-central1-<project-name>.cloudfunctions.net/graphql/graphiql (you'd probably want to change the function export to api/graphql etc).

I still do not have an answer for how definitions of objects outside the function definition are handled or how Cloud Functions can take an entire ExpressJS object and just magically work seemingly stripping the object back to it's request, response parts. I've asked a detailed question on SO and asked in the Firebase slack channel so am not expecting an answer here.

Assuming there are no drawbacks to initializing the express object outside of the function export, I personally don't see a reason in increasing the code surface area by creating a graphql-server-cloud-function package and will no longer be pursuing that myself. Thanks for the help @soda0289

Also, how would you do this with subscriptions? Is that not possible I'm guessing?

This graphql-subscription issue is tracking that. I'm looking into managed PubSub services, particularly Google Cloud PubSub at the moment.

@jthegedus just to close the loop, would you mind sending a PR to the README and docs here adding the code snippet about google cloud functions?

@stubailo It would be my pleasure :smile:

@stubailo @jthegedus did an official best practice example ever make the docs? I presume not as I can't seem to find it in in the server docs

Are their any plans to add one?

I have used the the the example walk through in this article and it worked well. Could I suggest reviewing it and maybe adding it to the docs ?

let me know if there is any way I could help with that. 馃槃

@Paddy-Hamilton I did record this early Jan in my backlog of things to do. I've scheduled time this weekend to address this. I'll ping you in the PR if you wish to give feedback. (that example you linked does actually reference my article for doing this on Firebase, I just never got around to vanilla Google Cloud Functions)

@jthegedus ha, so it does, thanks for the help 馃憤

Sounds good, will take a look at the PR, although i'm a relative graphql and GCF newbie so my feedback maybe limited. I'll help in whatever way I can.
Cheers 馃嵒

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mathroc picture mathroc  路  3Comments

dupski picture dupski  路  3Comments

stevezau picture stevezau  路  3Comments

danilobuerger picture danilobuerger  路  3Comments

veeramarni picture veeramarni  路  3Comments