Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Google cloud functions? #357

Closed
zackify opened this issue Apr 12, 2017 · 16 comments
Closed

Google cloud functions? #357

zackify opened this issue Apr 12, 2017 · 16 comments

Comments

@zackify
Copy link

zackify commented Apr 12, 2017

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!

@soda0289
Copy link
Contributor

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.

@zackify
Copy link
Author

zackify commented Apr 12, 2017

Thanks for the info. Yes it is still in beta

@helfer
Copy link
Contributor

helfer commented Apr 19, 2017

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. 😉

@zackify
Copy link
Author

zackify commented Apr 19, 2017

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

@helfer
Copy link
Contributor

helfer commented Apr 19, 2017

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

@DxCx
Copy link
Contributor

DxCx commented Apr 22, 2017

@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..

@jthegedus
Copy link

jthegedus commented Apr 24, 2017

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?

@soda0289
Copy link
Contributor

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.

@jthegedus
Copy link

jthegedus commented Apr 24, 2017

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

@zackify
Copy link
Author

zackify commented Apr 25, 2017

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

@jthegedus
Copy link

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

@stubailo
Copy link
Contributor

@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?

@jthegedus
Copy link

@stubailo It would be my pleasure 😄

@Paddy-Hamilton
Copy link

@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. 😄

@jthegedus
Copy link

jthegedus commented Mar 13, 2018

@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)

@Paddy-Hamilton
Copy link

@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 🍻

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants