Rukmal Senavirathne
API Integration Essentials
2 min readMar 14, 2021

--

How to use GraphQL Context to shared data efficiently between DataFetchers ??

In some special cases in GraphQL the same database data should be used with different DataFetchers.

For an example:

In the special case of user requirement, developers need to create two different DataFetchers for createTime and lastUpdateTime.

For the operationInformation developer should have to use an apiType that comes from AM_DB.

This situation has an issue,

If a user requests all of these three data fields then 3 parallel requests go to the AM_DB database.

How to handle this issue??

Example :- If user requests createdTime then getCreatedTimeDataFetcher get also createdTime and apiType from the same query. After that pass createdTime and apiType to the GraphQL context.

What is the GRAPHQL CONTEXT??

In the lastUpdateDataFetcher and OperationDataFetcher we can check if the context is null or not.if context not null then lastUpdate and apiType can get from GraphQL context. So we can reduce two dataBase calls that are going from getLastUpdateDataFetcher and getOperationInfomation DataFetcher.

Do this in the getLastUpdateDataFetcher and getOperationInformation DataFetchers.

How to solve this issue in spring boot GRAPHQL??

Pass empty Map<String, ContextDTO> to GraphQL Context.

In the DataFetchers get createTime, lastUpdatetime and apiType Data from DataBase.

  1. For the operationInformationDataFetcher need apiType but from the dataBase query also get createTime and lastUpdatetime Data.

2. In the createdTime and lastUpdateTime DataFetchers do the above operation.

for more Information follow this implementation:

https://github.com/rukmals/carbon-apimgt/blob/registry-abstraction-5-graphql/components/apimgt/org.wso2.carbon.graphql.api.devportal/src/main/java/org/wso2/carbon/graphql/api/devportal/dataFetcher/ApiDataFetcherImpl.java

--

--

Rukmal Senavirathne
API Integration Essentials

Graduated from the Department of Computer Science and Engineering at the University of Moratuwa.