GraphQL Yoga
GraphQL Yoga (opens in a new tab) is a batteries-included cross-platform GraphQL over HTTP spec-compliant (opens in a new tab) GraphQL server powered by Envelop (opens in a new tab) and GraphQL Tools (opens in a new tab) that runs anywhere; focused on easy setup, performance and great developer experience.
Installation
npm i graphql graphql-yoga
Start
Provide the database GraphQL schema.
examples/server/graphql-yoga/index.ts
import { createServer } from 'node:http';
import { createYoga } from 'graphql-yoga';
import { sessionIdFromCookie, sessionIdToCookie } from '@server/common';
import { buildSchema, createContext } from '@database/mongodb/schema';
const SESSION_REQUEST_TO_ID_MAP = new WeakMap<Request, string>();
const yoga = createYoga({
schema: await buildSchema(),
context: ({ request }) =>
createContext({
sessionId: sessionIdFromCookie(request.headers.get('cookie')),
setSessionId(sessionId) {
SESSION_REQUEST_TO_ID_MAP.set(request, sessionId);
},
}),
plugins: [
{
onResponse({ request, response }) {
const sessionId = SESSION_REQUEST_TO_ID_MAP.get(request);
if (sessionId) {
response.headers.set('set-cookie', sessionIdToCookie(sessionId));
}
},
},
],
});
const server = createServer(yoga);
server.listen(50005, () => {
console.info('Server is running on http://localhost:50005/graphql');
});
And run it:
npx tsx index.ts
Visit http://localhost:50005/graphql
(opens in a new tab) to see Yoga in action.