-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.js
238 lines (230 loc) · 7.43 KB
/
schema.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
230
231
232
233
234
235
236
237
238
import { Meteor } from 'meteor/meteor';
import { Random } from 'meteor/random';
// import { PubSub } from 'graphql-subscriptions'; // PROD: Later use https://github.com/davidyaha/graphql-redis-subscriptions
import { asyncIterator } from 'apollo-live-server';
import GraphQLJSON, { GraphQLJSONObject } from 'graphql-type-json';
import { Items, Groups, ListsHead, ListsBody, OpenOrdersHead, OpenOrdersBody } from '/imports/db';
import listsQuery from '/imports/db/lists/queries/listsQuery';
import openOrdersQuery from '/imports/db/openorders/queries/openOrdersQuery';
// import { query } from "@kaviar/nova"; // https://github.com/kaviarjs/nova/blob/master/docs/index.md#graphql-integration
// export const pubsub = new PubSub();
export const USER_CHANGE_CHANNEL = 'user_changed';
export const typeDefs = [
`
scalar JSON
type Email {
address: String
verified: Boolean
}
type User {
emails: [Email]
_id: String,
username: String
}
type ListElement {
_id: ID,
row_id: Int,
itemId: ID,
item: JSON
}
type OpenOrderElement {
_id: ID,
list_id: ID,
row_id: Int,
itemId: ID,
unit: String,
item_amount: Float,
total_price: Float,
item: JSON
}
type Query {
user(userId: ID): User
listbody(listId: ID, limit: Int, skip: Int): [ListElement]
openorderbody(openOrderId: ID): [OpenOrderElement]
}
type Subscription {
user(userId: ID): SubscriptionEvent
listbody(listId: ID, limit: Int, skip: Int): SubscriptionEvent
openorderbody(openOrderId: ID): SubscriptionEvent
}
type SubscriptionEvent {
event: String,
doc: JSON
}
type MutationSuccess {
success: Boolean
}
type Mutation {
addToCart(_id: ID, list_id: ID, item: JSON, itemId: ID, item_amount: Float, unit: String): OpenOrderElement
}
`
];
export const resolvers = {
Query: {
user(_, args, context) {
const { userId } = args;
return Meteor.users.findOne({ _id: userId });
},
// standard Mongo
// listbody(_, args, context, ast) {
// const { listId, limit, skip } = args;
// return ListsBody.find({ list_id: listId }, { sort: { row_id: 1 }, limit, skip }).fetch();
// },
listbody(_, args, context, ast) {
const { userId } = context;
const { listId, limit, skip } = args;
const user = Meteor.users.findOne({ _id: userId });
const listhead = ListsHead.findOne({ _id: listId });
const supplierId = listhead.supplier_id;
// cloned query
const listbody = listsQuery.clone({
listId: listId,
user,
group: null,
supplierId,
skip,
limit
}).fetch();
listbody.forEach((listItem, index) => {
if(index == 0 && Meteor.isDevelopment) console.log(JSON.stringify(listItem, false, 2));
});
return listbody;
// https://cult-of-coders.github.io/grapher/#GraphQL-Bridge
// const listbody = ListsBody.astToQuery(ast, {
// $filters: {
// list_id: listId
// },
// $options: {
// sort: { row_id: 1 },
// limit,
// skip
// }
// }).fetch();
// return listbody;
},
// openorderbody(_, args, context) {
// const { openOrderId } = args;
// const openOrderHead = OpenOrdersHead.findOne({ _id: openOrderId });
// console.log('FETCH openOrderHead', openOrderHead);
// return OpenOrdersBody.find({ list_id: openOrderId }, { sort: { row_id: 1 } }).fetch();
// },
openorderbody(_, args, context, ast) {
const { userId } = context;
const { openOrderId } = args;
const user = Meteor.users.findOne({ _id: userId });
const openOrderHead = OpenOrdersHead.findOne({ _id: openOrderId });
const supplierId = openOrderHead.supplier_id;
// cloned query
const openorderbody = openOrdersQuery.clone({
listId: openOrderId,
user,
group: null,
supplierId
}).fetch();
openorderbody.forEach((listItem, index) => {
if(index == 0 && Meteor.isDevelopment) console.log(JSON.stringify(listItem, false, 2));
});
return openorderbody;
// https://cult-of-coders.github.io/grapher/#GraphQL-Bridge
// const listbody = ListsBody.astToQuery(ast, {
// $filters: {
// list_id: listId
// },
// $options: {
// sort: { row_id: 1 },
// limit,
// skip
// }
// }).fetch();
// return listbody;
},
},
Subscription: {
// userChange: {
// subscribe: () => pubsub.asyncIterator(USER_CHANGE_CHANNEL),
// }
// https://www.apollographql.com/docs/apollo-server/data/data/#context-argument
// userChange: {
// resolve: payload => payload,
// subscribe() {
// const observable = Meteor.users.find({ _id: 'seDueMBtGiuMCWez6' });
// return asyncIterator(observable);
// }
// }
user: {
resolve: payload => payload,
subscribe(_, args, context) {
const { userId } = args;
const observable = Meteor.users.find({ _id: userId });
return asyncIterator(observable);
}
},
// https://github.com/Swydo/ddp-apollo#setting-up-pubsub
listbody: {
// https://github.com/cult-of-coders/apollo-live-server#creating-subscriptions
resolve: ({ event, doc }, args, context, ast) => {
console.log('listbody sub resolve:', { event, doc, args });
doc.__typename = 'ListElement';
if(event == 'added' || event == 'changed') {
Object.assign(doc, {
item: Items.findOne(doc.itemId)
});
}
return { event, doc };
},
subscribe(_, args, context, ast) {
const { listId, limit, skip } = args;
console.log('SUB listbody args:', args);
const observable = ListsBody.find({ list_id: listId });
return asyncIterator(observable, {
sendInitialAdds: true
});
}
},
openorderbody: {
resolve: ({ event, doc }, args, context, ast) => {
console.log('openorderbody sub resolve:', { event, doc, args });
doc.__typename = 'OpenOrderElement';
if(event == 'added' || event == 'changed') {
Object.assign(doc, {
item: Items.findOne(doc.itemId)
});
}
return { event, doc };
},
subscribe(_, args, context) {
const { openOrderId } = args;
const openOrderHead = OpenOrdersHead.findOne({ _id: openOrderId });
if(!openOrderHead) {
console.error(`openOrderHead not found for _id ${openOrderId}`);
return;
}
console.log('SUB openOrderHead', openOrderHead);
const observable = OpenOrdersBody.find({ list_id: openOrderId });
return asyncIterator(observable, {
sendInitialAdds: true
});
}
}
},
Mutation: {
addToCart(_, args, context) {
const { _id, list_id, itemId, item_amount, unit } = args;
// console.log('addToCart args', JSON.stringify(args, false, 2));
const lastOOItem = OpenOrdersBody.findOne({ list_id }, { sort: { row_id: -1 } });
const newBodyItem = {
_id,
list_id,
itemId,
item_amount,
unit: unit || undefined,
row_id: lastOOItem && (lastOOItem.row_id + 1) || 0
};
newBodyItem._id = OpenOrdersBody.insert(newBodyItem);
newBodyItem.item = args.item;
// console.log('newBodyItem', newBodyItem);
return newBodyItem
}
},
JSON: GraphQLJSON
}