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.api 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 has 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:
graphql-mesh serve 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.
You 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.