-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
146 lines (138 loc) · 4.05 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
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
144
145
146
const express = require("express");
const expressGraphQL= require("express-graphql");
const {GraphQLSchema, GraphQLObjectType, GraphQLList,GraphQLInt, GraphQLNonNull, GraphQLString} = require("graphql");
const app=express();
const authors = [
{ id: 1, name: 'J. K. Rowling' },
{ id: 2, name: 'J. R. R. Tolkien'},
{ id: 3, name: 'Brent Weeks' }
]
const books =[
{id:1, name: "My first book", authorID: 1},
{id:2, name: "My second book", authorID: 1},
{id:3, name: "My third book", authorID: 1},
{id:4, name: "My forth book", authorID: 2},
{id:5, name: "My fifth book", authorID: 2},
{id:6, name: "My sixth book", authorID: 2},
{id:7, name: "My seventh book", authorID: 3},
{id:8, name: "My eighth book", authorID: 3},
]
// const schema=new GraphQLSchema({
// query: new GraphQLObjectType({
// name: 'MyFirstQuery',
// fields:()=>({
// message: {
// type: GraphQLString,
// resolve: ()=>'Hello World'
// },
// context:{
// type: GraphQLString,
// resolve: ()=> "literature"
// }
// //resolve is a function which will tell where to get the data from
// })
// })
// })
const AuthorType= new GraphQLObjectType({
name:"Author",
description:"This represents an author",
fields: () =>({
id: {type: GraphQLNonNull(GraphQLInt)},
name: {type: GraphQLNonNull(GraphQLString)},
books:{
type: new GraphQLList(BookType),
resolve: (author)=>{
return books.filter(book => book.authorID=== author.id)
}
}
})
})
const BookType= new GraphQLObjectType({
name:"Book",
description:"This represents a book",
fields: () =>({
id: {type: GraphQLNonNull(GraphQLInt)},
name: {type: GraphQLNonNull(GraphQLString)},
authorID: {type: GraphQLNonNull(GraphQLInt)},
author: {
type: AuthorType,
resolve:(book)=>{
return authors.find(author => author.id === book.authorID)
}
}
})
})
const RootMutationType = new GraphQLObjectType({
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)=>{
const book={id:books.length+1, name:args.name, authorID: args.authorID}
books.push(book)
return book
}
},
addAuthor:{
type: AuthorType,
description:"Add an Author",
args:{
name: {type: GraphQLNonNull(GraphQLString)}
},
resolve: (parent, args)=>{
const author={id:authors.length+1, name:args.name}
authors.push(author)
return author
}
}
})
})
const rootQueryType= new GraphQLObjectType({
name: "Query",
description: 'Root Query',
fields: ()=>({
book:{
type: BookType,
description: "A single book",
args:{
id: {type: GraphQLInt}
},
resolve:(parent, args)=> books.find(book => book.id === args.id)
},
books:{
type: new GraphQLList(BookType),
description: "List of books",
resolve: ()=> books
},
author:{
type: AuthorType,
description: "A single author",
args:{
id: {type: GraphQLInt}
},
resolve:(parent,args)=> authors.find(author => author.id=== args.id)
},
authors:{
type: new GraphQLList(AuthorType),
description: "List of authors",
resolve: ()=> authors
}
})
})
const schema= new GraphQLSchema({
query: rootQueryType,
mutation: RootMutationType
})
app.use('/graphql',expressGraphQL.graphqlHTTP({
schema: schema,
graphiql: true
}))
app.listen(3000,()=>{
console.log("Server is running");
})