Search code examples
argumentsgraphqlgraphiql

Graphql multiple arguments in field


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”


Solution

  • 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"'
      },
    }