Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

GraphQL · Function

buildSchema

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.

How it works

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.

Syntax

buildSchema(source: string): GraphQLSchema

Parameters

NameTypeDescription
sourcestringA GraphQL SDL document containing the schema definition.

Returns

A GraphQL schema representing the types and fields declared in the SDL.

Key points

  • The SDL supplied to buildSchema describes the data shape and capabilities exposed by the API.
  • A schema covers the complete set of queryable types for the service, not just one operation.
  • buildSchema is useful when you prefer a readable, language-agnostic schema declaration.
  • Object types declare fetchable entities and the fields available on them.
  • SDL can include descriptions that make the generated schema easier for clients and developers to understand.
  • A schema built from SDL can contain the six named type categories defined by GraphQL.

Common mistakes

  • The SDL passed to buildSchema describes the type system, but it does not by itself provide application data or resolver behavior.
  • A built schema is used for validation and execution; creating it does not execute a query.

Example: Build a schema from SDL

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.

When to use it

Schema-first API design

Define a service contract in a readable, language-agnostic SDL file before implementing its data resolvers.

Query validation and execution

Create the schema object used to validate incoming operations and execute them.

Self-documenting APIs

Expose type, field, and argument descriptions for client tooling and schema exploration.

Interview questions

QWhat does 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.

QWhat role does SDL play when using 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.

QWhy is a schema required in a GraphQL service?

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 schema
  • Schema Definition Language (SDL)
  • Object types
  • Scalar types
  • Enum types
  • Interface types
  • Union types
  • Input Object types

Source: Schemas and Types. Checked 2026-10-15.