Resolver Anatomy
Let’s find out how a resolver looks when implemented.
Example
In the below example, we will create a resolver inside a Query object and we will name it `getUser`. The resolver takes in 4 arguments, as mentioned above. Inside the resolver, we write the logic to fetch the data from the database, and return that data in the required format. For the sake of this example, we will return dummy data to the client.
Javascript
const resolvers = { Query: { getUser(parent, args, context, info) { return { "id" : "1" , "name: " GFG: } } } }; |
Resolvers in GraphQL
Resolvers are a crucial part of GraphQL that determines how data is fetched and returned in response to a query. They act as the bridge between the client’s request and the data source, whether it’s a database, API, or any other data store. Resolvers are responsible for fetching the data for each field in a query and transforming it into the format expected by the client. In this article, we will learn about Resolvers along with an understanding of How Resolvers Work with examples and so on.