Graphql with mutation spring boot

Viewed 3203

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: "kitte@gmail.com",
    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.

1 Answers

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

Related