Skip to content

Commit

Permalink
Add an example for GCP Cloud Run (#2737)
Browse files Browse the repository at this point in the history
* add example for GCP Cloud Run

* fix example package.json
  • Loading branch information
EmrysMyrddin authored May 8, 2023
1 parent 7a83869 commit 2b8e064
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 1 deletion.
11 changes: 11 additions & 0 deletions examples/gcp-cloud-run/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM node:20-slim

WORKDIR /usr/src/app

COPY package.json ./

RUN npm install

COPY src src

ENTRYPOINT npm run start
16 changes: 16 additions & 0 deletions examples/gcp-cloud-run/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "graphql-yoga-cloud-run-guide",
"version": "1.0.0",
"description": "",
"type": "module",
"main": "src/index.js",
"scripts": {
"start": "node ."
},
"author": "",
"license": "MIT",
"dependencies": {
"graphql": "^16.6.0",
"graphql-yoga": "^3.9.1"
}
}
27 changes: 27 additions & 0 deletions examples/gcp-cloud-run/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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}`,
)
})
15 changes: 14 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

100 changes: 100 additions & 0 deletions website/src/pages/docs/integrations/integration-with-gcp-cloud-run.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
---
description: Google Cloud Run is a Platform as a Service by Google. It is straight forward to use Yoga with it.
---

import { Callout, PackageCmd } from '@theguild/components'

# 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](https://cloud.google.com/sdk/docs/install).

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

[Create a new project](https://cloud.google.com/resource-manager/docs/creating-managing-projects) and make sure [billing is enabled](https://cloud.google.com/billing/docs/how-to/verify-billing-enabled)

## Installation

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

<PackageCmd packages={['graphql', 'graphql-yoga']} />

<Callout>
This example uses ESM syntax, so you set `"type": "module"` in your
`package.json`.
</Callout>

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

```json
{
"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.

```js
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.

```bash
$ gcloud run deploy --source .
```

<Callout>
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.
</Callout>

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](https://github.com/dotansimha/graphql-yoga/tree/v3/examples/gcp-cloud-run)

0 comments on commit 2b8e064

Please sign in to comment.