【发布时间】:2019-04-25 18:51:47
【问题描述】:
我试图弄清楚如何访问retrieveRowsWithoutLocations 的args 字段。但是,我什至不确定 UnionType 的 resolveType 函数中的 value 参数是什么。
我正在查看文档https://graphql.org/graphql-js/type/#graphqluniontype,但它非常简短,并没有详细说明从何处获取该信息。我试过查看其他来源,但它们不是 graphql-js。
我要做的是访问args.type 并检查它的值,然后允许联合决定它应该返回哪种类型。
let rows_union =
new graphql.GraphQLUnionType({
name:
"rows_union",
types:
[
many_item,
many_list,
many_display_list,
],
resolveType(value)
{
switch(value)
{
case "item":
return many_item
case "list":
return many_list
case "display_list":
return many_display_list
}
}
})
// Define the Query type
var query =
new graphql.GraphQLObjectType({
name: "Query",
fields:
{
retrieveRowsWithoutLocations:
{
type:
rows_union,
args:
{
_id:
{
type:
nonNullId()
},
page:
{
type:
nonNullInt()
},
page_length:
{
type:
nonNullInt()
},
type:
{
type:
nonNullString()
},
},
async resolve ()
{
}
},
}
})
let many_type =
new graphql.GraphQLObjectType({
name: "many_"+name,
fields: {
results: {
type: graphql.GraphQLList(type)
},
total: {
type: graphql.GraphQLNonNull(graphql.GraphQLInt)
},
}
})
type 是另一个 ObjectType
【问题讨论】:
-
你能把
many_item,`many_list, andmany_display_list`的代码包括进来吗? -
@DanielRearden 不需要......它们只是对象类型。您可以轻松地假装他们是
Int、String或Float,这没关系... -
我不能这样做,因为标量不能在联合中使用:P 我想看看那个代码来满足我对您的特定用例的回答
-
@DanielRearden 我添加了它,但我认为它没有任何意义,因为我要在查询/突变中访问
args,这似乎不起作用因为resolveType函数实际上是在读取结果。我将把它拆分成多个查询。
标签: javascript node.js graphql-js