• v3
  • Quick Start

GraphQL Yoga is a batteries-included cross-platform GraphQL over HTTP spec-compliant GraphQL Server using Envelop, and GraphQL Tools that runs anywhere.

Installation

yarn add graphql graphql-yoga

Usage

You will need to provide schema to Yoga, either by an existing executable schema, or by providing your type definitions and resolver map.

import { createYoga, createSchema } from 'graphql-yoga'
import { createServer } from 'http'
 
const yoga = createYoga({
  schema: createSchema({
    typeDefs: /* GraphQL */ `
      type Query {
        hello: String
      }
    `,
    resolvers: {
      Query: {
        hello: () => 'Hello from Yoga!',
      },
    },
  }),
})
 
const server = createServer(yoga)
server.listen(4000)

Yoga uses GraphQL Tools under the hood so you'll want to follow the makeExecutableSchema pattern.

That is it!

Now visit http://localhost:4000/graphql to execute the followiing query operation:

query HelloWorld {
  hello
}

Using existing schema

You can also pass an existing GraphQLSchema instance to createServer.

If you're using a library such as Pothos, GraphQL Nexus, gqtx, or vanilla graphql-js, you simply can pass the GraphQLSchema to schema:

import { createYoga } from 'graphql-yoga'
import { createServer } from 'http'
 
import { schema } from './schema'
 
const yoga = createYoga({
  schema,
})
 
const server = createServer(yoga)
 
server.listen(4000)
Last updated on September 20, 2022