-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
156 lines (147 loc) · 4.43 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
147
148
149
150
151
152
153
154
155
156
const express = require('express')
const { graphqlHTTP } = require('express-graphql');
const {
GraphQLObjectType,
GraphQLString,
GraphQLInt,
GraphQLNonNull,
GraphQLList,
GraphQLSchema
} = require('graphql')
const app = express()
var Owners = [
{ id: 1, name: 'John Astle' },
{ id: 2, name: 'Gautam Sharma' },
{ id: 3, name: 'Kane Williamson' }
]
var Websites = [
{ id: 1, name: 'Facebook', ownerId: 1 },
{ id: 2, name: 'Google', ownerId: 2 },
{ id: 3, name: 'Amazon', ownerId: 3 },
{ id: 4, name: 'Github', ownerId: 1 },
{ id: 5, name: 'Medium', ownerId: 2 },
{ id: 6, name: 'Baidu', ownerId: 3 },
{ id: 7, name: 'Zapak', ownerId: 1 },
{ id: 8, name: 'Cricinfo', ownerId: 2 }
]
const WebsiteType = new GraphQLObjectType({
name: 'Website',
description: 'This represents a website made by a Owner(Programmer)',
fields: () => ({
id: { type: GraphQLInt },
name: { type: GraphQLString },
ownerId: { type: GraphQLInt },
})
})
const OwnerType = new GraphQLObjectType({
name: 'Owner',
description: 'This represents a owner of a website',
fields: () => ({
id: { type: GraphQLInt },
name: { type: GraphQLString },
})
})
const RootQueryType = new GraphQLObjectType({
name: 'Query',
description: 'Root Query',
fields: () => ({
websites: {
type: new GraphQLList(WebsiteType),
description: 'List of All Websites',
resolve: () => Websites
},
owners: {
type: new GraphQLList(OwnerType),
description: 'List of All Owners',
resolve: () => Owners
}
})
})
const RootMutationType = new GraphQLObjectType({
name: 'Mutation',
description: 'Root Mutation',
fields: () => ({
addWebsite: {
type: WebsiteType,
description: 'Add a website',
args: {
name: { type: GraphQLString },
ownerId: { type: GraphQLInt }
},
resolve: (parent, args) => {
const website = { id: Websites.length + 1, name: args.name,ownerId:args.ownerId }
Websites.push(website)
return website
}
},
removeWebsite: {
type: WebsiteType,
description: 'Remove a Website',
args: {
id: { type: new GraphQLNonNull(GraphQLInt) }
},
resolve: (parent, args) => {
Websites = Websites.filter(website => website.id !== args.id)
return Websites[args.id]
}
},
addOwner: {
type: OwnerType,
description: 'Add an Owner',
args: {
name: { type: GraphQLString }
},
resolve: (parent, args) => {
const owner = { id: Owners.length + 1, name: args.name }
Owners.push(owner)
return owner
}
},
removeOwner: {
type: OwnerType,
description: 'Remove an Owner',
args: {
id: { type: new GraphQLNonNull(GraphQLInt) }
},
resolve: (parent, args) => {
Owners = Owners.filter(owner => owner.id !== args.id)
return Owners[args.id]
}
},
updateOwner: {
type: OwnerType,
description: 'Update an Owner',
args: {
id: { type: new GraphQLNonNull(GraphQLInt) },
name:{type:new GraphQLNonNull(GraphQLString)}
},
resolve: (parent, args) => {
Owners[args.id - 1].name = args.name
return Owners[args.id - 1]
}
},
updateWebsite: {
type: WebsiteType,
description: 'Update a Website',
args: {
id: { type: new GraphQLNonNull(GraphQLInt) },
name:{type:new GraphQLNonNull(GraphQLString)},
ownerId:{type:new GraphQLNonNull(GraphQLInt)}
},
resolve: (parent, args) => {
Websites[args.id - 1].name = args.name
Websites[args.id - 1].ownerId = args.ownerId
return Websites[args.id - 1]
}
},
})
})
const schema = new GraphQLSchema({
query: RootQueryType,
mutation: RootMutationType
})
app.use('/graphql', graphqlHTTP({
graphiql: true,
schema: schema
}))
app.listen(5000, () => console.log('Server Running'))