The Guild LogoThe Guild Monogram
GraphQL Mesh

GraphQL Mesh

Query anything, run anywhere.

Contact Us

Performances best practices#

A Mesh Gateway that does is not properly configured will face the same famous issue as any other GraphQL servers: the N+1 Query problem.

Fortunately, GraphQL Mesh provides a way to batch requests with specific additionalResolvers parameters.

 

Enable requests batching#

Our multiple-sources example Gateway:

Will emit multiple requests to the "Authors" API when resolving the nested Book.author field:

query bestSellersByStore { stores { id name bookSells { sellsCount book { id title author { id name } } } } }

 

Fortunately, Mesh allows a additionalResolvers configuration to provide a "batching" query that will help resolve many record of the same type.

Our current additionalResolvers configuration for Book.author is the following:

.meshrc.yaml

sources: # ... transforms: # ... additionalTypeDefs: | # ... extend type Book { author: authors_v1_Author } additionalResolvers: - targetTypeName: Book targetFieldName: author sourceName: Authors # Which source does the target field belong to? sourceTypeName: Query # Which root type does the target field belong to? sourceFieldName: authors_v1_AuthorsService_GetAuthor # What is the source field name? requiredSelectionSet: | { authorId } sourceArgs: # What args does this need to take? input.id: "{root.authorId}"

Assuming that the "Authors" is exposing a authors_v1_AuthorsService_GetAuthors(input: authors_v1_GetAuthorsRequest_Input) with authors_v1_GetAuthorsRequest_Input being:

input authors_v1_GetAuthorsRequest_Input { ids: [String!]! }

We could update our .meshrc.yaml configuration as follows:

.meshrc.yaml

sources: # ... transforms: # ... additionalTypeDefs: | # ... extend type Book { author: authors_v1_Author } additionalResolvers: # Create a resolver with batching to solve N+1 problem - sourceName: Authors sourceTypeName: Query targetTypeName: Book targetFieldName: author sourceFieldName: authors_v1_AuthorsService_GetAuthors keyField: authorId keysArg: input.ids

requiredSelectionSet and sourceArgs got replaced by keyField and keysArg:

  • keysArg provides the name of the batching primary key argument (input.ids from authors_v1_GetAuthorsRequest_Input)
  • keyField indicates which Book selection-set field should be used to provide the ids value

Now, our Mesh Gateway will try to batch calls to the "Authors" API when resolving Book.author: