You can use GraphQL Mesh as a completely type-safe SDK in your existing TypeScript project.
Instead of using GraphQL operations as string with
execute - you can use GraphQL Mesh and generate a ready-to-use TypeScript SDK to fetch your data. It will make sure to have type-safety and auto-complete for variables and returned data.
Create your own GraphQL operations in a
.graphql file for your SDK, for example:
In this case we only have
fieldB in our result so those are not added to the response object anymore.
Now, point to those operations in your
You can run
mesh dev to try your operations in the playground;
Mesh can generate the operations for you if you don't want to use manually written operations. Just add the following to your configuration;
This will generate an SDK inside your Mesh artifacts under
Now, instead of using
execute manually, you can use the generated
getSdk method with your a GraphQL Mesh client, and use the functions that are generated based on your operations:
You can find an example for that here