How to: Configure Sources with no definition
GraphQL Mesh v0 documentation (superseded by v1): Learn how to configure sources with no definition using GraphQL Mesh. Use the jsonSchema handler to provide a definition of APIs that don't provide an API definition.
GraphQL Mesh provides an extensive range of Handlers (OpenAPI, gRPC, SOAP, GraphQL, and even Databases!), however, you might try to configure a Source that does not provide an API definition.
We will again use the “Books” example REST API:
- Books API (REST API)
GET /booksGET /books/:idGET /categories
However, let’s pretend that the “Books” API is not providing any OpenAPI definition file:
Once again, GraphQL Mesh gets you covered with the @graphql-mesh/json-schema handler that will
help provide a definition of the API.
An overview of the jsonSchema handler
A jsonSchema handler configuration must provide a set of operations that define the Query and
Mutation to expose.
A standard jsonSchema handler configuration will look at the following:
operations defines a set of GraphQL queries or mutations mapped to some API endpoints (path,
method)
Above, the users Query targets the GET /users endpoint.
Finally, we provide responseSample that points to a sample file of the Query response.
By using the responseSample file, GraphQL Mesh will be able to generate a GraphQL definition of
the users Query.
Let’s put it in practice with our “Books” REST API GET /book/:id endpoint.
Configuring our “Books” REST API
You will find all the source code of the below example in the dedicated repository:
graphql-mesh-docs-first-gateway.
After installing the @graphql-mesh/json-schema package, a good starting point for our “Books” REST
API jsonSchema handler configuration would be the following
.meshrc.yaml:
We need to provide some operations for the GET /book/:id along with some sample data.
We can get some sample data by running the following commands (at the root of the project):
- Build and start the Books REST API
- In a separate terminal, run the following
curlcommand:
Which will create the following
packages/single-source-no-source-definition/samples/book-1.json
file:
We can now provide use this sample file to configure our Query.book operation as follows
.meshrc.yaml:
Let’s build and start our Gateway by running:
Once built, you will find the Unified Schema definition at
packages/single-source-no-source-definition/.mesh/schema.graphql:
We can open the browser at and http://0.0.0.0:4000 try the following Query with GraphiQL:
We successfully added a Source without definition! 🎉
Going further
Rename generated types
Our Query.book jsonSchema configuration generates a query_book type which is poorly named and
using snake_case.
The responseTypeName allows us to provide a type name that we will be used for the generated type,
as follows
.meshrc.yaml:
Which produces the following Unified Schema:
The same parameter exists for request: requestTypeName (see “Mutations”).
Queries/Mutations arguments
We saw that operations[].path can take arguments using syntax similar to string interpolation
.meshrc.yaml:
GraphQL Mesh will assign the ID type to all arguments by default.
Now, let’s say that the “Books” API introduces a search endpoint as follows:
GET /books/search?q=<query>&categoryId=<categoryId>
We would need the query argument to be of type String (not ID).
To achieve this, we will leverage the argTypeMap parameter as follow:
Now, GraphQL Mesh knows that the type definition of our search query is:
Query.search(query: String!, categoryId: ID).
Mutations
Since Mutations are most likely to rely on POST requests, we need to provide both a
requestSample and a responseSample parameters.
For example, if our “Books” API exposed a POST /books, we could define the following
Mutation.addBook(…) mutation:
Query/Mutation with multiple response shapes
Providing responseSample/requestSample is an efficient way to configure a Source without API
definition.
However, the sample files might not always represent all the variants of a given endpoint.
For example, our previous POST /books endpoint could return a totally different response shape
depending on the scenario:
Successfully book creation response
Unauthorized response
Duplicate book response
Relying on curl to get a sample of all those scenarios is impossible.
In that case, we will to provide a responseSchema parameter that will point to a JSON Schema file:
This guide doesn’t provide any materials to learn JSON Schema (this would require multiple guides).
However, you can get started on the official JSON Schema tutorial.
Our ./schema/add-book-response.json would look as follows:
Building our Mesh Gateway will generate the following Unified Schema:
Note that you can also leverage the responseByStatusCode parameter to provide a schema per HTTP
Status Code, as follows: