如何使用Axios和GraphQL发布对象数据

时间:2019-01-25 02:23:29

标签: graphql axios

我试图使用突变查询和axios发布名称为city的对象数据,但出现错误状态代码:400错误的请求。

  axios.post('http://localhost:5000/graphql/', {
  query: `
  mutation ($city:cityInput!) {
    createCity (city:$city) {
      id
      cityName
    }
  }
`,
  variables:
    '{ "city": {"cityName": "test", "cityCode": "0001", "cityEnName": "test", "cityTwName": "test", "postCode": "0001", "provinceID": 1 }}',
})
.then(res => console.log(res))
.catch(err => console.log(err));

也许是因为我正在使用对象数据?但是我更喜欢使用对象数据。请帮助我,谢谢。

1 个答案:

答案 0 :(得分:0)

我测试了您的代码是否正常工作

客户:

axios.post('https://o5okynl7jy.sse.codesandbox.io/', {
  query: `
  mutation ($city: CityInput!) {
    createCity (city: $city) {
      id
      cityName
    }
  }`,
  variables:
    '{ "city": {"cityName": "test", "cityCode": "0001", "cityEnName": "test", "cityTwName": "test", "postCode": "0001", "provinceID": 1 }}',
})
.then(res => console.log(res))
.catch(err => console.log(err));

服务器:

https://codesandbox.io/s/o5okynl7jy