Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Graphql with mutation spring boot

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.

like image 987
shagun Avatar asked Nov 13 '17 14:11

shagun


People also ask

Can we use GraphQL with spring boot?

The Spring Boot GraphQL Starter offers a fantastic way to get a GraphQL server running in a very short time. Combined with the GraphQL Java Tools library, we need only write the code necessary for our service.

What does mutation mean in GraphQL?

Mutations allow you to modify server-side data, and it also returns an object based on the operation performed. It can be used to insert, update, or delete data. Dgraph automatically generates GraphQL mutations for each type that you define in your schema.


Video Answer


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

like image 128
Andrija Ćeranić Avatar answered Nov 13 '22 23:11

Andrija Ćeranić