Cloud Functions is a serverless execution environment for building and connecting cloud services.
With Cloud Functions, you write simple, single-purpose functions that are attached to events, such
as an HTTP request.
It is probably the most straight forward way to deploy a Yoga server to GCP.
Installation
npm i @google-cloud/functions-framework graphql-yoga graphql
Create a GraphQL server with your schema. You can use any HTTP server; here we will use Node’s HTTP
implementation.
import { createServer } from 'node:http'import { createSchema, createYoga } from 'graphql-yoga'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) || 4000server.listen(port, () => { console.info(`Server is running on http://localhost:${port}${yoga.graphqlEndpoint}`)})
You can now deploy to Cloud Run. You can use all default values, except the last one, which allows
unauthenticated access to your service.
$ gcloud run deploy --source .
You can now access your API using the URL provided by gcloud. The default GraphQL endpoint is
/graphql.
If you need to use TypeScript or any other tool that requires a build phase, such as code
generation, add a Dockerfile to the root of your project so that Cloud Run can build a custom image
for you.
You can also check a full example in our GitHub repository
here
This site uses cookies for analytics and improving your experience.