Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在grpahql和strapi中传递JSON对象_Json_Graphql_Strapi - Fatal编程技术网

如何在grpahql和strapi中传递JSON对象

如何在grpahql和strapi中传递JSON对象,json,graphql,strapi,Json,Graphql,Strapi,当我手动编写变异查询(在graphql插件中)时,它正在工作: mutation { createExam(input: { data: { name: "myName" desription: "ggg" questions: [{gf: "hello"}]

当我手动编写变异查询(在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,但后来出现了一个错误,甚至没有通过变异


提前感谢大家

以这种方式构造查询字符串容易出错且危险;它会让您面临大量的bug和众所周知的安全漏洞。(如果
newExamName
我的“超级复制者”考试!!!
?)

GraphQL提供了一种更好的方法来传递数据。在您的例子中,因为您有一个复杂的结构化对象,所以可能最容易将整个输入作为一个对象传入(其他语法也是可能的)。我希望这看起来像:

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,并且您没有执行复杂的字符串操作。

非常感谢David我还没有时间检查它,但您似乎真的帮助我理解了如何构造查询感谢David,不幸的是,这个确切的代码不起作用,我收到了“错误的请求”。你知道是什么引起的吗?也许是因为strapi中的graphql插件的语法略有不同?我试着玩弄它,但没有成功。我能想到的最明显的事情是,我只是在变异中编造了GraphQL输入类型的名称,但你应该会得到一个GraphQL错误。我知道这是一个老问题,但你得到了一个坏的请求,因为输入变量需要一个“数据”对象键
变量{input:{数据:{…所有变量}}}