如何使用 Apollo graphql 动态调整查询形状(字段)?

How to dynamically adjust Query shape (fields) using Apollo graphql?

我想在 java 中使用 Apollo graphql 仅查询选定的字段。

找不到任何说明我们如何实现该目标的文章

我在 .graphql 文件中这样定义我的查询,

query getResources{
   resources(filterBy: {} ) {
      edges{
         cursor
         node   {
                id
                name
                canonicalName
                description
                createdAt
                updatedAt
                createdBy
                updatedBy
         }
      }
      pageInfo{
          hasPreviousPage
          hasNextPage
      }
   }
}

发出查询请求时 执行 = apolloClient.query(getResourcesQuery).execute();

我想将 getResourcesQuery 对象更改为仅查询某些字段。 我们该怎么做?

Apollo Android 并不是真正的查询构建器——您不能指定要添加到选择集中的各个字段。相反,您提供的查询将按原样发送。如果您正在寻找那种功能,您可能需要查看不同的客户端(例如 nodes)。

也就是说,您可以利用 @skip@include 指令,结合一些变量,动态控制请求选择集中包含的内容。例如:

query getResources(
  $includeEdges: Boolean = true
  $includePageInfo: Boolean = true
) {
  resources(filterBy: {}) {
    edges @include(if: $includeEdges) {
      cursor
      node {
        id
        name
        canonicalName
        description
        createdAt
        updatedAt
        createdBy
        updatedBy
      }
    }
    pageInfo @include(if: $includePageInfo) {
      hasPreviousPage
      hasNextPage
    }
  }
}

然后只需添加变量:

GetResources getResourcesQuery = GetResources.builder()
    .includePageInfo(false)
    .build();

apolloClient().query(getResourcesQuery).execute();

apollo 不支持此功能

https://github.com/apollographql/apollo-android/issues/1014 dynamic query graphql apollo with java