Schema-first API design
Define a service contract in a readable, language-agnostic SDL file before implementing its data resolvers.
GraphQL · Function
buildSchema builds a GraphQL schema from Schema Definition Language (SDL). The resulting schema describes the types and fields that GraphQL operations can validate and execute against.
buildSchema converts a GraphQL schema written in SDL into a GraphQLSchema object that can validate and execute operations. Root fields can resolve through the supplied root value; more advanced resolver behavior generally uses explicit schema construction APIs.
buildSchema(source: string): GraphQLSchema| Name | Type | Description |
|---|---|---|
source | string | A GraphQL SDL document containing the schema definition. |
A GraphQL schema representing the types and fields declared in the SDL.
buildSchema describes the data shape and capabilities exposed by the API.buildSchema is useful when you prefer a readable, language-agnostic schema declaration.buildSchema describes the type system, but it does not by itself provide application data or resolver behavior.import { buildSchema, graphql } from "graphql";
const schema = buildSchema(`
type Query {
greeting: String!
}
`);
const rootValue = { greeting: () => "Hello, GraphQL!" };
const result = await graphql({
schema,
source: "{ greeting }",
rootValue,
});
console.log(result.data?.greeting);Builds an executable schema from SDL, supplies a resolver through the root value, and executes a query against it.
Define a service contract in a readable, language-agnostic SDL file before implementing its data resolvers.
Create the schema object used to validate incoming operations and execute them.
Expose type, field, and argument descriptions for client tooling and schema exploration.
buildSchema create, and what is the resulting object used for?buildSchema takes SDL and produces a schema object representing the service’s supported types and fields. GraphQL then validates and executes incoming operations against that schema.
Follow-up: Why might a team use explicit schema construction instead? It can keep resolver functions and schema types together, rather than defining the type system separately in SDL.
buildSchema?SDL is a language-agnostic way to describe GraphQL types and fields. The resulting schema defines the capabilities that clients can query, while resolver logic supplies field values.
Follow-up: How can a schema be documented? GraphQL supports descriptions on types, fields, and arguments, including Markdown-formatted descriptions.
A GraphQL schema describes the possible data available from a service, including its types and fields. Requests are checked against that schema before execution, which makes results predictable.
Follow-up: What other schema-building approaches exist? Libraries may construct types in code, infer schemas from resolver annotations, or infer both schema and resolvers from data sources.
GraphQL schemaSchema Definition Language (SDL)Object typesScalar typesEnum typesInterface typesUnion typesInput Object types