首页 > 解决方案 > 如何使用 Axios 和 GraphQL 发布对象数据

问题描述

我正在尝试使用突变查询和 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));

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

标签: graphqlaxios

解决方案


我测试了你的代码实际上是有效的

客户端:

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


推荐阅读