GraphQL Schema Design
Defining a strongly-typed schema using GraphQL SDL that serves as the contract between client and server, specifying queries, mutations, and types.
Description
GraphQL schema design is the practice of defining the type system, queries, mutations, and subscriptions that form the API contract using Schema Definition Language (SDL). Unlike REST, where the server dictates the response shape, a GraphQL schema lets clients request exactly the fields they need. The schema acts as both documentation and runtime validation -- any query that doesn't conform to the schema is rejected before execution.
Effective schema design follows several principles: use domain-driven naming that reflects business concepts rather than database tables, prefer explicit types over generic JSON blobs, use interfaces and unions for polymorphism, and design input types separately from output types to control what clients can write versus read. Connection-based pagination (the Relay specification) is the standard pattern for list fields, using edges and nodes rather than simple arrays to allow attaching metadata to relationships.
Schema evolution in GraphQL differs from REST versioning. Because clients select specific fields, new fields can be added without breaking anyone. Fields are deprecated using the @deprecated directive with a reason string, and tooling (GraphiQL, Apollo Studio) surfaces these deprecations to consumers. The goal is to maintain a single evolving schema rather than multiple versioned schemas, which is one of GraphQL's key advantages over REST.
Prompt Snippet
Define the schema using SDL with domain-specific types (e.g., type Invoice, type LineItem) rather than mirroring database tables. Use the Relay connection specification for all list fields: type InvoiceConnection { edges: [InvoiceEdge!]!, pageInfo: PageInfo! }. Separate input types from output types (CreateInvoiceInput vs Invoice). Mark deprecated fields with @deprecated(reason: "Use invoiceV2 field instead") and track usage via Apollo Studio field-level metrics before removal. Enforce schema linting with graphql-eslint in CI.Tags
Related Terms
GraphQL Resolvers
Functions that populate each field in a GraphQL schema by fetching data from databases, services, or other sources.
GraphQL N+1 (DataLoader)
A batching and caching strategy using DataLoader to solve the N+1 query problem inherent in naive GraphQL resolver implementations.
API Documentation Generation
Automatically generating interactive API documentation from OpenAPI specifications or code annotations for developer consumption.