API Reference

GraphQL Yoga

Spec-compliant GraphQL server with a Fetch-first API, file uploads, subscriptions, and built-in GraphiQL for Node and edge runtimes.

Official Documentation ↗
CompatibilityThis documentation covers version v17.0.2. Check the official docs for the latest API surface.

Installation

npmnpm install graphql graphql-yoga
pnpmpnpm add graphql graphql-yoga
yarnyarn add graphql graphql-yoga

Methods

GraphQL Yoga is a GraphQL server built on the standard Fetch API. It accepts a schema, runs a spec-compliant execution pipeline, serves GraphiQL during development, and can be mounted on Node HTTP, serverless functions, or edge runtimes without changing the schema layer.

Overview

Yoga combines schema building, context creation, error masking, and plugins behind one instance. Schema-first development uses SDL plus resolver maps through createSchema, while code-first schemas from tools such as Pothos can be passed directly. File uploads, persisted operations, response caching, and subscriptions are available as plugins rather than core bloat.

Installation

Install graphql and graphql-yoga together, because graphql is a peer dependency. TypeScript projects get types from the package without extra @types. For Node hosting, import createServer from node:http; for serverless or edge platforms, export the yoga instance as the request handler. No code generation step is required for schema-first usage.

Core API walkthrough

createYoga builds the server from a schema and options such as graphqlEndpoint and plugins. createSchema turns SDL and a resolver map into an executable schema. context derives per-request data from headers or cookies and passes it to resolvers. yoga.fetch executes a request through WHATWG Fetch for tests and edge runtimes, and createServer(yoga) mounts the same instance on Node HTTP.

Pitfalls

Resolvers must not trust context values that were never validated; authentication belongs in context creation. GraphiQL left on in production reveals the schema. Error masking hides stack traces from clients but also hides useful validation detail, so log server-side. Watch the graphql version: two copies in one dependency tree cause schema type errors at runtime.

Verification

Send an introspection query and a named operation through curl or yoga.fetch, and confirm the response contains data rather than an errors array. Disable GraphiQL and verify the endpoint still answers POST requests. Add a resolver that throws to confirm masked errors reach the client while the full error is logged.

FAQ

Why do I need to install graphql separately?
graphql is a peer dependency that provides the reference implementation of the GraphQL specification. Yoga builds schemas and executes queries on top of it, so the version must be installed explicitly.
Should GraphiQL stay enabled in production?
GraphiQL is convenient during development but exposes schema exploration. Disable it in production with graphiql: false, or protect the endpoint behind authentication and keep introspection policy consistent.
How do I add logging or error masking?
Pass plugins such as useLogger() in the plugins array, and rely on maskedErrors to hide internal error messages from clients. Custom plugins can hook into parse, validate, and execute phases.