To get started with a simple Mesh, create a GraphQL Mesh configuration file -
.meshrc.yaml, under your project root, and fill in your sources, for example:
This demo will use the public API pf Wikipedia, which uses
openapi spec, so we'll need to make sure we have
@graphql-mesh/openapi handler installed as well:
Try your new API
GraphQL Mesh comes with a built in GraphiQL interface, so it means that you can try your newly-created GraphQL before writing any line of code. All you need to get started is the configuration file.
To test your new GraphQL API based on your API specs, you can run:
This will serve a GraphiQL interface with your schema, so you'll be able to test it right away, before intergrating it to your application, you can try to run a test query.
Open your browser in
http://localhost:4000 to start using it.
For example, this following will fetch all page views for Wikipedia.org on the past month:
You can give it a try and run it directly in your browser.
You can find the complete example here
Consuming Mesh Schema in code
After you have tested your new API, you can use it directly in your app in order to fetch data.
You can use the Mesh
GraphQLSchema instance to query your data from your application code by using
getMesh with your configuration object.
Start by load and parsing your configuration file, and pass it to
getMesh, this will return for your a modified version of GraphQL's
execute, so you can use it directly to fetch your data:
You need to install
@graphql-mesh/runtime to access GraphQL Mesh Schema inside your code.