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