How to read GrqphQL resolver

42 Views Asked by At

While I try to learn GraphQL-Tools.I found this article.In this article,example resolvers are described as follows.

const resolvers = {
  Query: {
    posts: () => posts,
    author: (_, { id }) => find(authors, { id })
  },

  Mutation: {
    upvotePost: (_, { postId }) => {
      const post = find(posts, { id: postId })
      if (!post) {
        throw new Error(`Couldn't find post with id ${postId}`)
      }
      post.votes += 1
      return post
    }
  },

  Author: {
    posts: author => filter(posts, { authorId: author.id })
  },

  Post: {
    author: post => find(authors, { id: post.authorId })
  }
}

I understood that Queryand MutationandSubscriptionis main function of Graphql resolvers. My question is what is Author,Post in this resolver ? I can understand QueryandMutationsection of this resolver. but how to think about Authorand Post?

It seems that it defined functions.

Thanks

1

There are 1 best solutions below

0
On

That is Trival Resolver https://graphql.org/learn/execution/#trivial-resolvers

Until Graphql has a completely understandable return value.
Graphql goes on to the next search with the returned type.
Those functions [Author, Post] tell to Graphql how to find the object's value.

For exameple if "PostFindById" reoslver promised to graphql that will return "author" field But instead it return "Post" Graphql will excute Trival Rsolver to find value of author field with given "Post".

So it will execute this resolver with given 'post'

 Post: {
    author: post => find(authors, { id: post.authorId })
  }