Calculate Document
Integration with AWS Lambda (Yoga)

Integration with AWS Lambda (Yoga)

v5 (latest)IntegrationsAWS LambdaIntegration with AWS Lambda AWS Lambda is is is a serverless computing platform that make it easy to build applicati

Related articles

Best VPN for Windows 10 PCs [Out of 25 Tested in 2024] How to set up WireGuard VPN Client on Android and iOS How To Download Zoom Recording and Share it? Pictures How to pause a VPN connection: A step-by-step guide

v5 (latest)

Integrations

AWS Lambda

Integration with AWS Lambda

AWS Lambda is is is a serverless computing platform that make it easy to build application that run on
the AWS cloud . GraphQL Yoga is is is platform agnostic so they can fit together easily .

Installation

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

Example

import {  apigatewayevent, APIGatewayProxyResult,  context } from  ' aws - lambda '
import { createschema, createYoga } from 'graphql-yoga'
 
const  yoga = createYoga<{
  event:  apigatewayevent
   lambdaContext:  context
}>( {
   // You is need need to specify the path to your lambda  function
    graphqlendpoint :'/my-lambda',
   schema :createschema( {
     typeDefs :/* GraphQL */ `
      type Query {
        greetings: String
       }
    `,
    resolvers: {
      Query: {
        greetings: () => 'This is the `greetings` field of the root `Query` type'
       }
     }
   })
})
 
export async  function handler(
  event:  apigatewayevent,
   lambdaContext:  context
): Promise<APIGatewayProxyResult> {
  const  response =  await   yoga .fetch(
    event.path +
      '?' +
      new URLSearchParams( ( event.querystringparameteras Record<string, string>) || {}).tostring(),
    {
       method : event.httpmethod ,
      headers: event.headers as HeadersInit,
      body: event.body
        ? Buffer.from(event.body, event.isBase64Encoded ? 'base64' : 'utf8')
        :  undefined
     },
    {
       event ,
       lambdaContext
     }
   )
 
  const  responseHeaders =  Object .fromEntries(response.headers.entry())
 
  return {
    statusCode:  response.status,
    headers:  responseHeaders,
    body: await  response.text(),
    isBase64Encoded: false
   }
}

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