Quick start
GraphQL Yoga is a batteries-included cross-platform GraphQL over HTTP spec-compliant GraphQL server powered by Envelop and GraphQL Tools that runs anywhere; focused on easy setup, performance and great developer experience.
Installation Permalink for this section
npm i graphql-yoga graphql
pnpm add graphql-yoga graphql
yarn add graphql-yoga graphql
bun add graphql-yoga graphql
Schema Permalink for this section
You will need to provide a schema to Yoga, there are many ways to assemble a GraphQL schema:
- graphql-yoga
- @graphql-tools/schema
- PothosGraphQL
- Nexus
- gqtx
- graphql-js
Use the createSchema function included in Yoga. It actually reuses the makeExecutableSchema from @graphql-tools/schema.
import { createSchema } from 'graphql-yoga'
export const schema = createSchema({
typeDefs: /* GraphQL */ `
type Query {
hello: String
}
`,
resolvers: {
Query: {
hello: () => 'world'
}
}
})
Tip
Get the best developer experience! GraphQL Code Generator and its server preset can help you build type-safe and scalable GraphQL servers. Check out GraphQL Code Generator, then read how to set up GraphQL server with server preset.
npm i @graphql-tools/schema
pnpm add @graphql-tools/schema
yarn add @graphql-tools/schema
bun add @graphql-tools/schema
import { makeExecutableSchema } from '@graphql-tools/schema'
export const schema = makeExecutableSchema({
typeDefs: /* GraphQL */ `
type Query {
hello: String
}
`,
resolvers: {
Query: {
hello: () => 'world'
}
}
})
Tip
npm i @pothos/core
pnpm add @pothos/core
yarn add @pothos/core
bun add @pothos/core
import SchemaBuilder from '@pothos/core'
const builder = new SchemaBuilder({})
builder.queryType({
fields: t => ({
hello: t.string({
resolve: () => 'world'
})
})
})
export const schema = builder.toSchema()
You can also create schemas with Nexus GraphQL:
npm i nexus
pnpm add nexus
yarn add nexus
bun add nexus
import { makeSchema, queryField } from 'nexus'
const helloField = queryField('hello', {
type: 'String',
resolve: () => 'world'
})
export const schema = makeSchema({ types: [helloField] })
Using gqtx:
npm i gqtx
pnpm add gqtx
yarn add gqtx
bun add gqtx
import { buildGraphQLSchema, Gql } from 'gqtx'
const Query = Gql.Query({
fields: () => [
Gql.Field({
name: 'hello',
type: Gql.String,
resolve: () => 'world'
})
]
})
export const schema = buildGraphQLSchema({ query: Query })
graphql-js is a reference implementation of GraphQL for JavaScript:
import { GraphQLObjectType, GraphQLSchema, GraphQLString } from 'graphql'
export const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: {
hello: {
type: GraphQLString,
resolve: () => 'world'
}
}
})
})
Server Permalink for this section
After you have created a GraphQL schema, simply pass it into Yoga and liftoff! 🚀
import { createServer } from 'node:http'
import { createYoga } from 'graphql-yoga'
import { schema } from './schema.js'
// Create a Yoga instance with a GraphQL schema.
const yoga = createYoga({ schema })
// Pass it into a server to hook into request handlers.
const server = createServer(yoga)
// Start the server and you're done!
server.listen(4000, () => {
console.info('Server is running on http://localhost:4000/graphql')
})
Visit http://localhost:4000/graphql to see Yoga in action.