Hi there! I’m Vikas Sankhla, your full-stack buddy from Web Codder 👋.
Today, we’re going to dive into a magical tool called GraphQL. It’s like a super-smart waiter who brings you exactly what you ask for — not more, not less! 🍽️
By the end of this article, you’ll understand:
What GraphQL is
How it works
Why it’s different from REST APIs
How to set up your own GraphQL server
How to write GraphQL queries, mutations, and more!
Let’s get started! 🛠️
🧠 What is GraphQL?
GraphQL is a way to get data from your server.
It was made by Facebook in 2015.
Instead of asking for everything, GraphQL lets you ask for exactly what you need.
jsCopyEdit// index.js
const { ApolloServer, gql } = require('apollo-server');
const typeDefs = gql`
type Query {
hello: String
}
`;
const resolvers = {
Query: {
hello: () => "Hello Web Codder Family! 👋",
},
};
const server = new ApolloServer({ typeDefs, resolvers });
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});
Output
Go to the URL and run this query:
graphqlCopyEdit{
hello
}
🎉 You’ll see: "Hello Web Codder Family! 👋"
✍️ Writing Queries and Mutations
Let’s expand our schema:
jsCopyEditconst typeDefs = gql`
type User {
id: ID!
name: String!
}
type Query {
getUser(id: ID!): User
}
type Mutation {
createUser(name: String!): User
}
`;
Vikas Sankhla is a seasoned Full Stack Developer with over 7 years of experience in web development. He is the founder of Web Codder, a platform dedicated to providing comprehensive web development tutorials and resources. Vikas specializes in the MERN stack (MongoDB, Express.js, React.js, Node.js) and has been instrumental in mentoring aspiring developers through his online courses and content. His commitment to simplifying complex web technologies has made him a respected figure in the developer community.