graphql-yoga
Installation
SKILL.md
GraphQL Yoga
Overview
GraphQL Yoga (v5) is a spec-compliant GraphQL server built on the WHATWG Fetch API, so the same yoga object works as a Node http handler, a Fetch handler (yoga.fetch) in Workers, Bun and Deno, or behind Express, Fastify and Next.js. It is extended with Envelop plugins. It ships with GraphQL over SSE subscriptions, multipart file uploads, CORS, GraphiQL and error masking; you add caching, rate limiting and depth limits as plugins. Schemas can be written with createSchema (SDL plus resolvers) or with code-first builders such as Pothos.
Instructions
- Install:
npm install graphql-yoga graphql. - Build the schema with
createSchema({ typeDefs, resolvers }), pass it tocreateYoga, and hand the result tocreateServerfromnode:http. The endpoint defaults to/graphql; change it withgraphqlEndpoint. - Per-request state (database handle, current user) goes in the
contextoption, which receives{ request }(a FetchRequest) plus runtime-specific fields such asreq/reson Node. - Subscriptions: use
createPubSub()exported bygraphql-yoga;pubSub.publish(topic, payload)andpubSub.subscribe(topic)(an async iterable). Clients connect over SSE by default; no WebSocket server is needed. For several instances, back the pub/sub with Redis using@graphql-yoga/redis-event-target. - File uploads: declare
scalar Filein the schema and acceptFile!arguments. The value is a standardFile/Blob(.text(),.arrayBuffer(),.stream()). Multipart parsing is on by default;multipart: falseturns it off. - Errors: Yoga masks unexpected errors by default. Throw
GraphQLError(fromgraphql) withextensions.codefor errors clients should see.maskedErrors: falsedisables masking (development only). WithNODE_ENV=developmentthe original error appears inextensions.originalError. - GraphiQL is enabled only in development by default; set
graphiql: falseor a function to control it. - Plugins go in
plugins: []. Packages verified to exist:@graphql-yoga/plugin-response-cacheexportsuseResponseCache({ session, ttl, invalidateViaMutation });ttlis milliseconds,sessionreturns a string per user ornullfor a global cache.@envelop/rate-limiterexportsuseRateLimiter({ identifyFn }); limits are declared in the schema with the@rateLimit(max: 10, window: "5s")directive (you must define the directive in your SDL).@envelop/depth-limitexportsuseDepthLimit({ maxDepth }).@escape.tech/graphql-armor-max-depthis an alternative.