/
index.js
64 lines (54 loc) · 1.27 KB
/
index.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const express = require('express');
const { graphqlHTTP } = require('express-graphql');
const {
GraphQLSchema ,
GraphQLObjectType ,
GraphQLString ,
GraphQLList ,
GraphQLInt ,
GraphQLNonNull ,
} = require('graphql');
const app = express();
const PORT = 5000;
const books = [
{ id : 0 , title : "Sandhu is here" } ,
{ id : 1 , title : "Sandhu is here" } ,
{ id : 2 , title : "Sandhu is here" } ,
{ id : 3 , title : "Sandhu is here" } ,
{ id : 4 , title : "Sandhu is here" } ,
{ id : 5 , title : "Sandhu is here" } ,
]
const BookType = new GraphQLObjectType({
name : "Book" ,
description : "dv" ,
fields : () => ({
id : { type : GraphQLNonNull(GraphQLInt) } ,
title : { type : GraphQLNonNull(GraphQLString) } ,
})
})
const RootQueryType = new GraphQLObjectType({
name : "Query" ,
description : "Root Query" ,
fields : () => ({
books : {
type : new GraphQLList(BookType) ,
description : "List of Books" ,
resolve : (book) => books
}
})
});
const schema = new GraphQLSchema({
query : RootQueryType ,
});
app.use('/api' , graphqlHTTP({
schema : schema ,
graphiql : true ,
}));
async function main(argument) {
try {
app.listen(PORT , () => console.log("Server is running at "+PORT));
} catch(e) {
console.log(e);
}
}
main();