-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
229 lines (215 loc) · 6.23 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
const express = require('express');
const server = express();
const PORT = process.env.PORT || 3000;
const graphqlHTTP = require('express-graphql');
const {
GraphQLSchema,
GraphQLObjectType,
GraphQLNonNull,
GraphQLID,
GraphQLString,
GraphQLInt,
} = require('graphql');
const dataHandler = require('./src/data/index');
const {
globalIdField,
connectionDefinitions,
connectionFromPromisedArray,
connectionArgs,
mutationWithClientMutationId,
} = require('graphql-relay');
const { nodeInterface, nodeField } = require('./src/node');
const departmentType = new GraphQLObjectType({
name: 'Department',
description: 'A department in grocery store',
fields: {
id: globalIdField(),
name: {
type: GraphQLString,
description: 'The name of the department'
},
description: {
type: GraphQLString,
description: 'Description of the department'
},
created_at: {
type: GraphQLString,
description: 'Date on which the department was created'
},
updated_at: {
type: GraphQLString,
description: 'Date on which the department was updated'
}
},
interfaces: [nodeInterface]
});
exports.departmentType = departmentType;
const userType = new GraphQLObjectType({
name: 'User',
description: 'A user account',
fields: {
id: globalIdField(),
name: {
type: GraphQLString,
description: 'the name of the user'
},
favs: {
type: GraphQLString,
description: "a list of the user's favourite items"
},
grocery_id: {
type: GraphQLID,
description: 'groceries linked to the user'
},
ingredient_id: {
type: GraphQLID,
description: 'ingredients linked to the user'
}
},
interfaces: [nodeInterface]
});
exports.userType = userType;
const { connectionType: departmentConnection } = connectionDefinitions({
nodeType: departmentType,
connectionFields: () => ({
totalCount: {
type: GraphQLInt,
description: 'Total number of departments in this connection.',
resolve: (connection) => {
return connection.edges.length;
}
}
})
});
const { connectionType: userConnection } = connectionDefinitions({
nodeType: userType,
connectionFields: () => ({
totalCount: {
type: GraphQLInt,
description: 'Total number of users in this connection',
resolve: (connection) => {
return connection.edges.length;
}
}
})
});
const queryType = new GraphQLObjectType({
name: 'QueryType',
description: 'Root query type',
fields: {
node: nodeField,
departments: {
type: departmentConnection,
args: connectionArgs,
resolve: (_, args) => connectionFromPromisedArray(
dataHandler.showType.showAllDepartments(),
args
)
},
department: {
type: departmentType,
args: {
id: {
type: new GraphQLNonNull(GraphQLID),
description: 'The id of the department'
}
},
resolve: (_, args) => {
return dataHandler.getTypeById.getDepartmentById(args.id);
}
},
users: {
type: userConnection,
args: connectionArgs,
resolve: (_, args) => connectionFromPromisedArray(
dataHandler.showType.showAllUsers(),
args
)
},
user: {
type: userType,
args: {
id: {
type: new GraphQLNonNull(GraphQLID),
description: 'The id of the user'
}
},
resolve: (_, args) => {
return dataHandler.getTypeById.getDepartmentById(args.id);
}
}
}
});
const departmentMutation = mutationWithClientMutationId({
name: 'AddDepartment',
inputFields: {
name: {
type: GraphQLString,
description: 'The name of the department'
},
description: {
type: GraphQLString,
description: 'Description of the department'
}
},
outputFields: {
department: {
type: departmentType
}
},
mutateAndGetPayload: (args) => new Promise((resolve, reject) => {
Promise.resolve(dataHandler.createType.createDepartment(args))
.then((department) => resolve({ department }))
.catch(reject);
})
});
const userMutation = mutationWithClientMutationId({
name: 'addUser',
inputFields: {
name: {
type: GraphQLString,
description: 'The name of the user'
},
favs: {
type: GraphQLString,
description: 'User favourites'
},
grocery_id: {
type: GraphQLID,
description: 'User groceries'
},
ingredient_id: {
type: GraphQLID,
description: 'User ingredients'
}
},
outputFields: {
user: {
type: userType
}
},
mutateAndGetPayload: (args) => new Promise((resolve, reject) => {
Promise.resolve(dataHandler.createType.createUser(args))
.then((user) => resolve({ user }))
.catch(reject);
})
});
const mutationType = new GraphQLObjectType({
name: 'Mutation',
description: 'Root mutation type',
fields: {
createDepartment: departmentMutation,
createUser: userMutation
}
});
const schema = new GraphQLSchema({
query: queryType,
mutation: mutationType
});
server.use('/graphql', graphqlHTTP({
schema,
graphiql: true
}));
server.listen(PORT, () => {
console.log(`Listening on http://localhost:${PORT}/graphql`);
});