diff --git a/examples/gcp-cloud-run/Dockerfile b/examples/gcp-cloud-run/Dockerfile new file mode 100644 index 0000000000..a8646d8146 --- /dev/null +++ b/examples/gcp-cloud-run/Dockerfile @@ -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 diff --git a/examples/gcp-cloud-run/package.json b/examples/gcp-cloud-run/package.json new file mode 100644 index 0000000000..83cd25994e --- /dev/null +++ b/examples/gcp-cloud-run/package.json @@ -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" + } +} diff --git a/examples/gcp-cloud-run/src/index.js b/examples/gcp-cloud-run/src/index.js new file mode 100644 index 0000000000..aef3ccdb13 --- /dev/null +++ b/examples/gcp-cloud-run/src/index.js @@ -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}`, + ) +}) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ce461b7a83..c8cbb85e4f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -633,6 +633,15 @@ importers: specifier: 5.0.4 version: 5.0.4 + examples/gcp-cloud-run: + dependencies: + graphql: + specifier: 16.6.0 + version: 16.6.0 + graphql-yoga: + specifier: workspace:^3.9.1 + version: link:../../packages/graphql-yoga + examples/generic-auth: dependencies: '@envelop/generic-auth': @@ -3014,6 +3023,7 @@ packages: '@aws-cdk/core': 1.200.0 '@aws-cdk/custom-resources': 1.200.0(@aws-cdk/aws-ec2@1.200.0)(@aws-cdk/aws-iam@1.200.0)(@aws-cdk/aws-lambda@1.200.0)(@aws-cdk/aws-logs@1.200.0)(@aws-cdk/aws-s3@1.200.0)(@aws-cdk/core@1.200.0)(@aws-cdk/cx-api@1.200.0)(constructs@3.4.127) constructs: 3.4.127 + punycode: 2.3.0 transitivePeerDependencies: - '@aws-cdk/aws-ec2' - '@aws-cdk/aws-logs' @@ -3319,6 +3329,9 @@ packages: /@aws-cdk/cloud-assembly-schema@1.200.0: resolution: {integrity: sha512-sgMBMWQR2boMPZa8h92cFf6VmPUAPOhMfrrABZqAU5LDSpfl8JGE6jQ8nKngKIQKlFPjLQfDG7/SOkIdR3xkJg==} engines: {node: '>= 14.15.0'} + dependencies: + jsonschema: 1.4.1 + semver: 7.3.8 dev: false bundledDependencies: - jsonschema @@ -3384,6 +3397,7 @@ packages: engines: {node: '>= 14.15.0'} dependencies: '@aws-cdk/cloud-assembly-schema': 1.200.0 + semver: 7.3.8 dev: false bundledDependencies: - semver @@ -22772,7 +22786,6 @@ packages: /jsonschema@1.4.1: resolution: {integrity: sha512-S6cATIPVv1z0IlxdN+zUk5EPjkGCdnhN4wVSBlvoUO1tOLJootbo9CquNJmbIh4yikWHiUedhRYrNPn1arpEmQ==} - dev: true /jsonwebtoken@8.5.1: resolution: {integrity: sha512-XjwVfRS6jTMsqYs0EsuJ4LGxXV14zQybNd4L2r0UvbVnSF9Af8x7p5MzbJ90Ioz/9TI41/hTCvznF/loiSzn8w==} diff --git a/website/src/pages/docs/integrations/integration-with-gcp-cloud-run.mdx b/website/src/pages/docs/integrations/integration-with-gcp-cloud-run.mdx new file mode 100644 index 0000000000..74a7df5bd7 --- /dev/null +++ b/website/src/pages/docs/integrations/integration-with-gcp-cloud-run.mdx @@ -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. + + + + + 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. + +```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 . +``` + + + 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](https://github.com/dotansimha/graphql-yoga/tree/v3/examples/gcp-cloud-run)