问题描述
给定以下 apollo 服务器 graphql 架构我想将这些分解成单独的模块,所以我不希望在根查询模式下的作者查询..并希望将其分开.所以我在将它添加到根查询之前添加了另一个名为 authorQueries 的层
Given the following apollo server graphql schemaI wanted to break these down into separate modules so I don't want the author query under the root Query schema.. and want it separated. So i added another layer called authorQueries before adding it to the Root Query
type Author {
id: Int,
firstName: String,
lastName: String
}
type authorQueries {
author(firstName: String, lastName: String): Author
}
type Query {
authorQueries: authorQueries
}
schema {
query: Query
}
我尝试了以下..您可以看到在指定作者函数之前将 authorQueries 添加为另一层.
I tried the following.. you can see that authorQueries was added as another layer before the author function is specified.
Query: {
authorQueries :{
author (root, args) {
return {}
}
}
}
在 Graphiql 中查询时,我还添加了额外的层..
When querying in Graphiql, I also added that extra layer..
{
authorQueries {
author(firstName: "Stephen") {
id
}
}
}
我收到以下错误.
"message": "Resolve function for \"Query.authorQueries\" returned undefined",
推荐答案
要创建嵌套"解析器,只需在父字段的返回类型上定义解析器.在这种情况下,您的 authorQueries
字段返回类型 authorQueries
,因此您可以将解析器放在那里:
To create a "nested" resolver, simply define the resolver on the return type of the parent field. In this case, your authorQueries
field returns the type authorQueries
, so you can put your resolver there:
{
Query: { authorQueries: () => ({}) },
authorQueries: {
author(root, args) {
return "Hello, world!";
}
}
}
所以从技术意义上讲,没有嵌套解析器这样的东西——每个对象类型都有一个扁平的字段列表,这些字段有返回类型.GraphQL 查询的嵌套是结果嵌套的原因.
So in the technical sense, there is no such thing as a nested resolver - every object type has a flat list of fields, and those fields have return types. The nesting of the GraphQL query is what makes the result nested.
这篇关于如何在 apollo graphql 服务器中创建嵌套解析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!