【问题标题】:How to pass JSON object in grpahql and strapi如何在graphql和strapi中传递JSON对象
【发布时间】:2019-06-22 22:30:43
【问题描述】:

当我手动编写突变查询(在 graphql 插件中)时,它正在工作:

mutation {
                    createExam(input: {
                      data: {
                        name: "myName"
                        desription: "ggg"
                        questions: [{gf: "hello"}]
                        time: 2
                        subjects: ["5c468e2d61670b25b46ccdfe"]
                      }
                    }) {
                      exam {
                        name
                                desription
                        time

                      }
                    }
                  }

但是,如果我对其进行编码并传递完全相同的数组,我会得到一个完全相同的对象数组,我会得到 [null, null]

let parsedQuestion = [{gf: "hello"}];

 const response = await strapi.request('POST', '/graphql', {
            data: {
                query: `mutation {
                    createExam(input: {
                      data: {
                        name: "` + examInfo.newExamName + `"
                        desription: "` + examInfo.newExamDescription + `"
                        time: ` + Number(examInfo.newExamTime) + `,
                        questions: `+ parsedQuestion + `, 
                        subjects: ["` + this.state.modalSubject._id + `"]
                      }
                    }) {
                      exam {
                        name
                        desription
                        time
                        questions

                      }
                    }
                  }`
            }

怎么可能?这可能是一个错误吗?我也尝试过使用 JSON.stringify 但后来出现错误,甚至没有发生突变

提前非常感谢

【问题讨论】:

    标签: json graphql strapi


    【解决方案1】:

    以这种方式构造查询字符串容易出错且危险;它使您面临大量错误和众所周知的安全漏洞。 (如果newExamNameMy "super-duper" exam!!! 怎么办?)

    GraphQL 提供variables 作为传递数据的更好方法。在您的情况下,由于您有一个复杂的结构化对象,因此将整个输入作为一个对象传递可能是最简单的(其他语法也是可能的)。我希望这看起来像:

    const response = await strap.request('POST', '/graphql', {
      data: {
        query: `mutation CreateExam($input: CreateExamInput!) {
          createExam(input: $input) {
            exam { name, desription, time, questions }
          }
        }`,
        variables: {
          input: {
            name: examInfo.newExamName,
            desription: examInfo.newExamDescription,
            time: Number(examInfo.newExamTime),
            questions: [{gf: "hello"}],
            subjects: [this.state.modalSubject._id]
          }
        }
      }
    });
    

    现在 HTTP 客户端库可以负责从您的输入生成格式良好的 JSON,而您无需执行棘手的字符串操作。

    【讨论】:

    • 非常感谢大卫,我还没有时间检查它,但您似乎真的帮助我了解了如何构建查询,非常感谢
    • 嘿大卫,不幸的是,这个确切的代码不起作用,我收到了“错误的请求”。你知道是什么原因造成的吗?也许是因为strapi中的graphql插件的语法略有不同?我尝试过使用它,但根本没有成功
    • 我能想到的最明显的事情是,我只是在突变中编造了 GraphQL 输入类型的名称,但你应该得到一个 GraphQL 错误。
    • 我知道这是一个老问题,但你的请求很糟糕,因为输入变量需要一个“数据”对象键 variables { input : { data : { ... all vars }}}
    • 不敢相信 GQL 如此受欢迎,因为样板如此之多......
    猜你喜欢
    • 2020-09-27
    • 2021-11-26
    • 1970-01-01
    • 1970-01-01
    • 2017-07-31
    • 1970-01-01
    • 1970-01-01
    • 2020-02-03
    • 2020-08-16
    相关资源
    最近更新 更多