字段中的 Graphql 多个参数
Graphql multiple arguments in field
我正在使用 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 获取文章 ID,但我在获取 parentValue.priceType 时遇到问题。
GraphiQL 还告诉我 priceType 不存在:
Unknown argument “priceType”. On field “price” of type “RootQuery”
args
对于字段采用对象而不是数组。尝试:
args: {
articleID: {
type: new GraphQLList(GraphQLString),
description: 'List with articles. Example: ["artid1","artid2"]'
},
priceType: {
type: new GraphQLList(GraphQLString) ,
description: 'PriceType. Example: "SalePrice","CurrentPrice"'
},
}
我正在使用 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 获取文章 ID,但我在获取 parentValue.priceType 时遇到问题。
GraphiQL 还告诉我 priceType 不存在:
Unknown argument “priceType”. On field “price” of type “RootQuery”
args
对于字段采用对象而不是数组。尝试:
args: {
articleID: {
type: new GraphQLList(GraphQLString),
description: 'List with articles. Example: ["artid1","artid2"]'
},
priceType: {
type: new GraphQLList(GraphQLString) ,
description: 'PriceType. Example: "SalePrice","CurrentPrice"'
},
}