Integration with Cloudflare Workers#
GraphQL Yoga provides you a cross-platform GraphQL Server. So you can easily integrate it into any platform besides Node.js.
Cloudflare Workers provides a serverless execution environment that allows you to create entirely new applications or augment existing ones without configuring or maintaining infrastructure.
But instead of graphql-yoga
npm package, we will use @graphql-yoga/common
which has an agnostic HTTP handler using Fetch API's Request
and Response
objects.
Installation#
yarn add graphql
yarn add @graphql-yoga/common
pnpm add graphql
pnpm add @graphql-yoga/common
npm install graphql
npm install @graphql-yoga/common
Example with regular fetch
event listener#
listener.mjs
import { createServer } from '@graphql-yoga/common'
const server = createServer()
server.start()
You can also check a full example on our GitHub repository here
Example with Modules Approach#
modules.mjs
import { createServer } from '@graphql-yoga/common'
const yoga = createServer()
export default {
fetch: yoga.fetch,
}
You can also check a full example on our GitHub repository here
Enabling Subscriptions#
In order to enable Server Sent Events based subscriptions with Cloudflare Workers, you should add a compatibility flag in your wrangler configuration file like below;
compatibility_flags = ["streams_enable_constructors"]
Debug Logging#
You should expose DEBUG
variable in your environment to enable more verbose logging from GraphQL Yoga application.