Apollo-React-GraphQL Mutation

泄露秘密 提交于 2019-12-11 02:38:09

问题


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.


回答1:


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
    }
  }
;


来源:https://stackoverflow.com/questions/43087126/apollo-react-graphql-mutation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!