v3 (latest)
Integrations
Integration with Gcp Cloud Run

Integration with Google Cloud Platform

Google Cloud Run is a Platform as a Service by Google. It is straightforward to use Yoga with it.

Prerequisites

You will first need to install GCP command line tool : gcloud. You can find instructions here (opens in a new tab).

If you already had gcloud installed, make sure it is up to date with gcloud components update

Create a new project (opens in a new tab) and make sure billing is enabled (opens in a new tab)

Installation

Create a new Node project and add Yoga to it's dependencies.

Terminal
yarn add graphql
yarn add graphql-yoga
💡

This example uses ESM syntax, so you set "type": "module" in your package.json.

Add a start script to your package.json. Cloud Run needs to know how to start your application.

{
  "name": "graphql-yoga-cloud-run-guide",
  "version": "1.0.0",
  "type": "module",
  "main": "src/index.js",
  "scripts": {
    "start": "node ."
  },
  "dependencies": {
    "graphql": "^16.6.0",
    "graphql-yoga": "^3.9.1"
  }
}

Usage

Create a graphql server with your schema. You can use any http server, here we will need node's http implementation.

import { createSchema, createYoga } from 'graphql-yoga'
import { createServer } from 'node:http'
 
const yoga = createYoga({
  schema: createSchema({
    typeDefs: /* GraphQL */ `
      type Query {
        greetings: String
      }
    `,
    resolvers: {
      Query: {
        greetings: () =>
          'This is the `greetings` field of the root `Query` type'
      }
    }
  })
})
 
const server = createServer(yoga)
const port = parseInt(process.env.PORT) || 4000
 
server.listen(port, () => {
  console.info(
    `Server is running on http://localhost:${port}${yoga.graphqlEndpoint}`
  )
})

You can now deploy to Cloud Run using. You can use all default value, except the last one which allow unauthenticated access to your service.

$ gcloud run deploy --source .
💡

If this is your first time using Cloud Run, the enabling of the service can take up to few minutes to really be effective. If you have some 403 Forbidden errors, just wait 2 minutes and try again.

You can now access to your API using the url given by gcloud. The default graphlq endpoint is /graphql.

It is also possible to use Typescript or any other tool requiring a build phase (such as codegen). Add a Dockerfile to the root of your project so that Cloud Run build a custom image for you.

You can also check a full example on our GitHub repository here (opens in a new tab)