GraphQL Java:使用@Batched DataFetcher [英] GraphQL Java: Using @Batched DataFetcher

查看:82
本文介绍了GraphQL Java:使用@Batched DataFetcher的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何从datafetcher中的服务中检索bean :

public class MyDataFetcher implements DataFetcher {
  ...

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

但是具有嵌套列表的架构应使用BatchedExecutionStrategy并使用批注为@Batched的get()方法创建批处理的DataFetchers(请参见graphql-java文档).

But schemas with nested lists should use a BatchedExecutionStrategy and create batched DataFetchers with get() methods annotated @Batched (see graphql-java doc).

但是我该把getData()调用放在哪里?

But where do I put my getData() call then?

///// 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?
  }
}

推荐答案

警告::原始的 BatchedExecutionStrategy 已被弃用,并将被删除.当前首选的解决方案是数据加载器库.另外,整个执行引擎将在未来被替换,而新的执行引擎将再次本地"支持批处理.您已经可以使用新引擎新的 BatchedExecutionStrategy (均在 nextgen 软件包中),但它们对检测的支持有限.下面的答案同样适用于传统执行引擎和下一代执行引擎.

WARNING: The original BatchedExecutionStrategy has been deprecated and will get removed. The current preferred solution is the Data Loader library. Also, the entire execution engine is getting replaced in the future, and the new one will again support batching "natively". You can already use the new engine and the new BatchedExecutionStrategy (both in nextgen packages) but they have limited support for instrumentations. The answer below applies equally to both the legacy and the nextgen execution engine.

这样看.普通的 DataFetchers 接收一个对象作为源( DataFetchingEnvironment#getSource ),并返回一个对象.例如,如果您有这样的查询:

Look at it like this. Normal DataFetcherss receive a single object as source (DataFetchingEnvironment#getSource) and return a single object as a result. For example, if you had a query like:

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

您的 company 解析程序(fetcher)将获得一个 User 对象作为源,并且将基于该对象以某种方式返回 Company 例如

Your company resolver (fetcher) would get a User object as source, and would be expected to somehow return a Company based on that e.g.

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

现在,在完全相同的情况下,如果您的 DataFetcher 已批处理,并且您使用了 BatchedExecutionStrategy ,而不是接收 User 并返回一个 Company ,您将收到一个 List< User> ,并将返回一个 List< Company> .

Now, in the exact same scenario, if your DataFetcher was batched, and you used BatchedExecutionStrategy, instead of receiving a User and returning a Company, you'd receive a List<User> and would return a List<Company> instead.

例如

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

请注意,这意味着您的基础逻辑必须具有一种可以一次获取多个事物的方法,否则将无法进行批处理.因此,除非您已经可以一次性获取多个源对象的数据,否则您的 myService.getData 调用将需要更改.

Notice that this means your underlying logic must have a way to fetch multiple things at once, otherwise it wouldn't be batched. So your myService.getData call would need to change, unless it can already fetch data for multiple source object in one go.

还要注意,批处理分辨率仅在嵌套查询中有意义,因为顶级解析器已经可以获取对象列表,而无需进行批处理.

Also notice that batched resolution makes sense in nested queries only, as the top level resolver can already fetch a list of object, without the need for batching.

这篇关于GraphQL Java:使用@Batched DataFetcher的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆