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”

1 Answer 1

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"'
  },
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.