Quick Start#
GraphQL Yoga is a batteries-included cross-platform GraphQL over HTTP spec-compliant GraphQL Server using Envelop and GraphQL Tools.
Installation#
yarn add graphql
yarn add @graphql-yoga/node
pnpm add graphql
pnpm add @graphql-yoga/node
npm install graphql
npm install @graphql-yoga/node
Create a Hello World GraphQL API#
The following example shows how to create a simple GraphQL Yoga server that responds to a simple query.
import { createServer } from '@graphql-yoga/node'
// Create your server
const server = createServer({})
// start the server and explore http://localhost:4000/graphql
server.start()
That is it!
Now visit http://localhost:4000/graphql
in your browser and execute the followiing query operation:
query HelloWorld {
hello
}
Provide your own GraphQL schema#
You probably want to write your own schema instead of using the hello world schema that is served by default!
import { createServer } from '@graphql-yoga/node'
// Create your server
const server = createServer({
schema: {
typeDefs: /* GraphQL */ `
type Query {
hello: String
}
`,
resolvers: {
Query: {
hello: () => 'Hello Hello Hello',
},
},
},
})
// start the server and explore http://localhost:4000/graphql
server.start()
You can simply provide type definitions and a resolver map based on the popular graphql-tools makeExecutableSchema
pattern. You can learn more on the GraphQL Tools documentation
Passing an executable schema created by other tools#
You can also pass an existing GraphQLSchema
instance to createServer
.
Use your favorite schema building library such as Pothos, gqtx, GraphQL Nexus or plain graphql-js schema classes.
import { schema } from './schema'
import { createServer } from '@graphql-yoga/node'
const server = createServer({
schema,
})
// start the server and explore http://localhost:4000/graphql
server.start()