arguments - Graphql字段中的多个参数
问题描述
我正在使用 GraphQL。我可以在一个字段中传递一个参数。但我想知道如何将多个参数传递给一个字段。
这是我的代码: GraphlQL 对象类型:价格可用性
const priceAvailability = new GraphQLObjectType({
name: "priceAvailability",
description: "Check price and availability of article",
fields: () => ({
articleID: {
type: GraphQLString
},
priceType:{
type:GraphQLString
},
stockAvailability: {
type: StockAvailabilityType,
resolve(parentValue, args) {
// stuff to get the price and availability
return (data = getStockAvailability.getStockAvailability(
parentValue.isbn, parentValue.omgeving
));
}
}
})
});
根查询
const RootQuery = new GraphQLObjectType({
name: "RootQuery",
fields: () => ({
price: {
type: new GraphQLList(priceAvailability),
args: [{
articleID: {
type: new GraphQLList(GraphQLString),
description:
'List with articles. Example: ["artid1","artid2"]'
},
priceType: {
type: new GraphQLList(GraphQLString) ,
description:
'PriceType. Example: "SalePrice","CurrentPrice"'
}]
},
resolve: function(_, { articleID , priceType}) {
var data = [];
// code to return data here
return data;
}
}
})
});
架构
module.exports = new GraphQLSchema({
query: RootQuery
});
这是我在 GraphiQL 中用来测试的查询:
{
query: price(articleID:"ART03903", priceType:"SalePrice" ){
stockAvailability {
QuantityAvailable24hrs
QuantityAvailable48hrs
}
}
}
我可以通过 parentValue.articleID 获取 articleID,但在获取 parentValue.priceType 时遇到问题。
GraphiQL 还告诉我 priceType 不存在:
未知参数“priceType”。在“RootQuery”类型的“价格”字段上</p>
解决方案
args
对于一个字段,它接受一个对象而不是一个数组。尝试:
args: {
articleID: {
type: new GraphQLList(GraphQLString),
description: 'List with articles. Example: ["artid1","artid2"]'
},
priceType: {
type: new GraphQLList(GraphQLString) ,
description: 'PriceType. Example: "SalePrice","CurrentPrice"'
},
}
推荐阅读
- reactjs - 元素隐式具有“任何”类型,因为类型...在访问对象时没有索引签名,其中键是枚举
- sql - 如何更正此删除查询
- python - 从嵌套字典中删除键(Python 键)
- boto3 - 通过 Firehose 将地图数据放到 Athena
- php - 问题正则表达式模式寻找相似匹配
- vb.net - 如何在 VB.NET 集合类中实现 LINQ 函数
- python - Opencv全向标定整改
- python - 在 Heroku 上部署 Flask 应用程序,但在 / [GET] 和找不到模板时出现异常
- linux - 基于bash shell中的公共列合并文件
- python - 有没有办法在基于 Linux 的系统上为 Outlook 创建 .msg 文件?