代码之家  ›  专栏  ›  技术社区  ›  Saulo

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

  •  1
  • Saulo  · 技术社区  · 8 年前

    鉴于此模式:

    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。
    1 回复  |  直到 8 年前
        1
  •  3
  •   Shivam Pandey    8 年前

    你需要在 key 在分解器中,

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