Hello Everyone, in this part of the GraphQL series, I will discuss resolvers.
Resolvers
They are functions that define how to retrieve or manipulate data for each field in a GraphQL schema. Resolvers are a crucial part of GraphQL server implementations and are responsible for fetching data from various sources, such as databases, APIs, or other services, and returning the requested data.
When a client sends a GraphQL query, the server matches the fields in the query to the corresponding resolvers. Each field in the schema has an associated resolver function that determines how to resolve the data for that field.
Creating a resolver-
- In the previous part of this series, you have created a folder called "schema", inside that folder, create another file named "resolvers,js" and paste this code there.
const { fakeData } = require("../FakeData")
const lodesh = require("lodash")
const resolvers = {
Query: {
// Get All users
users() {
return fakeData
},
// Get user by ID
user(parent, args) {
const id = args.id
const user = lodesh.find(fakeData, { id: Number(id) })
return user
},
// Get user by Name
userByName(parent, args) {
const name = args.name
const user = lodesh.find(fakeData, { name })
return user
},
},
Mutation: {
createUser(parent,args){
const user = args.newUser
user.id = Math.floor(Math.random() * 999999)
fakeData.push(user)
return user
},
updateUser(parent,args){
const {id,name,role,age,isEmployee} = args.updatedUser
let updateUserName;
fakeData.forEach(item => {
if(item.id == id){
item.name = name
item.age = age
item.role = role
item.isEmployee = isEmployee
updateUserName = item
}
})
return updateUserName
},
deleteUser(parent,args){
const {id} = args.delUser
lodesh.remove(fakeData,(user) => user.id === Number(id))
return null
},
}
}
module.exports = { resolvers }
- Here we have 2 Properties Query, which is used for data fetching, and Mutation which is used for manipulating data like adding, updating, or deleting, there properties have handler functions for our handlers created in typeDefs.
args will have all the properties which are defined in the typeDefs schema file.
Also Create a file named fakeData.js and add this data to it.
//fakeData.js
const fakeData = [
{
id: 101,
name: "User 1",
age: 24,
isEmployee: true,
role: "WebDeveloper",
friends: [
{
id: 102,
name: "User 2",
age: 21,
isEmployee: true,
role: "Tester"
},
{
id: 103,
name: "User 3",
age: 20,
isEmployee: false,
role: "SoftwareEngineer"
},
{
id: 104,
name: "User 4",
age: 27,
isEmployee: true,
role: "WebDeveloper"
}
]
},
{
id: 102,
name: "User 2",
age: 21,
isEmployee: true,
role: "Tester",
friends: [
{
id: 103,
name: "User 3",
age: 20,
isEmployee: false,
role: "SoftwareEngineer"
},
{
id: 104,
name: "User 4",
age: 27,
isEmployee: true,
role: "WebDeveloper"
}
]
},
{
id: 103,
name: "User 3",
age: 20,
isEmployee: false,
role: "SoftwareEngineer",
friends: [
{
id: 104,
name: "User 4",
age: 27,
isEmployee: true,
role: "WebDeveloper"
}
]
},
{
id: 104,
name: "User 4",
age: 27,
isEmployee: true,
role: "WebDeveloper",
friends: [
{
id: 103,
name: "User 3",
age: 20,
isEmployee: false,
role: "SoftwareEngineer"
}
]
}
];
module.exports = { fakeData }
- Fake Dataset to manipulate GraphQL handler functions.
Now run this command -
npm run start
- It will start the server at localhost:4000 with GraphQL UI, where you can check your API responses
This is how you can create a Resolver for your GraphQL API. In the Next Part of this Series, I will be discussing Mutation, which will help in creating, updating, or deleting data from the dataset.
CREDITS - https://www.youtube.com/results?search_query=pedrotech
THANK YOU FOR CHECKING THIS POST
You can contact me on -
Instagram - https://www.instagram.com/supremacism__shubh/
LinkedIn - https://www.linkedin.com/in/shubham-tiwari-b7544b193/
Email - shubhmtiwri00@gmail.com
^^You can help me with some donation at the link below Thank youππ ^^
β --> https://www.buymeacoffee.com/waaduheck <--
Also check these posts as well
https://dev.to/shubhamtiwari909/website-components-you-should-know-25nm
https://dev.to/shubhamtiwari909/smooth-scrolling-with-js-n56
https://dev.to/shubhamtiwari909/swiperjs-3802
https://dev.to/shubhamtiwari909/custom-tabs-with-sass-and-javascript-4dej
Top comments (0)