Skip to content

Commit 53720e6

Browse files
committed
feat: add graphql explanation
1 parent f488009 commit 53720e6

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

advanced/graphql.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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!

0 commit comments

Comments
 (0)