【问题标题】:how to pass the variables dynamic in GraphQL?如何在 GraphQL 中动态传递变量?
【发布时间】:2021-03-18 13:30:07
【问题描述】:

执行代码需要进行哪些更改:

const data = client.mutate({
    mutation: (Query)
});

let Query = gql`
  mutation{
    create(input:{
         department:"Tester", 
         code:"Tester"
    }
    )
      {
        code,
        details,
        description,   
    }
  }`
console.log(data, "data")

如何动态传递输入?我的意见是:

var department = {
   department:"Tester", 
   code:"Tester"
}

【问题讨论】:

    标签: reactjs graphql apollo-client gql


    【解决方案1】:

    我还没有测试过,但它会起作用。
    在反应阿波罗客户端

    import { gql, useMutation } from '@apollo/client';
    
    const Query = gql`
      mutation Create($department: String!, $code: String!) {
        create(department: $department, code: $code) {
            code
            details
            description
        }
      }
    `;
    const [create] = useMutation(Query);
    create({ variables: { department: department_value, code: code_value } });
    

    其他方式

    const departmentValue = "Tester"
    const codeValue = "Tester"
    client.mutate({
      variables: { department: departmentValue, code:codeValue },
      mutation: gql`
        mutation Create($department: String!, $code: String!){
          create(department: $department, code: $code){
            code
            details
            description
          }
        }
      `,
    })
    .then(result => { console.log(result) })
    .catch(error => { console.log(error) });
    

    【讨论】:

      猜你喜欢
      • 2020-12-26
      • 2019-11-19
      • 2021-06-09
      • 2013-11-09
      • 2019-03-28
      • 2021-09-23
      • 2020-05-05
      • 1970-01-01
      • 2020-09-30
      相关资源
      最近更新 更多