8

My schema file is

type Mutation { createCustomer(name: String!, email: String!, product: [Product]): Customer } input Product { id: ID! name: String! price: Int } interface Person { id: ID! name: String! email: String! } type Customer implements Person { id: ID! name: String! email: String! product: [Product] } 

I want to insert customer detail here which has product list as input. My query is

mutation { createCustomer( name: "kitte", email: "[email protected]", product: [ { name: "soap", price: 435, } ] ) { id name email product{name} } } 

But I am getting exception

{ "data": null, "errors": [ { "validationErrorType": "WrongType", "message": "Validation error of type WrongType: argument value ArrayValue{values=[ObjectValue{objectFields=[ObjectField{name='name', value=StringValue{value='dars76788hi'}}, ObjectField{name='price', value=IntValue{value=123}}]}, ObjectValue{objectFields=[ObjectField{name='name', value=StringValue{value='darr'}}, ObjectField{name='price', value=IntValue{value=145}}]}]} has wrong type", "locations": [ { "line": 5, "column": 5 } ], "errorType": "ValidationError" } ] } 

I don't understand what is the error. And how to pass list to mutation. I have referred some examples but not able to insert product as list.

2
  • Have you tried to get some product fields in your mutation, like mutation { createCustomer(...) { id, name, email, product {name} } }? Commented Nov 13, 2017 at 17:21
  • It solved this error. But getting new error Commented Nov 14, 2017 at 9:05

1 Answer 1

7

Make sure you are passing the right type of objects to your mutation. GraphQL needs separate types for input fields. In your schema, Product types should be something like this and you should change the mutation accordingly.

type Product { id: ID! name: String! price: Int } input ProductInput { name: String! price: Int } input CustomerInput { ... products: [ProductInput] } 

There are couple of very useful examples in the docs, see Mutations and Input Types

Sign up to request clarification or add additional context in comments.

5 Comments

It is storing customer to db. but not able to return product by products { name }
mutation Customer { createCustomer(name: "trrth", email: "night", product: [{id:"1", name: "thrh", price: 123}]) { id name email products { id name } } }
It should be product {id, name} based on your schema. Watch for single product vs products and add a coma between id and name.
Never mind the comas :) I see that you were just writing quickly. You are getting Wrong type error so try to add type Product {...} in addition to your input type.
Thanks for your answer. I have resolved this by taking input type for mutation and returning type instread of input type. So i am not getting type cast error now.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.