0

我正在使用 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>

4

1 回答 1

2

args对于一个字段,它接受一个对象而不是一个数组。尝试:

args: {
  articleID: {
    type: new GraphQLList(GraphQLString),
    description: 'List with articles. Example: ["artid1","artid2"]'
  },
  priceType: {
    type: new GraphQLList(GraphQLString) ,
    description: 'PriceType. Example: "SalePrice","CurrentPrice"'
  },
}
于 2018-10-03T14:55:19.690 回答