How to use batched datafetcher

90 views
Skip to first unread message

Mousom dhar gupta

unread,
Sep 13, 2016, 11:35:39 AM9/13/16
to graphql-java
Hi,

It would be nice to have specific unit tests showing how to use batched data-fetcher.
I have seen few of them along with tests for other data-fetchers (in GraphqlExecutionSpec.groovy) but I couldn't grasp from them how to support the following
use-case.
Apologies in advance if it's trivial, which I thought it would be in the beginning.

So the use-case is looking up Objects using primary-key ids. But each field of the object comes from a different source over the network so we would like
to batch load each field.
An example would be "Person" object with two fields "address" and "name" where each of them needs a dataloader to fetch data from a remote service using personId.
And a single query might want to load many personIds.

Following could be the schema for loading information of one person but am not sure how to implement batch-loading when multiple personIds are involved.

public static final GraphQLObjectType PersonQueryType = newObject()
      .name("PersonQueryType")
      .field(newFieldDefinition()
          .name("name")
          .type(Scalars.GraphQLString)
          .argument(newArgument()
              .name("personId")
              .type(new GraphQLNonNull(Scalars.GraphQLBigInteger))
              .build())
          .dataFetcher(new DataFetcher() {
            @Override
            @Batched
            public Object get(DataFetchingEnvironment environment) {
              // call a remote service
              return environment.getArgument("input");
            }
          })
          .build())
      .field(newFieldDefinition()
          .name("address")
          .type(Scalars.GraphQLString)
          .argument(newArgument()
              .name("personId")
              .type(new GraphQLNonNull(Scalars.GraphQLBigInteger))
              .build())
          .dataFetcher(new DataFetcher() {
            @Override
            @Batched
            public Object get(DataFetchingEnvironment environment) {
              // call a remote service
              return environment.getArgument("input");
            }
          })
          .build())
      .build();


Thanks in advance,
Mousom
Reply all
Reply to author
Forward
0 new messages