Skip to Content
SWR 2.0 is out! Read more →

Other Environments

If you have a different environment that you want to integrate with, you can implement your middleware/handler like the example below.

GraphQL Yoga understands WHATWG Request object to consume your HTTP request. So you need an adapter implementation between your environment and WHATWG standard objects.

import { createServer } from '@graphql-yoga/common' import { Request } from '@whatwg-node/fetch' // or fetch-like library const yoga = createServer<MyRandomCtx>() export async function myMiddleware(req: MyRandomRequest, ctx: MyRandomCtx) { // req.url is a full url here not a relative path const request = new Request(req.url, { method: req.method, headers: req.headers, body: req.body // req.body should be a valid BodyInit like an AsyncIterable, a ReadableStream, a Node.js Readable, a string or a Buffer etc... }) // Second parameter becomes your server context const response = await yoga.handleRequest(req, ctx) // response is a WHATWG `Response` object // Create a headers object for your middleware response const headersObj: any = {} response.headers.forEach((value, key) => { headersObj[key] = value }) // Let's say your environment needs to return something like the below; return { statusCode: response.status, body: response.body, // static responses will disable subscriptions // body: await response.text() If it accepts a string // body: await response.json() If it accepts a json // body: response.body if it accepts a ReadableStream or an AsyncIterable // body: Readable.from(response.body) if it accepts a Node.js Readable headers: headersObj // We assume that your environments accepts a regular JS object for response headers } }