2

假设我的查询如下所示:

query {
  post {
    id
    user { id, name } 
  }
}

解析器映射如下所示:

{
  Query: {
    post: myPostResolverFunc,
  }
}

如何为 post.user 添加额外的“嵌套”解析器?我试过这个但它不起作用:

addResolveFunctionsToSchema(schema, {
  Query: {
    post: {
      user: postUserResolveFunc,
    },
  }
});
4

1 回答 1

3

你只需要为你的领域写一个解析器。假设您的架构是这样的:

type Post {
  id: ID!,
  user: User
}

type User {
  id: ID!,
  username: String!
}

type Query {
  post(id: ID!): Post  #assuming you want to request a simple post here
}

您可以像这样编写解析器:

addResolveFunctionsToSchema(schema, {
  Post: {
    user(root) {
      return getUserById(root.user)
    }
  }
  Query: {
    post(root, args, context) {
      return getPostById(args.id)
    }
  }
});
于 2017-03-22T15:01:22.410 回答