node.js - 如何接收数组作为 GraphQL 服务的输入参数的成员?
问题描述
鉴于此架构:
input TodoInput {
id: String
title: String
}
input SaveInput {
nodes: [TodoInput]
}
type SavePayload {
message: String!
}
type Mutation {
save(input: SaveInput): SavePayload
}
鉴于此解析器:
type TodoInput = {
id: string | null,
title: string
}
type SaveInput = {
nodes: TodoInput[];
}
type SavePayload = {
message: string;
}
export const resolver = {
save: (input: SaveInput): SavePayload => {
input.nodes.forEach(todo => api.saveTodo(todo as Todo));
return { message : 'success' };
}
}
当我发送此请求时:
mutation {
save(input: {
nodes: [
{id: "1", title: "Todo 1"}
]
}) {
message
}
}
然后值input.nodes
是undefined
在服务器端。
有人知道我在做什么错吗?
有用信息:
- 突变适用于标量值(例如作为输入和返回的字符串)
- 我正在使用 typescript、express 和 express-graphql。
解决方案
您需要key
在解析器中进行更改,
export const resolver = {
save: (args: {input: SaveInput}): SavePayload => {
args.input.nodes.forEach(todo => api.saveTodo(todo as Todo));
return { message : 'success' };
}
}
推荐阅读
- c# - 正确使用正则表达式提取单词
- lstm - “numpy.ndarray”对象的错误没有属性“append”
- python - 如何将您的 Tinder data.json 转换为 CSV
- python - 查找列表中元素的索引
- python - 如何将安装库 pip 安装到 Azure Web 应用程序上的“antenv”venv?
- apache-flink - 更改在纱线上运行的 flink 作业的日志文件输出路径
- python - How to create new column with parts of the sentence from another column of pandas dataframe?
- javascript - 如何在 TypeScript 中表示包含多种类型的二维数组?
- azure-cognitive-services - 如何强制认知服务返回错误
- performance - WP 网站在 000webhost.com 上加载超过一分钟工作正常