Apollo 解析器链返回 null

Apollo resolver chain returning null

现在已多次查看文档,但我不认为我明白了。我有以下(简化的)定义:

type executive {
  executiveName: String,
  office: [office]
}
type office {
  officeName: String,
}

可能有很多(数千)个办公室可以 returned,所以我想打破高管和办公室之间的通话,这样我就可以先展示高管,然后加载办公室的背景。 我这样称呼我的 API:

query {
    executives {
      executiveName
      office {
        officeName
      } 
    }
}

问题 1:这种层次结构是否适用于这种情况?有没有更好的方法可以建议我构建查询?

我还有以下解析器:

const queryType = new GraphQLObjectType({
  name: "Query",
  description: "Query entry point for the application",
  fields: () => ({
    executives: {
      type: GraphQLList(executives),
      resolve: (_, __, { dataSources }) => {
        return dataSources.elasticAPI.getExecutives();
      }
    },
    {
      office: {
        type: office,
        name: "office",
        resolve: async (_, { startDate, endDate }, { dataSources }) => {
          const offices = await dataSources.elasticAPI.getOffices(
            startDate,
            endDate
          );
          return offices
        }
      }
    })
  })
})
    

但得到如下响应:

{
  "data": {
    "executives": [
      {
        "executiveName": "Joe Smith",
        "office": null
      }
      ...
    ]
  }
}

问题 2:为什么我的解析器 return 为空?我原以为 offices 解析器会 return 编辑信息,但 GraphQL 无法识别它。

Why are my resolvers returning null? I would have thought that the offices resolver would have returned the information but it is not being recognized by GraphQL.

您在所需架构中显示的 office 字段是 executive 类型的一部分,而不是 Query 类型的一部分。您的解析器对象需要反映这一点。