GraphQL Mesh is able to merge different data sources into a single unified GraphQL Schema, and GraphQL Mesh is not an alternative to Schema Stitching, Apollo Federation, Bare Schema Merging or another merging strategy. GraphQL Mesh can consume and merge your data sources in different approaches.
In addition to
@apollo/gateway, GraphQL Mesh supports subscriptions out-of-box.
Extending GraphQL Schema with
You can add new types and/or fields to the current unified GraphQL Schema by using
additionalTypeDefs configuration field;
Let's say we have
Wikipedia API in our Mesh configuration;
And here we add a new field under
Query root type named
viewsInPastMonth. But we need a resolver for this new field.
Declare a resolver to the new
additionalTypeDefs by using
additionalResolvers field to make our new field executable in the unified schema;
additionalResolvers programmatically from a code source#
You can add custom resolvers and custom GraphQL schema SDL, and use the API SDK to fetch the data and manipulate it. So the query above could be simplified with custom logic.
This is possible because GraphQL Mesh will make sure to expose all available services in each API in your
It's named the same as the API name, so to access the API of
Wiki source, you can use
context.Wiki.Query and use the methods you need. It's useful when you need add custom behaviours, fields and types, and also for linking types between schemas.
In the following example, we will add take the query we had in the previous example, and simplify it by adding a new root operation to
Query type, and automate the variables that it needs, in order to create a simpler version of it for the consumers.
To add a new simple field, that just returns the amount of views for the past month, you can wrap it as following in your GraphQL config file, and add custom resolvers file using
Now, we need to implement
src/mesh/additional-resolvers.js with code that fetches and manipulate the data:
mesh dev and you'll be able to see your new field as part of your GraphQL schema, and you'll be able to query for it.
And now we run the the following GraphQL query to fetch the simplified data:
You can find the complete example here
You can use TypeScript to have full type-safety in additional resolvers. See TypeScript Support section to learn more.
We learnt that we can combine multiple APIs in Mesh using
The following example has two different OpenAPI sources; we add two new fields to a type of
Cities, and those fields have return types from
But this time we don't use an extra resolvers file for
additionalResolvers but only the configuration file.
The declaration above equals to the following;
Also checkout Postgres GeoDB example example that combines GitHub API and a Postgres DB sources.
Let's say you have two different services;
Authors. And those two are exposing the following schemas at the end;
And you renamed
then you expect following query works fine;
But it won't work because Mesh doesn't know which field belongs to where and how to combing those. For sure, you could add
additionalResolvers then extract
AuthorWithBooks then return it as
books field of
Author type but this sounds a little bit overhead. So let's try Type Merging here;
We have Type Merging transform to teach Mesh how to fetch entities from different sources;
Then now our query will work as expected!
The example above works fine but there is an N+1 problem. It sends
n requests for
n entities. But we have
books. Type Merging is smart enough to handle batching if you point it to a field that returns a list of entities. Let's update our configuration for this;
And now it batches the requests to the inner sources.
In the current example, we want to have a field called
Book property then point it to
Normally we supposed to do the following definitions;
But we want to solve N+1 problem;
And that's it. Now GraphQL Mesh will batch the queries of
Book.author by using
authorId field into
GraphQL Mesh uses the approach of Schema Stitching in order to consume the existing Apollo Federation services inside GraphQL Mesh. So you can combine Federation and Type Merging in GraphQL Mesh
You can follow Apollo Federation spec and integrate your existing Federated services into GraphQL Mesh.
GraphQL Mesh is smart enough to mix and match Federation and Stitching approaches including all other transforms (Type Merging, Rename, Filter etc)
You can also transform your existing non-federated schemas into federated service.
You can check out documentation of federation transformer to learn more about adding federation metadata to a non-federated GraphQL Schema.