← Docs
Recipe

GraphQL schema + resolver scaffold

Define a typed schema, auto-generate resolver stubs, and wire everything into a single Apollo Server endpoint. No boilerplate drift.

1. Schema definition

type Query {
  user(id: ID!): User
  posts(limit: Int): [Post!]!
}

type User {
  id: ID!
  email: String!
  name: String
}

type Post {
  id: ID!
  title: String!
  body: String!
  author: User!
}

2. Resolver scaffold

const resolvers = {
  Query: {
    user: (_, { id }, ctx) => ctx.db.user.findUnique({ where: { id } }),
    posts: (_, { limit }, ctx) => ctx.db.post.findMany({ take: limit ?? 20 }),
  },
  Post: {
    author: (parent, _, ctx) => ctx.db.user.findUnique({ where: { id: parent.authorId } }),
  },
};

3. Server bootstrap

import { ApolloServer } from "@apollo/server";
import { startStandaloneServer } from "@apollo/server/standalone";

const server = new ApolloServer({ typeDefs, resolvers });
const { url } = await startStandaloneServer(server, {
  context: async () => ({ db: prisma }),
  listen: { port: 4000 },
});
console.log(`Ready at ${url}`);

Tip: Keep resolvers thin. Push authorization, filtering, and pagination into the context or a dedicated service layer so the resolver only orchestrates data flow.