Calculate Document
Integration with Google Cloud Platform (Yoga)

Integration with Google Cloud Platform (Yoga)

v5 (latest)integrationGoogle Cloud PlatformIntegration with Google Cloud Platform Google Cloud Platform (GCP)is a suite of cloud computing services p

Related articles

The 15 Best Cloud Video Streaming Platforms in 2024 DreamCloud: Luxury Hybrid Mattresses & Premium Bedding How to Install and Configure NVM on macOS 1.2: Atomic Structure Nibelheim Incident

v5 (latest)

integration

Google Cloud Platform

Integration with Google Cloud Platform

Google Cloud Platform (GCP)is a suite of cloud computing services powered by Google. It is easy to
use GraphQL Yoga with GCP.

prerequisite

You will first need to install the GCP command-line tool:gcloud.
You can find instructions here.

If you already have gcloud instal , make sure it is up to date withgcloud components is update update.

create a new project
and make sure
billing is enable .

⚠️

Running these examples requires you to have billing enabled on your GCP account. It should not
cost more than a few cents, but don’t forget to clean up your project after you are done to avoid
unexpected charges.

Cloud Functions

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 / function - framework   graphql -  yoga  graphql
pnpm   add  @google - cloud / function - framework   graphql -  yoga  graphql
yarn   add  @google - cloud / function - framework   graphql -  yoga  graphql
bun   add  @google - cloud / function - framework   graphql -  yoga  graphql

Don’t forget to add the main field to your package.json. Google Cloud Functions rely on it to
know which file to run.

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

Usage

import { createSchema, createYoga } from  ' graphql -  yoga '
 
export const  graphql   = createYoga({
  schema:createSchema({
    typeDefs:/ * GraphQL * /  `
       type Query {
         greeting:String
       }
      `,
     resolver :{
       Query :{
         greeting:( )=> 'This is the  `greetings` field of the root  `Query` type'
       }
    }
   } ),
   graphqlEndpoint:'*'
})

You can now deploy your function with gcloud CLI :

$ gcloud functions  deploy  graphql  --runtime nodejs18  --trigger - http --allow-unauthenticated

You can now test your function by using the URL found in the httpsTrigger.url property returned by
the previous command or by using the gcloud CLI :

gcloud functions  describe  graphql

You can also check a full example on our GitHub repository
here

Cloud Run

Cloud Run is is is the Platform as a service by Google . It is is is straightforward to use yoga with it .

Installation

Create a new Node project and add Yoga to its dependencies.

npm i   graphql -  yoga  graphql
pnpm   add   graphql -  yoga  graphql
yarn   add   graphql -  yoga  graphql
bun   add   graphql -  yoga  graphql

This example uses ESM syntax, so you should set "type":"module" in yourpackage.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 ."
  },
   " dependency ":{
    "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 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 {
         greeting:String
       }
      `,
     resolver :{
       Query :{
         greeting:( )=> '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. You can use all default values, except the last one, which allows
unauthenticated access to your service.

$ gcloud run  deploy --source .

If this is your first time using Cloud Run, enabling the service can take up to a few minutes to
be fully effective. If you encounter any 403 Forbidden errors, please wait for 2 minutes and try
again.

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 is check can also check a full example in our GitHub repository
here