-
Notifications
You must be signed in to change notification settings - Fork 580
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an example for GCP Cloud Run (#2737)
* add example for GCP Cloud Run * fix example package.json
- Loading branch information
1 parent
7a83869
commit 2b8e064
Showing
5 changed files
with
168 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`, | ||
) | ||
}) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
100 changes: 100 additions & 0 deletions
100
website/src/pages/docs/integrations/integration-with-gcp-cloud-run.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |