GraphQL Java: using @Batched DataFetcher

I know how to extract a bean from a service in datafetcher :

public class MyDataFetcher implements DataFetcher {
  ...

  @Override
  public Object get(DataFetchingEnvironment environment) {
    return myService.getData();
  }
}

But nested-list schemes should use BatchedExecutionStrategy and create batch DataFetchers using get () methods annotated with @Batched ( see graphql-java document ).

But where can I put the getData () call?

///// Where to put this code?
List list = myService.getData();
/////

public class MyDataFetcher implements DataFetcher {

  @Batched
  public Object get(DataFetchingEnvironment environment) {
    return list.get(environment.getIndex()); // where to get the index?
  }
}
+4
source share
1 answer

: BatchedExecutionStrategy 6 . . , , "".

. DataFetchers (DataFetchingEnvironment#getSource) . , :

{
   user (name: "John") {
       company {
           revenue
       }
}

company () User , , - Company ,

User owner = (User) environment.getSource();
Company company = companyService.findByOwner(owner);
return company;

, , DataFetcher , BatchedExecutionStrategy, , User Company, List<User> List<Company>.

List<User> owners = (List<User>) environment.getSource();
List<Company> companies = companyService.findByOwners(owners);
return companies;

, , , . , myService.getData , .

, , .

+3

Source: https://habr.com/ru/post/1668813/


All Articles