2

Let's say my query looks like this:

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

And resolver map looks like this:

{
  Query: {
    post: myPostResolverFunc,
  }
}

How I can add additional "nested" resolver for post.user? I tried this but it does not work:

addResolveFunctionsToSchema(schema, {
  Query: {
    post: {
      user: postUserResolveFunc,
    },
  }
});
user606521
  • 14,486
  • 30
  • 113
  • 204

1 Answers1

3

You just have to write a resolver for your field. Assuming your schema is something like this :

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
}

You can write resolvers like this :

addResolveFunctionsToSchema(schema, {
  Post: {
    user(root) {
      return getUserById(root.user)
    }
  }
  Query: {
    post(root, args, context) {
      return getPostById(args.id)
    }
  }
});
cefn
  • 2,895
  • 19
  • 28
Pierre Criulanscy
  • 8,726
  • 3
  • 24
  • 38