【发布时间】:2018-08-05 16:46:06
【问题描述】:
我目前正在尝试解析一个包含成分参考的简单食谱列表。
数据布局如下所示:
type Ingredient {
name: String!
amount: Int!
unit: Unit!
recipe: Recipe
}
type Recipe {
id: Int!
name: String!
ingredients: [Ingredient]!
steps: [String]!
pictureUrl: String!
}
据我了解,我的解析器应如下所示: 第一个解析配方,第二个解析配方中的成分字段。它可以(根据我的理解)使用 recipe 提供的参数。在我的食谱对象中,成分由 id (int) 引用,所以这应该是参数(至少我是这么认为的)。
var root = {
recipe: (argument) => {
return recipeList;
},
Recipe: {
ingredients: (obj, args, context) => {
//resolve ingredients
}
},
这些解析器像这样传递给应用程序:
app.use('/graphql', graphqlHTTP({
schema: schema,
graphiql: true,
rootValue: root,
}));
但是,我的解析器似乎没有被调用。我希望在我的查询中查询时即时解析这些成分。
端点可以工作,但只要我查询成分,就会返回一条错误消息"message": "Cannot return null for non-nullable field Ingredient.name.",。
当尝试在我的解析器中记录传入的参数时,我可以看到它从未被执行。不幸的是,当我像我一样使用 express-graphql 时,我找不到如何使用它的示例。
如何为 express-graphQL 中的嵌套类型编写单独的解析器?
【问题讨论】:
标签: node.js express graphql graphql-js express-graphql