0

I'm using GraphQL. I'm able to pass one argument in a field. But I would like to know how to pass multiple arguments to a field.

This is my code: GraphlQL Object type: Price availability

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
        ));
      }
    }
  })
});

The root query

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;
      }
    }
  })
});

Schema

module.exports = new GraphQLSchema({
  query: RootQuery
});

This is the query I use in GraphiQL to test:

{
  query: price(articleID:"ART03903", priceType:"SalePrice" ){
        stockAvailability {
          QuantityAvailable24hrs
          QuantityAvailable48hrs
        }
    }
}

I can get the articleID via parentValue.articleID, but I have issues with getting parentValue.priceType.

Also GraphiQL tells me that priceType does not exists:

Unknown argument “priceType”. On field “price” of type “RootQuery”

Elvira
  • 1,410
  • 5
  • 23
  • 50

1 Answers1

2

args for a field takes an object instead of an array. Try:

args: {
  articleID: {
    type: new GraphQLList(GraphQLString),
    description: 'List with articles. Example: ["artid1","artid2"]'
  },
  priceType: {
    type: new GraphQLList(GraphQLString) ,
    description: 'PriceType. Example: "SalePrice","CurrentPrice"'
  },
}
Daniel Rearden
  • 80,636
  • 11
  • 185
  • 183