graphql - 如何使用 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));
也许是因为我使用了对象数据?但我更喜欢使用对象数据。请帮助我,在此先感谢。
解决方案
我测试了你的代码实际上是有效的
客户端:
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));
服务器:
推荐阅读
- android - 应用程序未安装错误开发 Android 应用程序
- java - ClassNotFoundException:没有找到与 org.redisson.RedissonMap 的类类型与 org.redisson.codec.JsonJacksonCodec 的编解码器类型匹配的 RObject
- javascript - 如何正确地将事件侦听器附加到 Angular 6+ 中的全局“窗口”?
- android - 在 EditText 上使用 requestFocus 时出现问题
- java - 试图从 Java 中的 jwk 获取公钥
- gradle - 在 TestKit 的 GradleRunner 上下文中,Gradle 登录到哪里?
- c++ - 读取文本文件以 C++ 结束
- python-3.x - 如何在 Python Pillow 中合并图像和 Gif
- assembly - 为程序集中的数组元素赋值(GAS 语法)
- kubernetes - 访问 api-server Kubernetes