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

查看:27
本文介绍了是否可以将对象作为变量传递给 graphQL 突变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将对象作为变量传递给 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
    }
  }
`;

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

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

推荐答案

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

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

这篇关于是否可以将对象作为变量传递给 graphQL 突变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆