Define an executable API contract
Build a GraphQL server
GraphQL · Class
GraphQLSchema represents the schema of a GraphQL service: the typed contract describing what data clients can query. Requests are validated and executed against this schema.
GraphQLSchema is the executable representation of a GraphQL service’s schema. It combines the supported type system with root operation types, allowing incoming operations to be checked and executed against the service definition.
new GraphQLSchema({ query, mutation?, subscription?, types?, directives? })A schema describing the GraphQL service’s supported types and queryable capabilities.
import { GraphQLObjectType, GraphQLSchema, GraphQLString } from "graphql";
const Query = new GraphQLObjectType({
name: "Query",
fields: {
greeting: { type: GraphQLString, resolve: () => "Hello, GraphQL!" },
},
});
const schema = new GraphQLSchema({ query: Query });
console.log(schema.getQueryType()?.name);Creates a minimal executable schema with a query root and one field, then reads the root type name.
Build a GraphQL server
Validate client operations
Generate developer tooling
A GraphQL schema is the contract describing the types and fields a service exposes. Incoming operations are checked against that contract and then executed to produce results.
Follow-up: What does the schema determine for an operation? It determines which types and fields are valid and where execution begins through the relevant root operation type.
Object types represent fetchable objects and define the fields available on each object. The broader type system also includes scalar, enum, interface, union, and input object types.
Follow-up: Why use interfaces or unions? They model relationships where a field may resolve to one of several object types, while preserving a typed contract.
The schema is used both for validation and execution. A request can only use fields and types defined by the service, and valid fields are then resolved to produce the response.
Follow-up: How can clients discover the contract? GraphQL’s introspection system exposes schema metadata so tooling and clients can inspect available types and fields.
GraphQL type systemObject typesScalar typesEnum typesInterface typesUnion typesInput object typesIntrospection