Create a schema for a GraphQL server
Server construction
GraphQL · Function
makeExecutableSchema combines GraphQL type definitions with resolvers to produce an executable schema. The result can be passed to a graphql-js-based server.
makeExecutableSchema combines GraphQL SDL and resolver maps into an executable schema. The resulting schema can be passed to a graphql-js server or used directly to execute operations.
makeExecutableSchema({ typeDefs, resolvers })| Name | Type | Description |
|---|---|---|
typeDefs | DocumentNode or SDL string | GraphQL schema definition language documents describing the types and fields. |
resolvers | Resolver map | Resolver functions and resolver maps implementing the schema fields. |
An executable GraphQL schema.
import { makeExecutableSchema } from "@graphql-tools/schema";
import { graphql } from "graphql";
const typeDefs = `
type Query { greeting: String! }
`;
const resolvers = {
Query: { greeting: () => "Hello, GraphQL!" },
};
const schema = makeExecutableSchema({ typeDefs, resolvers });
const result = await graphql({ schema, source: "{ greeting }" });
console.log(result.data);This builds a runnable schema from SDL type definitions and matching resolver functions, then executes a query against it.
Server construction
Local execution
Schema composition
makeExecutableSchema creates an executable GraphQL schema from type definitions and resolvers. The returned schema can be handed to a graphql-js-based server.
Follow-up: Why are both inputs needed? SDL describes the schema contract, while resolvers provide the runtime field behavior.
The function accepts an options object containing typeDefs and resolvers. The example on the official GraphQL site imports it from @graphql-tools/schema.
Follow-up: What would happen if a field had no resolver? GraphQL may use its default field resolver, but custom behavior must be supplied through the resolver map.
After construction, the executable schema is suitable for use by a GraphQL server. It can also be composed with middleware before serving requests.
Follow-up: Why apply middleware after creation? Middleware can wrap schema execution or field behavior without changing the SDL definition itself.
typeDefsresolversgraphql-jsapplyMiddleware