File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed
Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change 1+ // 📌 JavaScript Advanced - Introduction to GraphQL
2+
3+ // Welcome to the GraphQL section of the JavaScript Advanced tutorial!
4+ // Here, you'll learn how to use GraphQL as an alternative to REST APIs.
5+
6+ // Installing GraphQL and Express
7+ // Run the following command in your Node.js project:
8+ // npm install express graphql express-graphql
9+
10+ const express = require ( "express" ) ;
11+ const { graphqlHTTP } = require ( "express-graphql" ) ;
12+ const { buildSchema } = require ( "graphql" ) ;
13+
14+ // Defining a GraphQL Schema
15+ const schema = buildSchema ( `
16+ type Query {
17+ message: String
18+ }
19+ ` ) ;
20+
21+ // Root Resolver
22+ const root = {
23+ message : ( ) => "Hello, GraphQL!" ,
24+ } ;
25+
26+ // Setting Up an Express Server with GraphQL
27+ const app = express ( ) ;
28+ app . use (
29+ "/graphql" ,
30+ graphqlHTTP ( {
31+ schema : schema ,
32+ rootValue : root ,
33+ graphiql : true , // Enables GraphiQL UI
34+ } )
35+ ) ;
36+
37+ app . listen ( 4000 , ( ) => {
38+ console . log ( "GraphQL server running at http://localhost:4000/graphql" ) ;
39+ } ) ;
40+
41+ // 💡 GraphQL allows flexible querying, reducing over-fetching and under-fetching of data!
You can’t perform that action at this time.
0 commit comments