是否可以将一个对象作为变量传入graphQL突变中?[英] Is it possible to pass an object into a graphQL mutation as variables?

本文是小编为大家收集整理的关于是否可以将一个对象作为变量传入graphQL突变中?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

是否可以将对象作为变量传递到GraphQl突变?

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

这是不起作用的,我看不到将对象定义为参数类型的任何方法.

推荐答案

您必须定义GraphQL输入类型.看看这个备忘单.

因此,在您的情况下,您必须在服务器上定义输入并将其在突变中使用,例如:

...

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

...

addData(data: DataInput!): SalesDate

`

在客户端,您可以使用它:

const mutation = gql
  mutation addData($data: DataInput!){
    addData(data: $data){
     data
    }
  }
;

本文地址:https://www.itbaoku.cn/post/1938080.html

问题描述

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.

推荐答案

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