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.