GraphQl tutorial in hindi

 GraphQL Tutorial in Hindi

GraphQL एक शक्तिशाली और लचीला डेटा query language है जो आपको API से डेटा प्राप्त करने और म्यूटेट करने के लिए एक नया तरीका प्रदान करता है। यह REST API के मुकाबले अधिक flexible और efficient है। GraphQL के द्वारा आप API से केवल वही डेटा प्राप्त कर सकते हैं जिसकी आपको जरूरत हो, बिना अतिरिक्त डेटा के। GraphQL को Facebook ने 2012 में विकसित किया था और 2015 में इसे open-source किया गया था।

इस ट्यूटोरियल में हम GraphQL के बारे में विस्तार से समझेंगे और इसे कैसे implement किया जा सकता है, इसके बारे में चर्चा करेंगे।

GraphQL क्या है?

GraphQL एक query language है जो आपको API से data retrieve करने के लिए precise queries बनाने की सुविधा देता है। साथ ही, यह एक runtime environment भी है जो API के data को execute करता है। इसमें दो मुख्य concepts हैं:

  1. Queries: Data retrieve करने के लिए।
  2. Mutations: Data को modify करने के लिए।

इसके अलावा, GraphQL में Schemas और Resolvers का भी महत्व है।

GraphQL की विशेषताएँ (Features of GraphQL)

  1. Flexible Queries:

    • GraphQL में client को control होता है कि वह API से कौन सा डेटा चाहे। एक ही query में आप यह specify कर सकते हैं कि किस field या attribute की आपको आवश्यकता है।
  2. Single Endpoint:

    • GraphQL APIs केवल एक ही endpoint से काम करती हैं, unlike REST APIs जो multiple endpoints पर आधारित होती हैं।
  3. Strongly Typed Schema:

    • GraphQL APIs में schema define होता है जो आपके data के types को validate करता है, जिससे data consistency और reliability बनी रहती है।
  4. Real-time updates:

    • GraphQL Subscriptions का support करता है, जिससे आपको real-time updates मिल सकते हैं (जैसे chat applications या live score updates)।

GraphQL के प्रमुख concepts

  1. Schemas:

    • Schema वह structure है जिसमें सारे types, queries, mutations और subscriptions define किए जाते हैं।
    • Schema define करता है कि API से किस तरह का data निकल सकता है, उसकी types क्या होंगी, और आप किस प्रकार के operations perform कर सकते हैं।
  2. Queries:

    • Queries का उपयोग data को fetch करने के लिए किया जाता है। एक query के तहत आप यह specify कर सकते हैं कि आपको कौन सा डेटा चाहिए। आप एक ही query में अलग-अलग data requests कर सकते हैं।
  3. Mutations:

    • Mutations का उपयोग data को modify करने के लिए किया जाता है। इसका use तब होता है जब आपको data insert, update, या delete करना हो।
  4. Resolvers:

    • Resolvers GraphQL के functions होते हैं जो queries और mutations का response देते हैं। ये functions server पर implement किए जाते हैं और API से data प्राप्त करते हैं।
  5. Subscriptions:

    • Subscriptions का use real-time data updates के लिए किया जाता है। यह client को किसी specific event या change के लिए notify करता है। जैसे live chat या notifications।

GraphQL की तुलना REST API से

FeatureREST APIGraphQL
Data FetchingMultiple endpoints (specific data per endpoint)One endpoint, custom query (flexible data retrieval)
Over-fetchingCommon (you get more data than needed)No over-fetching (get only what you need)
Under-fetchingPossible (multiple requests needed for related data)No under-fetching (related data in a single request)
VersioningAPI versions required (v1, v2)No need for versioning (queries define the structure)
Real-timeNot supported by defaultReal-time updates through subscriptions

GraphQL का उपयोग कैसे करें

अब हम GraphQL का उपयोग करने के लिए कुछ basic steps देखेंगे। इस tutorial में हम Node.js और Express का उपयोग करेंगे, और Apollo Server के साथ GraphQL API बनाएंगे।

1. GraphQL API सेटअप करना

Step 1: Project Initialize करें

सबसे पहले, एक नया Node.js project initialize करें:

mkdir graphql-demo cd graphql-demo npm init -y
Step 2: आवश्यक dependencies इंस्टॉल करें

Apollo Server और GraphQL को इंस्टॉल करें:

npm install apollo-server-express express graphql
Step 3: Apollo Server और Express सेटअप करें

index.js फाइल बनाएं और Apollo Server को Express server में integrate करें।

const express = require('express'); const { ApolloServer, gql } = require('apollo-server-express'); // Define a GraphQL schema const typeDefs = gql` type Query { hello: String user(id: Int!): User } type User { id: Int name: String email: String } `; // Define resolvers to handle data for the schema const resolvers = { Query: { hello: () => 'Hello, world!', user: (_, { id }) => { const users = [ { id: 1, name: 'John Doe', email: 'john@example.com' }, { id: 2, name: 'Jane Smith', email: 'jane@example.com' }, ]; return users.find(user => user.id === id); } } }; // Create an Apollo server instance const server = new ApolloServer({ typeDefs, resolvers }); // Create an Express app and apply the Apollo server as middleware const app = express(); server.applyMiddleware({ app }); // Start the server app.listen(4000, () => { console.log('Server running at http://localhost:4000/graphql'); });

इस कोड में हमने एक GraphQL schema define किया है जिसमें एक Query type है, जिसमें एक hello और एक user query है। user query एक parameter (id) लेता है और user की जानकारी लौटाता है।

Step 4: Server को रन करें
node index.js

अब आपका server http://localhost:4000/graphql पर चल रहा है।

2. GraphQL Query Example

GraphQL के जरिए हम डेटा query करने के लिए नीचे दिए गए तरीके से query लिख सकते हैं:

# Simple query to get 'hello' message query { hello } # Query to fetch a specific user by ID query { user(id: 1) { name email } }

Response:

{ "data": { "hello": "Hello, world!" } } # For user query { "data": { "user": { "name": "John Doe", "email": "john@example.com" } } }

3. GraphQL Mutation Example

आप डेटा को mutate करने के लिए mutations का उपयोग कर सकते हैं। यहाँ एक mutation का उदाहरण दिया गया है:

mutation { createUser(name: "Alex", email: "alex@example.com") { id name email } }

Mutation के लिए resolver को define करना भी ज़रूरी है:


const resolvers = { Query: { // existing resolvers }, Mutation: { createUser: (_, { name, email }) => { const newUser = { id: Date.now(), name, email }; users.push(newUser); // Assume `users` is your user data array return newUser; } } };

4. GraphQL Subscription Example

Real-time data updates के लिए subscriptions का उपयोग किया जाता है:

subscription { userAdded { id name email } }

यहां एक simple subscription का resolver है:

const resolvers = { Subscription: { userAdded: { subscribe: () => pubsub.asyncIterator(["USER_ADDED"]), }, }, };

pubsub के माध्यम से आप event emit कर सकते हैं जैसे USER_ADDED


GraphQL के फायदे

  1. Efficient Data Fetching:

    • Client को वही डेटा मिल सकता है जिसकी उसे जरूरत है। इसे over-fetching और under-fetching की समस्या से बचाता है।
  2. Single Endpoint:

    • एक ही endpoint से सभी data queries और mutations होते हैं, जिससे server पर management आसान हो जाता है।
  3. Strong Typing:

    • GraphQL का schema बहुत स्पष्ट होता है, जिससे data structure और types को validate किया जा सकता है।
  4. Real-Time Data:

    • GraphQL subscriptions की मदद से real-time data updates का समर्थन करता है।
  5. Versionless API:

    • GraphQL में API versioning की आवश्यकता नहीं होती। क्लाइंट्स अपनी query के अनुसार डेटा प्राप्त कर सकते हैं।

निष्कर्ष

GraphQL REST API से कहीं ज्यादा लचीलापन और control प्रदान करता है। इसे सीखना और implement करना REST API के मुकाबले थोड़ा अलग हो सकता है, लेकिन यह छोटे और बड़े दोनों प्रकार के एप्लिकेशन्स में performance और efficiency को बढ़ाता है। ऊपर दिए गए उदाहरणों से आपको GraphQL के उपयोग के बेसिक्स को समझने में मदद मिलेगी।

Comments

Popular posts from this blog

PrimeNG tutorial with examples using frequently used classes

Docker and Kubernetes Tutorials and QnA

Building strong foundational knowledge in frontend development topics