Executable Schemas
Generate a GraphQL schema from the concise type definition language.
The GraphQL-Tools package allows you to create a GraphQL.js GraphQLSchema instance from GraphQL
schema language using the function makeExecutableSchema.
Example
When using graphql-tools, you describe the schema as a GraphQL type language string:
Then you define resolvers as a nested object that maps type and field names to resolver functions:
Install the package
In the end, the schema and resolvers are combined using makeExecutableSchema:
This example has the entire type definition in one string and all resolvers in one object, but you can combine types and resolvers from multiple files, as documented in the extending types section below.
Extending Types
It’s easy to add additional fields to existing types using the extend keyword. Using extend is
particularly useful in avoiding a large list of fields on root Queries and Mutations. You can use it
like this:
If one of the types extended needs a resolver you can use makeExecutableSchema like this:
Learning the GraphQL Schema Language
The official documentation on graphql.org now has a section about GraphQL schemas which explains all of the different schema features and how to use them with the schema language.
The type definitions must define a query type, which means a minimal schema would look something like this:
Descriptions & Deprecations
GraphiQL has built-in support for displaying docstrings with markdown syntax. You can easily add docstrings to types, fields and arguments like below:
This GraphQL schema language cheat sheet by Hafiz Ismail is an excellent reference for all the features of the GraphQL schema language.
API
makeExecutableSchema
Takes a single argument: an object of options. Only the typeDefs option is required. It returns a
new schema, modified as specified.
-
typeDefsis a required argument and should be a GraphQL schema language string or an array of GraphQL schema language strings or a function that takes no arguments and returns an array of GraphQL schema language strings. The order of the strings in the array is not important, but it must include a schema definition. -
resolversis an optional argument (empty object by default) and should be an object or an array of objects that follow the pattern explained in article on resolvers -
parseOptionsis an optional argument that allows customization of parse when specifyingtypeDefsas a string. -
resolverValidationOptionsis an optional argument with the following properties, each of which can be set toerror,warn, orignore:-
requireResolversForArgswill causemakeExecutableSchemato throw an error (error) or issue a warning (warn)unless a resolver is defined for every field with arguments. The default isignore, causing this validator to be skipped. -
requireResolversForNonScalarrequire a resolver for every non-scalar field. Default isignore. -
requireResolversForAllFieldsasserts that all fields have valid resolvers. This option cannot be set in combination with the previous two validators. Default isignore. -
requireResolversForResolveTypewill require aresolveType()method for Interface and Union types. This can be passed in with the field resolvers as__resolveType(). Default isignore. -
requireResolversToMatchSchemarequires every resolver within the resolver map to correspond to a GraphQL entity within the schema. Defaults toerror, to help catch common errors.
-
-
inheritResolversFromInterfacesGraphQL Objects that implement interfaces will inherit missing resolvers from their interface types defined in theresolversobject. If this option is set totrueand you are using GraphQL Code Generator, you can enable addInterfaceFieldResolverTypes: true to ensure that the generated types correctly reflect this resolver inheritance behavior.