graphql - 如何设置 Apollo GraphQL 服务器以接受一个对象作为突变的变量?
问题描述
目前,我正在尝试将对象作为变量传递给突变,如下所示:
type ShopLocation {
lane1: String
lane2: String
city: String
postalCode: String
country: String
}
type ShopResponse {
statusCode: Int
messageCode: String
data: String
}
type Mutation {
createShop(
name: String
email: String
location: ShopLocation
): ShopResponse
}
但我收到以下错误:
{
"error": {
"errors": [
{
"message": "The type of Mutation.createShop(location:) must be Input Type but got: ShopLocation.",
"extensions": {
"code": "GRAPHQL_VALIDATION_FAILED",
"exception": {
"stacktrace": [
"Error: The type of Mutation.createShop(location:) must be Input Type but got: ShopLocation.",
" at assertValidSchema (.../node_modules/graphql/type/validate.js:71:11)",
" at Object.validate (.../node_modules/graphql/validation/validate.js:55:35)",
" at Promise.resolve.then (.../node_modules/apollo-server-core/src/runQuery.ts:188:30)",
" at <anonymous>",
" at process._tickDomainCallback (internal/process/next_tick.js:228:7)"
]
}
}
}
]
}
}
知道如何正确执行此操作吗?
解决方案
您需要使用ShopLocation
withinput
关键字而不是type
,
input ShopLocationInput {
lane1: String
lane2: String
city: String
postalCode: String
country: String
}
type ShopResponse {
statusCode: Int
messageCode: String
data: String
}
type Mutation {
createShop(
name: String
email: String
location: ShopLocationInput
): ShopResponse
}
推荐阅读
- java - 可以用不公开的 getter 和 setter 方法在 Java 中实现封装吗?
- c++ - 使用 MPI 为每个等级创建数组
- python - 减少科学计数法中的小数位数
- python - 点不在正确的位置 MatPlotLib pyplot.plot()
- html - 有双边框的表格
- excel - 这个excel vba代码Sheet.Range.End().Row有什么意义?
- python - 如何在 Python 中的除了 AssertionError 之外的 while 循环中使用 continue
- vue.js - VueJS 向组件发出工作但出现错误无法读取应用属性
- php - 设置 CURL 以接收来自 API 的 POST 响应
- excel - VBA:Sub Workbook_BeforeClose 用于删除数据