Type safe GraphQL server context

28 Feb 2022

overwrite: true
schema: "schema.graphql"
# documents: "**/*.graphql"
generates:
  types.ts:
    config:
      contextType: ./server#GraphQLContext
    plugins:
      - "typescript"
      - "typescript-operations"
      - "typescript-resolvers"
import { createServer, YogaInitialContext } from "@graphql-yoga/node";

import { Resolvers } from "./types";

export interface GraphQLContext extends YogaInitialContext {
  specialContextValue: number;
}

const resolvers: Resolvers = {
  Query: {
    hello: (_, __, { specialContextValue }) => `world ${specialContextValue}`,
  },
};

const server = createServer({
  context: ({ request }) => ({
    request,
    specialContextValue: 31,
  }),
});