- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
Latest commit
143 lines (135 loc) · 3.59 KB
/
Copy pathserver.js
File metadata and controls
143 lines (135 loc) · 3.59 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
const{
GraphQLSchema,
GraphQLObjectType,
GraphQLString,
GraphQLList,
GraphQLInt,
GraphQLNonNull,
}=require("graphql");
constauthors=[
{id: 1,name: "J. K. Rowling"},
{id: 2,name: "J. R. R. Tolkien"},
{id: 3,name: "Brent Weeks"},
];
constbooks=[
{id: 1,name: "Harry Potter and the Chamber of Secrets",authorId: 1},
{id: 2,name: "Harry Potter and the Prisoner of Azkaban",authorId: 1},
{id: 3,name: "Harry Potter and the Goblet of Fire",authorId: 1},
{id: 4,name: "The Fellowship of the Ring",authorId: 2},
{id: 5,name: "The Two Towers",authorId: 2},
{id: 6,name: "The Return of the King",authorId: 2},
{id: 7,name: "The Way of Shadows",authorId: 3},
{id: 8,name: "Beyond the Shadows",authorId: 3},
];
constexpress=require("express");
require("dotenv").config();
const{ graphqlHTTP }=require("express-graphql");
constapp=express();
constport=process.env.PORT||5000;
constRootMutationType=newGraphQLObjectType({
name: "Mutation",
description: "Root Mutation",
fields:()=>({
addBook:{
type:BookType,
description:'Add a book',
args:{
name:{type: GraphQLNonNull(GraphQLString)},
authorId:{type: GraphQLNonNull(GraphQLInt)}
},
resolve:(parent,args)=>{
constbook={id:books.length+1,name:args.name,authorId:args.authorId}
books.push(book)
returnbook
}
},
addAuthor:{
type:AuthorType,
description:'Add an author',
args:{
name:{type: GraphQLNonNull(GraphQLString)},
},
resolve:(parent,args)=>{
constauthor={id:authors.length+1,name:args.name,}
authors.push(author)
returnauthor
}
},
})
});
constBookType=newGraphQLObjectType({
name: "Book",
description: "This represents a books written by the an autor",
fields: ()=>({
id: {type: GraphQLNonNull(GraphQLInt)},
name: {type: GraphQLNonNull(GraphQLString)},
author: {
type: AuthorType,
resolve: (book)=>{
returnauthors.find((author)=>author.id===book.authorId);
},
},
}),
});
constAuthorType=newGraphQLObjectType({
name: "Author",
description: "This represendts a author",
fields: ()=>({
id: {type: GraphQLNonNull(GraphQLInt)},
name: {type: GraphQLNonNull(GraphQLString)},
books: {
type: newGraphQLList(BookType),
resolve: (author)=>{
returnbooks.filter((book)=>book.authorId===author.id);
},
},
}),
});
constRootQueryType=newGraphQLObjectType({
name: "Query",
description: "Root Query",
fields: ()=>({
book: {
type: BookType,
description: "Single Book",
args: {
id: {
type: GraphQLInt,
},
},
resolve: (parent,args)=>books.find((book)=>book.id==args.id),
},
author: {
type: AuthorType,
description: "single Author",
args: {
id: {
type: GraphQLInt,
},
},
resolve: (parent,args)=>authors.find((author)=>author.id==args.id),
},
books: {
type: newGraphQLList(BookType),
description: "List of great books",
resolve: ()=>books,
},
authors: {
type: newGraphQLList(AuthorType),
description: "List of great Authors",
resolve: ()=>authors,
},
}),
});
constschema=newGraphQLSchema({
query: RootQueryType,
mutation:RootMutationType
});
app.use(
"/graphql",
graphqlHTTP({
schema: schema,
graphiql: true,
})
);
app.listen(port,()=>console.log(`Server is listening on port ${port}`));