如何接收数组作为GraphQL服务的输入参数的成员?

时间:2018-10-31 05:44:51

标签: node.js express graphql express-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

有人知道我在做什么错吗?

有用的信息:

  • 该突变与标量值(例如将String作为输入和返回值)一起正常工作
  • 我正在使用打字稿,express和express-graphql。

1 个答案:

答案 0 :(得分:3)

您需要在解析器的key中进行更改

export const resolver = {
  save: (args: {input: SaveInput}): SavePayload => {
    args.input.nodes.forEach(todo => api.saveTodo(todo as Todo));
    return { message : 'success' };
  }
}
相关问题