Related Post
इस Tutorial में, हम GraphQL के लिएenvironmental setup के बारे में जानेंगे। इस Tutorial में Example को execute करने के लिए आपको निम्नलिखित की आवश्यकता होगी -
How to Build a GraphQL server with Nodejs
हम Nodejs के साथ GraphQL Server Create करने के लिए एक step-wise approach से गुजरेंगे जैसा कि नीचे दिखाया गया है -
NodeJs installing करने के बाद, terminal पर निम्नलिखित commands का उपयोग करके Node और npm के Version को verify करें -
C:\Users\Admin>node -v
v12.16.1
C:\Users\Admin>npm -v
6.13.4
Project के Root Folder को Test App नाम दिया जा सकता है
नीचे दिए गए instructions का उपयोग करके visual studio code editor का उपयोग करके Folder खोलें -
C:\Users\Admin>mkdir test-app
C:\Users\Admin>cd test-app
C:\Users\Admin\test-app>code.
एक package.json Filew बनाएँ जिसमें GraphQL Server Application की सभी dependencies होंगी।
नीचे दी गई Command का उपयोग करके dependencies Install करें -
C:\Users\Admin\test-app>npm install
इस step में, हम Data को Store और retrieve करने के लिए flat files का उपयोग करते हैं। एक Folder Data बनाएं और दो Files Students.json और Colleges.json जोड़ें।
Following is the colleges.json file −
Following is the students.json file −
हमें एक Datastore बनाने की आवश्यकता है जो Data Folder contents को लोड करता है। इस मामले में, हमें collection variables, Students और Colleges की Need है। जब भी Application को Data की आवश्यकता होती है, तो वह इन collection variables का उपयोग करता है।
Create file db.js with in the project folder as follows −
Step 6 − Create Schema File, schema.graphql
current project folder में एक Schema File बनाएँ और निम्नलिखित contents जोड़ें
current project folder में एक resolver File बनाएँ और निम्नलिखित contents जोड़ें -
एक Server File बनाएँ और graphql को निम्नानुसार configure करें -
Run the command npm start as given below −
C:\Users\Admin\test-app>npm start
Run http://localhost:9000/graphiql. URL And Type the following query in the editor
The response from the server is given below −