File: README.md
Quick start
Install
pnpm add graphql-yoga graphqlStart
Make a schema, create Yoga and start a Node server:
import { createServer } from 'node:http'
import { createSchema, createYoga } from 'graphql-yoga'
const yoga = createYoga({
schema: createSchema({
typeDefs: /* GraphQL */ `
type Query {
hello: String
}
`,
resolvers: {
Query: {
hello: () => 'Hello from Yoga!'
}
}
})
})
const server = createServer(yoga)
server.listen(4000, () => {
console.info('Server is running on http://localhost:4000/graphql')
})Overview
- Easiest way to run a GraphQL server: Sensible defaults & includes everything you need with
minimal setup (we also export a platform/env-agnostic handler so you can build your own wrappers
easily). - Includes Subscriptions: Built-in support for GraphQL subscriptions using
Server-Sent Events. - Compatible: Works with all GraphQL clients
(Apollo, Relay,
Urql...) and fits seamless in your GraphQL workflow. - WHATWG Fetch API: the core package depends on
WHATWG Fetch API so it can run and deploy on any environment
(Serverless, Workers, Deno, Node). - Easily Extendable: New GraphQL-Yoga support all
envelopplugins.
[Features](https://www.the-guild.dev/graphql/yoga-server/docs)
- Fully typed with TypeScript
- GraphQL over HTTP spec compliant
- GraphiQL included
- File uploads with GraphQL Multipart Request spec
- Subscriptions and realtime capabilities
- Automatic persisted queries
- Built-in parsing and validation caching
- Testing utilities
- Supports ESM
- Runs everywhere, including environments like:
- And more...
[Documentation](https://www.the-guild.dev/graphql/yoga-server/docs)
Our documentation website will help you get
started.
[Examples](https://github.com/graphql-hive/graphql-yoga/tree/main/examples)
We've made sure developers can quickly start with GraphQL Yoga by providing a comprehensive set of
examples.
See all of them in the examples/ folder.
[Comparison](https://www.the-guild.dev/graphql/yoga-server/docs/comparison)
Read more about how GraphQL Yoga compares to other servers in the ecosystem
here.
Contributing
If this is your first time contributing to this project, please do read our
Contributor Workflow Guide
before you get started off.
For this project in particular, to get started on stage/2-failing-test:
- Install Node.js
- Run in your terminal:
npm i pnpm@8 && pnpm install && pnpm build - Add tests to
packages/graphql-yoga/__tests__using Jest APIs - Run the tests with
pnpm test
Feel free to open issues and pull requests. We're always welcome support from the community.
Code of Conduct
Help us keep Yoga open and inclusive. Please read and follow our
Code of Conduct as adopted
from Contributor Covenant.
License
MIT
File: e2e/README.md
Yoga e2e testing
This project contains everything related to e2e testing for GraphQL-Yoga.
In order to ensure that GraphQL-Yoga is compatible with all popular runtimes.
To do that, we are using Pulumi (infrastructure-as-code) (with
TypeScript, and using Automation API) to
provision a real resource in every env, run smoke tests (GET -> GraphiQL,POST -> Execute GraphQL), and then destroy and remove all resources.
On each PR, this workflow runs, and tried to deploy and test an actual environment. This way we can
find issues like compatibility issues / runtime issues during code reviews.
Tested Enrivonments
- CloudFlare Worker
- Azure Function
- AWS Lambda
- Docker container
- Vercel Function
- K8s Pod
- Docker
Notes
- This check runs on every PR created to GraphQL-Yoga repo.
- We are using the locally built version of Yoga - so all functions are deployed as built artifact
(usingesbuild).
File: examples/sveltekit/README.md
GraphQL Yoga - SvelteKit
File: examples/subscriptions/README.md
subscriptions
This directory contains a simple GraphQL subscriptions example based on graphql-yoga.
Get started
Clone the repository:
git clone https://github.com/graphcool/graphql-yoga.git
cd graphql-yoga/examples/subscriptionsInstall dependencies and run the app:
pnpm install # or npm install
pnpm start # or npm startTesting
Counter
Open your browser at http://localhost:4000.
Paste the following subscription in the editor (left side) of GraphiQL:
subscription {
counter
}Press the Play (Execute Query) button.
The counter will increment every second and the corresponding data is received in the Playground:
{
"data": {
"counter": 1,
}
}
// ... 1 seconds
{
"data": {
"counter": 2,
}
}
// ... 2 seconds
{
"data": {
"counter": 3,
}
}
// ...Global counter
Open your browser at http://localhost:4000n.
Paste the following subscription in the editor (left side) of GraphiQL:
subscription {
globalCounter
}Press the Play (Execute Query) button.
You will receive this initial result:
{
"data": {
"globalCounter": 0
}
}Open another browser window and execute the following mutation:
mutation {
incrementGlobalCounter
}Press the Play (Execute Query) button.
You will receive this result:
{
"data": {
"incrementGlobalCounter": 1
}
}On the other window that is executing the subscription, you will receive a new result.
{
"data": {
"globalCounter": 1
}
}As you re-execute the mutation the global counter will be further incremented.
File: examples/service-worker/README.md
Yoga + CloudFlare Worker (bundle)
File: examples/redis-pub-sub/README.md
Redis Pub/Sub Example
Usage instructions
Start Redis with Docker
docker run -p "6379:6379" redis:7.0.2Start two server instances running on different ports
PORT=4000 pnpm --filter example-redis-pub-sub start
PORT=4001 pnpm --filter example-redis-pub-sub startVisit and set up the subscription by pressing the Play button.
http://127.0.0.1:4000/graphql?query=subscription+%7B%0A++message%0A%7DVisit and execute the mutation by pressing the Play button.
http://127.0.0.1:4001/graphql?query=mutation+%7B%0A++sendMessage%28message%3A+%22Yo+we+share+a+redis+instance.%22%29%0A%7DSee your subscription update appear on 127.0.0.1:4000, even though you executed the mutation on a
different Node.js server instance running on 127.0.0.1:4001.
The magic of Redis. ๐ชโจ
File: examples/nextjs-ws/README.md
Next.js WebSockets
Get Started
Run the custom server with:
npm start
# or
yarn start
# or
pnpm startNavigate to http://localhost:3000 to see the Next.js app running. You can
start editing the page by modifying pages/index.js. The page auto-updates as you edit files.
WebSockets cannot be used with
Next.js API Routes, we therefore have to create a
custom Next.js server that will serve the
GraphQL API, WebSockets and the rest of Next.js content.
This custom server implementation can be found in server.js.
Learn More
To learn more about Next.js, take a look at the following resources:
- Next.js Documentation - learn about Next.js features and API.
- Learn Next.js - an interactive Next.js tutorial.
- Next.js custom server - allows you to
start a server 100% programmatically in order to use custom server patterns
You can check out the Next.js GitHub repository - your
feedback and contributions are welcome!
No deployment on Vercel
Since this Next.js project uses a
custom server, it cannot be deployed to
Vercel.
File: examples/nextjs-legacy-pages/README.md
This is a Next.js project bootstrapped withcreate-next-app.
Getting Started
First, run the development server:
npm run dev
# or
pnpm devOpen http://localhost:3000 with your browser to see the result.
You can start editing the page by modifying pages/index.js. The page auto-updates as you edit the
file.
API routes can be accessed on
http://localhost:3000/api/hello. This endpoint can be edited inpages/api/hello.js.
The pages/api directory is mapped to /api/*. Files in this directory are treated as
API routes instead of React pages.
Learn More
To learn more about Next.js, take a look at the following resources:
- Next.js Documentation - learn about Next.js features and API.
- Learn Next.js - an interactive Next.js tutorial.
You can check out the Next.js GitHub repository - your
feedback and contributions are welcome!
Deploy on Vercel
The easiest way to deploy your Next.js app is to use the
Vercel Platform
from the creators of Next.js.
Check out our Next.js deployment documentation for more
details.
File: examples/nextjs-auth/README.md
This is a Next.js example using next-auth.js.
Before running this example you need to set upa GitHubOAuth App.
Follow the instructions here.
Use http://localhost:3000/ as the homepage URL.
Use http://localhost:3000/api/auth/callback/github as the authorization callback URL.
Afterwards, you must copy the .env.sample to .env (cp .env.sample .env) and fill in the
environment variables (GITHUB_ID and GITHUB_SECRET) you received from your GitHub OAuth
application.
Run the app with pnpm dev.
You can log in on http://localhost:3000.
Run the following operation on http://localhost:3000/api/graphql.
query Session {
session {
expires
user {
id
email
image
}
}
}This is a Next.js project bootstrapped withcreate-next-app.
Getting Started
First, run the development server:
pnpm devOpen http://localhost:3000 with your browser to see the result.
You can start editing the page by modifying pages/index.js. The page auto-updates as you edit the
file.
API routes can be accessed on
http://localhost:3000/api/hello. This endpoint can be edited inpages/api/hello.js.
The pages/api directory is mapped to /api/*. Files in this directory are treated as
API routes instead of React pages.
Learn More
To learn more about Next.js, take a look at the following resources:
- Next.js Documentation - learn about Next.js features and API.
- Learn Next.js - an interactive Next.js tutorial.
You can check out the Next.js GitHub repository - your
feedback and contributions are welcome!
Deploy on Vercel
The easiest way to deploy your Next.js app is to use the
Vercel Platform
from the creators of Next.js.
Check out our Next.js deployment documentation for more
details.
File: examples/nextjs-app/README.md
This is a Next.js project bootstrapped withcreate-next-app.
Getting Started
First, run the development server:
npm run dev
# or
yarn dev
# or
pnpm devOpen http://localhost:3000 with your browser to see the result.
You can start editing the page by modifying app/page.tsx. The page auto-updates as you edit the
file.
This project uses next/font to
automatically optimize and load Inter, a custom Google Font.
Learn More
To learn more about Next.js, take a look at the following resources:
- Next.js Documentation - learn about Next.js features and API.
- Learn Next.js - an interactive Next.js tutorial.
You can check out the Next.js GitHub repository - your
feedback and contributions are welcome!
Deploy on Vercel
The easiest way to deploy your Next.js app is to use the
Vercel Platform
from the creators of Next.js.
Check out our Next.js deployment documentation for more
details.