GraphQL Yoga v3 documentation (superseded by v5): AWS Lambda is a serverless computing platform that makes it easy to build applications that run on the AWS cloud. GraphQL Yoga is platform agnostic so they can fit together easily.
AWS Lambda is a serverless computing platform that makes it easy to build applications that run on
the AWS cloud. GraphQL Yoga is platform agnostic so they can fit together easily.
Installation
npm i graphql
yarn add graphql
pnpm add graphql
bun add graphql
npm i aws-lambda
yarn add aws-lambda
pnpm add aws-lambda
bun add aws-lambda
npm i graphql-yoga
yarn add graphql-yoga
pnpm add graphql-yoga
bun add graphql-yoga
Example
graphql.ts
import { APIGatewayEvent, APIGatewayProxyResult, Context } from 'aws-lambda'import { createSchema, createYoga } from 'graphql-yoga'const yoga = createYoga<{ event: APIGatewayEvent lambdaContext: Context}>({ // You 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.queryStringParameters as 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.entries()) return { statusCode: response.status, headers: responseHeaders, body: await response.text(), isBase64Encoded: false }}
You can also check a full example on our GitHub repository
here.
This site uses cookies for analytics and improving your experience.