React Apollo中本地解析器的执行顺序是什么?

时间:2019-11-01 22:40:18

标签: graphql apollo react-apollo apollo-client

说我有以下查询:

query Foo {
  foo {
    bar
    baz
    qux @client
  }
}

这些解析器:

Foo: {
  qux: () => {
    console.log("qux");
    return "qux";
  },
  bar: obj => {
    console.log("bar");
    return obj.bar;
  },
  baz: obj => {
    console.log("baz");
    return obj.baz;
  }
}

解析器的执行顺序是否取决于查询中的顺序?或者相反?

1 个答案:

答案 0 :(得分:0)

我知道了:没有“解析程序命令”。该顺序仅由查询给出。

如果您运行模拟查询,则会记录以下内容:

#include <iostream>
#include <numeric>
#include <iterator>
#include <vector>

template <typename IT, typename T>
T accumulate2d(IT outer_begin, IT outer_end,const T& init){
    using value_type = typename std::iterator_traits<IT>::value_type;
    return std::accumulate( outer_begin,outer_end,init,
        [](T accu,const value_type& inner){
            return std::accumulate( inner.begin(),inner.end(),accu);
        });
}

int main() {
    std::vector<std::vector<int>> x{ {1,2} , {1,2,3} };
    std::cout << accumulate2d(x.begin(),x.end(),0);
}

尽管import { ResourceCategory } from './resourceCategory'; import { Injectable } from '@angular/core'; import { Resource } from './resource'; import { HttpService } from '../project/http.service'; import { isNullOrUndefined } from 'util'; @Injectable() export class ResourceService { public allProjectResources: Resource[]; public allResourceCategories: ResourceCategory[]; uniqueResCategories = new Set(); constructor(private http: HttpService) { } getAllResourcesByProjId(projid: number) { if (!(isNullOrUndefined(this.allProjectResources))) { this.allProjectResources.length = 0; } this.http.getAllResourcesById(projid).subscribe((data: Resource[]) => this.allProjectResources = data); } getAllResourceCategories() { this.http.getAllResourceCategories().subscribe((data: ResourceCategory[]) => this.allResourceCategories = data); } getAllUniqieResourceCategories(){ this.allProjectResources.forEach(resource => { this.allResourceCategories.forEach(function (category) { if (resource.resourceCategoryId == category.id) { this.uniqueResCategories.add(category.name); console.log(category.name); }; }); }); } } 是第一个声明的解析器。仅当您在查询中将bar baz qux 移至上方时,才可以首先记录它,如下所示:

qux

那么日志将是:

qux @client

当然,此行为可能会因您所使用的版本而异。这是我的:

  • @ apolo / react-hooks @ 3.1.3
  • apollo-cache-inmemory@1.6.3
  • apollo-client@2.6.4
  • apollo-link-http@1.5.16
  • graphql@14.5.8
  • graphql-tag@2.10.1