Extending the Unified Schema
GraphQL Mesh v0 documentation (superseded by v1): Learn how to extend and shape the Unified Schema in GraphQL Mesh using custom resolvers implemented programmatically, with fully typed resolvers map and SDK to fetch data from sources.
We saw on the “How to: Combine multiple Sources”
guide that additionalResolvers could be used to shape and augment the Unified Schema with custom
resolvers.
However, the additionalResolvers value can also be the path to a JavaScript/TypeScript file that
exports the custom resolvers implementation.
Programmatic additionalResolvers
In our “How to: Combine multiple Sources” Gateway,
the additionalResolvers could have been provided programmatically as shown in the
multiple-sources-prog-resolvers
example.
The below .meshrc.yaml configuration add the following fields:
Store.bookSells: [Sells!]!: to get the selling from a given storeSells.book: Book: to get the book of a given store selling recordBook.author: authors_v1_Author: to get the author of a book
Mesh relies on GraphQL Code Generator to generate the
Resolvers type that gives you access to:
- fully typed resolvers map
- fully typed SDK (through the
context) to fetch data from Sources
Using the SDK to fetch Sources
Let’s take a closer look at the Book.author resolver implementation.
The resolver is accessing the “Authors” source SDK through the context to fetch data from the
authors_v1_AuthorsService_GetAuthor(id: ID!) Query as follows:
authors_v1_AuthorsService_GetAuthor is a generated SDK method that allows to query our gRPC Books
Source as it was a GraphQL Schema.
Any SDK method take the following arguments:
root,contextandinfoare mandatory parameters that we forward from the resolvers calling the method. If the current field’s return type doesn’t match, you should provide aselectionSet.args: arguments to pass to the Mutation or QueryselectionSet: allows to specify a selection on the Query/Mutation to fetch nested data like{ id name }valuesFromResults: allows to provide a function to transform the results (if the result is an array, the function will be mapped to each element)dataLoaderOptions: allows you to define the batching behaviour as defined by dataloader. This allows e.g. to define that retrievals from the subgraph should be done in batches of n ids, which might help in load balancing resource-intensive queries.
Going further
Transforms like “Hoist Field” also helps in shaping and extending the Unified Schema.