Skip to Content
Integrationgraphql-yoga

graphql-yoga

graphql-yoga  is a batteries-included GraphQL server that works in any JavaScript runtime.

Installation

npm install graphql-yoga
pnpm add graphql-yoga
yarn add graphql-yoga

Prerequisites

Create an executable schema following the Getting Started guide.

Basic Server

// src/server.ts import { createServer } from "node:http"; import { createYoga } from "graphql-yoga"; import { schema } from "./schema"; const yoga = createYoga({ schema }); const server = createServer(yoga); server.listen(4000, () => { console.log("Server is running on http://localhost:4000/graphql"); });

With Context

If your resolvers use a context type, first set up context and resolver factories, then provide a context factory to your server:

// src/server.ts import { createServer } from "node:http"; import { createYoga } from "graphql-yoga"; import { schema } from "./schema"; import type { Context } from "./gqlkit/context"; const yoga = createYoga<{}, Context>({ schema, context: async ({ request }) => { const user = await getUserFromRequest(request); return { currentUser: user, db: database, }; }, }); const server = createServer(yoga); server.listen(4000, () => { console.log("Server is running on http://localhost:4000/graphql"); });

Further Reading

Last updated on