如何使用fetch()将GraphQL查询发送到GraphQLJS

时间:2019-06-05 19:40:26

标签: node.js graphql fetch graphql-js

我在向我的GraphQLJS后端发送GraphQL查询时遇到了麻烦,我复制并粘贴了我在GraphiQL中使用的查询(效果很好),但这会导致语法错误:{"errors":[{"message":"Syntax Error: Expected :, found String \": \"","locations":[{"line":2,"column":45}]}]}

graphQLTest = async () => {
  var data = {
    query: `mutation {
      createAuctionOffering(features:"{\"year\": \"2002\", \"model\":{\"make\":\"honda\", \"selectedModel\":\"civic\"}}", start_time:"2019-03-15 19:05:45.109+00", duration:"2019-03-15 19:05:45.109+00"){
        auc_id
      }
    }`,
  }
  // data = await JSON.stringify(data)
  var token = jwt.sign({name:data}, 'LIdXNnmK2qJNyTGs456bR0iebf9eGZV7', {expiresIn: '10s'});
  let response = await fetch('http://localhost:8000/graphql', {
    method: 'post',
    headers: {
      "User": "charlesdsmith25@gmail.com",
      "Content-Type": "application/json"
    },
    body: JSON.stringify({name:token})
  })
  let jsonResponse = await response.text()
  console.log(jsonResponse)

}

我也一直在使用JWT,但这并不是问题,因为查询将其传递到后端并引发了语法错误。这是我要发送的查询:

var data = {
    query: `mutation {
      createAuctionOffering(features:"{\"year\": \"2002\", \"model\":{\"make\":\"honda\", \"selectedModel\":\"civic\"}}", start_time:"2019-03-15 19:05:45.109+00", duration:"2019-03-15 19:05:45.109+00"){
        auc_id
      }
    }`,
  }

问题似乎在于引号

0 个答案:

没有答案