Apollo Server 3 is officially end-of-life as of 22 October 2024.

Learn more about upgrading.

Deploying with Google Cloud Functions


This tutorial helps you deploy Apollo Server to Google Cloud Functions. It uses the following example function handler:

Click to expand
JavaScript
index.js
1const { ApolloServer, gql } = require('apollo-server-cloud-functions');
2const {
3  ApolloServerPluginLandingPageLocalDefault
4} = require('apollo-server-core');
5
6// Construct a schema, using GraphQL schema language
7const typeDefs = gql`
8  type Query {
9    hello: String
10  }
11`;
12
13// Provide resolver functions for your schema fields
14const resolvers = {
15  Query: {
16    hello: () => 'Hello world!',
17  },
18};
19
20const server = new ApolloServer({
21  typeDefs,
22  resolvers,
23  csrfPrevention: true,
24  cache: 'bounded',
25  plugins: [
26    ApolloServerPluginLandingPageLocalDefault({ embed: true }),
27  ],
28});
29
30exports.handler = server.createHandler();

Deploying from the Google Cloud Console

1. Configure the function

From your Google Cloud Console, go to the Cloud Functions page.

Click Create Function. Give the function a name and set the Trigger type to HTTP.

For quick setup and access to the GraphQL endpoint/landing page, choose to Allow unauthenticated invocations. To require authentication for this endpoint, you can manage authorized users via Cloud IAM.

Save your configuration changes in the Trigger section. Copy the trigger's URL for later.

Expand the "Runtime, Build, and Connections Settings" panel and add a runtime environment variable named NODE_ENV with value development. (This overrides Cloud Function's default of production and will allow you to easily test your graph with Apollo Sandbox. You can delete this environment variable once you're ready for your app to be considered a production app.)

Now click Next.

2. Write the API handlers and deploy

Now on the Code page, set the runtime to a currently supported version of Node.js (such as Node.js 14), and set the Entry point to handler.

Paste the example code at the top of this page into the contents of index.js in the code editor.

Edit package.json so that it lists apollo-server-cloud-functions and graphql in its dependencies:

JSON
package.json
1  "dependencies": {
2    "apollo-server-cloud-functions": "3.x",
3    "graphql": "^15.5.0"
4  }

Click Deploy to initiate deployment. Then, proceed to Testing the function.

Deploying from your local machine

Before proceeding, you need to set up the gcloud SDK:

  1. Install the gcloud SDK

  2. Initialize the gcloud SDK and authenticate your Google account

Next, initialize a new Node.js project by running npm init in an empty directory.

Run npm install apollo-server-cloud-functions graphql to install the necessary dependencies and to include them in the package.json file.

At this point, your package.json should resemble the following:

JSON
1{
2  "name": "apollo-gcloud",
3  "version": "1.0.0",
4  "description": "",
5  "main": "index.js",
6  "scripts": {
7    "test": "echo \"Error: no test specified\" && exit 1"
8  },
9  "author": "",
10  "license": "ISC",
11  "dependencies": {
12    "apollo-server-cloud-functions": "3.x",
13    "graphql": "^15.5.0"
14  }
15}

Create a new file named index.js and paste the sample code at the top of this page into it.

Run the following command to create and deploy the function to Cloud Functions:

Bash
1gcloud functions deploy apollo-graphql-example --entry-point handler --runtime nodejs14 --trigger-http

This creates a function named apollo-graphql-example that you can view from your console's Cloud Functions page

The command asks some configuration questions and prints metadata about your newly created function, which includes the function's trigger URL.

For more information, see the official Cloud Functions docs.

Testing the function

After deployment completes, navigate to your function's trigger URL, with /graphql added to the end. If deployment succeeded, you should see your server's landing page.

If you can't access your trigger URL, you might need to give allAuthenticatedUsers or allUsers the Cloud Functions Invoker role on the Permissions tab.

Click Query your Server and use Apollo Sandbox to test the following query:

GraphQL
1query TestQuery {
2  hello
3}

And verify that the following response appears:

JSON
1{
2  "data": {
3    "hello": "Hello world!"
4  }
5}

Getting request details

To obtain information about a currently executing Google Cloud Function (HTTP headers, HTTP method, body, path, etc.) use the context option. This enables you to pass any request-specific data to your server's resolvers.

JavaScript
1const { ApolloServer, gql } = require('apollo-server-cloud-functions');
2const {
3  ApolloServerPluginLandingPageLocalDefault
4} = require('apollo-server-core');
5
6// Construct a schema, using GraphQL schema language
7const typeDefs = gql`
8  type Query {
9    hello: String
10  }
11`;
12
13// Provide resolver functions for your schema fields
14const resolvers = {
15  Query: {
16    hello: () => 'Hello world!',
17  },
18};
19
20const server = new ApolloServer({
21  typeDefs,
22  resolvers,
23  csrfPrevention: true,
24  cache: 'bounded',
25  context: ({ req, res }) => ({
26    headers: req.headers,
27    req,
28    res,
29  }),
30  plugins: [
31    ApolloServerPluginLandingPageLocalDefault({ embed: true }),
32  ],
33});
34
35exports.handler = server.createHandler();

Enabling production mode

When you consider your app to be production-ready, you should remember to edit it to remove the NODE_ENV environment variable you set at the beginning. Among other things, this will change its landing page to be more discreet, and will mask error details from the end user.

Feedback

Edit on GitHub

Forums