问题描述
给出以下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
}
我尝试了以下操作..您可以看到在指定author函数之前,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服务器中创建嵌套的解析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!