B2B Integration 10.5 | Administering and Monitoring B2B Transactions | Service Development Help | Working with GraphQL Descriptors | Working with a GraphQL Dataloader
 
Working with a GraphQL Dataloader
Dataloader is a utility that improves the performance of your GraphQL query. Dataloader supports batching and caching functional capabilities.
Note: Integration Server supports Dataloader only for a Query operation.
When you create a Dataloader, Integration Server generates a loader service and a document type for keys. You can specify the field(s) in the key document for which you want to fetch the data from the data source. A loader service loads the data for the list of keys and returns a list of values.
While resolving the data for a field, Integration Server invokes the corresponding data resolver service. If you are using a Dataloader, the resolver service does not resolve the data for a field. Instead, it invokes the pub.graphql:load or pub.graphql:loadMany service. Integration Server then collects a batch of keys and invokes the loader service. The loader service loads the data for the batch. Resolver service collects this data and in turn, returns it to the user. All the loaded data is cached. Later, if you want to resolve data for the same keys, then Integration Server returns the values from the cache. This avoids repeated accessing of the data source.
In summary, a batch function helps in reducing multiple requests to the data source and a cache function eliminates repeated loading of same data in a single GraphQL request. Thus, Dataloader increases the query efficiency in GraphQL and resolves the N+1 problem. N+1 problem is explained using the following example.
* Example
Consider the following GraphQL schema:
type Query {
listPersons : [Person]
}

type Person {
name: String
email: String
friends: [Person]
}
Suppose you are using the following query:
{
listPersons {
name
email
friends {
name
email
}
}
}
For the above sample query, suppose resolver service returns one person and each person in has five friends. Then, resolver service queries the data source for 5+1 times. Similarly, if the resolver service returns N persons, then resolver service queries the data source for N+1 times. This is an N+1 problem.
As the number of values returned from the resolver service increase, the number calls to retrieve the data also increases. Moreover, if the child query has multiple complex fields, then the number of calls to the data source further increase.
Using Dataloader, the resolver service makes one call to fetch the person list and another call to fetch the friends list from the data source. In total, resolver service makes two calls instead of six (5+1) calls. Dataloader batches the keys to retrieve the person list and thus avoids multiple calls to data source.

Copyright © 2016- 2019 | Software AG, Darmstadt, Germany and/or Software AG USA, Inc., Reston, VA, USA, and/or its subsidiaries and/or its affiliates and/or their licensors.