Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to pass an object into a graphQL mutation as variables?

Is it possible to pass an object into a graphQL mutation as variables?

let salesDate = {
    company_id: "abcd",
    source: "tally",
    data: {
        value: "02",
        date: "2016-17-01"
    }
};

const mutation = gql`
  mutation addData($data: Object){
    addData(data: $data){
     data
    }
  }
`;

This is not working and I don't see any way to define an object as a parameter type.

like image 401
Avijit Dutta Avatar asked Oct 24 '25 04:10

Avijit Dutta


1 Answers

You have to define a GraphQL input type. Take a look at this cheat sheet.

So in your case you have to define a input on the server and use it in the mutation, something like:

...

`
input DataInput {
  value: Number!
  data: String!
}

...

addData(data: DataInput!): SalesDate

`

and on the client side you can use it like:

const mutation = gql
  mutation addData($data: DataInput!){
    addData(data: $data){
     data
    }
  }
;
like image 181
Locco0_0 Avatar answered Oct 25 '25 18:10

Locco0_0



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!