@webresto/graphql 1.3.3 → 1.3.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -8,6 +8,7 @@ exports.default = {
8
8
  sendAction
9
9
  };
10
10
  function sendMessage(orderId, message) {
11
+ console.log({ orderId, message });
11
12
  const pubsub = graphql_1.default.getPubsub();
12
13
  pubsub.publish("message", { orderId, message });
13
14
  }
@@ -11,6 +11,7 @@ export {
11
11
  }
12
12
 
13
13
  function sendMessage(orderId: String, message: any) {
14
+ console.log({orderId, message})
14
15
  const pubsub = graphql.getPubsub();
15
16
  pubsub.publish("message", {orderId, message});
16
17
  }
package/package.json CHANGED
@@ -60,5 +60,5 @@
60
60
  "test:js": "mocha test/bootstrap.js './test/{,!(fixture)/**}/*.test.js' --exit",
61
61
  "test:init": "cd ./test/fixture && npm i --no-package-lock --prefix ./ && cd -"
62
62
  },
63
- "version": "1.3.3"
63
+ "version": "1.3.4"
64
64
  }
@@ -70,7 +70,7 @@ exports.additionalResolver = {
70
70
  });
71
71
  dataloaders.set(info.fieldNodes, dl);
72
72
  }
73
- return dl.load(parent.parentGroup);
73
+ return await dl.load(parent.parentGroup);
74
74
  },
75
75
  images: async (parent, args, context, info) => {
76
76
  if (!parent.id)
@@ -87,7 +87,7 @@ exports.additionalResolver = {
87
87
  });
88
88
  dataloaders.set(info.fieldNodes, dl);
89
89
  }
90
- return dl.load(parent.id);
90
+ return await dl.load(parent.id);
91
91
  }
92
92
  },
93
93
  Group: {
@@ -108,7 +108,35 @@ exports.additionalResolver = {
108
108
  });
109
109
  dataloaders.set(info.fieldNodes, dl);
110
110
  }
111
- return dl.load(parent.parentGroup);
111
+ return await dl.load(parent.parentGroup);
112
112
  }
113
- }
113
+ },
114
+ Order: {
115
+ dishes: async (parent, args, context, info) => {
116
+ if (typeof parent.dishes === "object") {
117
+ return parent.dishes;
118
+ }
119
+ return await OrderDish.find({ order: parent.id });
120
+ },
121
+ },
122
+ OrderDish: {
123
+ dish: async (parent, args, context, info) => {
124
+ if (!parent.dish)
125
+ return;
126
+ if (!context.dataloaders)
127
+ context.dataloaders = new WeakMap();
128
+ const dataloaders = context.dataloaders;
129
+ if (typeof parent.dish === "object") {
130
+ return parent.dish;
131
+ }
132
+ let dl = dataloaders.get(info.fieldNodes);
133
+ if (!dl) {
134
+ dl = new DataLoader(async (dishId) => {
135
+ return await Dish.find({ id: dishId }).sort('id ASC');
136
+ });
137
+ dataloaders.set(info.fieldNodes, dl);
138
+ }
139
+ return await dl.load(parent.dish);
140
+ },
141
+ },
114
142
  };
@@ -68,7 +68,7 @@ export const additionalResolver = {
68
68
  });
69
69
  dataloaders.set(info.fieldNodes, dl);
70
70
  }
71
- return dl.load(parent.parentGroup);
71
+ return await dl.load(parent.parentGroup);
72
72
  },
73
73
  images: async (parent, args, context, info) => {
74
74
  if (!parent.id) return;
@@ -84,7 +84,7 @@ export const additionalResolver = {
84
84
  });
85
85
  dataloaders.set(info.fieldNodes, dl);
86
86
  }
87
- return dl.load(parent.id);
87
+ return await dl.load(parent.id);
88
88
  }
89
89
  },
90
90
  Group: {
@@ -105,7 +105,40 @@ export const additionalResolver = {
105
105
  });
106
106
  dataloaders.set(info.fieldNodes, dl);
107
107
  }
108
- return dl.load(parent.parentGroup);
108
+ return await dl.load(parent.parentGroup);
109
109
  }
110
- }
110
+ },
111
+ Order: {
112
+ dishes: async (parent, args, context, info) => {
113
+ if (typeof parent.dishes === "object") {
114
+ return parent.dishes;
115
+ }
116
+
117
+ return await OrderDish.find({order: parent.id});
118
+
119
+ },
120
+
121
+ },
122
+ OrderDish: {
123
+ dish: async (parent, args, context, info) => {
124
+
125
+ if (!parent.dish) return;
126
+ if (!context.dataloaders) context.dataloaders = new WeakMap();
127
+ const dataloaders = context.dataloaders;
128
+
129
+ if (typeof parent.dish === "object") {
130
+ return parent.dish;
131
+ }
132
+
133
+ let dl = dataloaders.get(info.fieldNodes);
134
+ if (!dl) {
135
+ dl = new DataLoader(async (dishId: string) => {
136
+ return await Dish.find({id: dishId}).sort('id ASC');
137
+ });
138
+ dataloaders.set(info.fieldNodes, dl);
139
+ }
140
+ return await dl.load(parent.dish);
141
+ },
142
+
143
+ },
111
144
  }
package/src/graphql.js CHANGED
@@ -65,11 +65,6 @@ exports.default = {
65
65
  type: String
66
66
  message: String
67
67
  }
68
- type Street {
69
- id: String
70
- name: String
71
- customData: Json
72
- }
73
68
  type Action {
74
69
  type: String
75
70
  data: Json
package/src/graphql.ts CHANGED
@@ -74,11 +74,6 @@ export default {
74
74
  type: String
75
75
  message: String
76
76
  }
77
- type Street {
78
- id: String
79
- name: String
80
- customData: Json
81
- }
82
77
  type Action {
83
78
  type: String
84
79
  data: Json
@@ -141,6 +136,7 @@ export default {
141
136
  getEmitter().on("core-maintenance-disabled", "graphql", function () {
142
137
  pubsub.publish("maintenance", null);
143
138
  });
139
+
144
140
  let apolloServer
145
141
  try {
146
142
  apolloServer = new ApolloServer({
@@ -62,11 +62,11 @@ exports.default = {
62
62
  order.customData.callback = data.customData.callback;
63
63
  }
64
64
  await Order.update({ id: order.id }, order).fetch();
65
- let success;
66
- success = await Order.check(order.id, data.customer, isSelfService, data.address, data.paymentMethodId);
67
- order = await Order.findOne(data.orderId).populateAll();
65
+ await Order.check(order.id, data.customer, isSelfService, data.address, data.paymentMethodId);
66
+ order = await Order.findOne(data.orderId);
67
+ console.log(order.dishes);
68
68
  let message;
69
- if (success && order.state === "CHECKOUT") {
69
+ if (order.state === "CHECKOUT") {
70
70
  message = {
71
71
  type: "info",
72
72
  title: "Внимание",
@@ -77,16 +77,17 @@ exports.default = {
77
77
  }
78
78
  else {
79
79
  if (order.message) {
80
- return eventHelper.sendMessage(args.orderId, {
80
+ message = {
81
81
  type: "error",
82
82
  title: "Внимание",
83
83
  message: order.message
84
84
  ? order.message
85
85
  : "Не удалось проверить заказ.",
86
- });
86
+ };
87
87
  }
88
88
  }
89
- return { order: order };
89
+ return eventHelper.sendMessage(args.orderId, message);
90
+ return { message: message, order: order };
90
91
  }
91
92
  catch (e) {
92
93
  let message = {
@@ -203,6 +204,7 @@ exports.default = {
203
204
  // }
204
205
  // }
205
206
  await Order.order(order.id);
207
+ order = await Order.findOne({ id: data.orderId });
206
208
  const message = {
207
209
  type: "info",
208
210
  title: "Успешно",
@@ -42,6 +42,7 @@ export default {
42
42
  message: "Корзина уже заказана",
43
43
  });
44
44
  }
45
+
45
46
  if (data.paymentMethodId) {
46
47
  if (!PaymentMethod.checkAvailable(data.paymentMethodId)) {
47
48
  return eventHelper.sendMessage(args.orderId, {
@@ -70,19 +71,20 @@ export default {
70
71
 
71
72
  await Order.update({ id: order.id }, order).fetch();
72
73
 
73
- let success: boolean;
74
-
75
- success = await Order.check(
74
+ await Order.check(
76
75
  order.id,
77
76
  data.customer,
78
77
  isSelfService,
79
78
  data.address,
80
79
  data.paymentMethodId
81
80
  );
82
- order = await Order.findOne(data.orderId).populateAll();
81
+
82
+ order = await Order.findOne(data.orderId);
83
+ console.log(order.dishes)
84
+
83
85
  let message;
84
86
 
85
- if (success && order.state === "CHECKOUT") {
87
+ if (order.state === "CHECKOUT") {
86
88
  message = {
87
89
  type: "info",
88
90
  title: "Внимание",
@@ -93,17 +95,19 @@ export default {
93
95
 
94
96
  } else {
95
97
  if (order.message) {
96
- return eventHelper.sendMessage(args.orderId, {
98
+ message = {
97
99
  type: "error",
98
100
  title: "Внимание",
99
101
  message: order.message
100
102
  ? order.message
101
103
  : "Не удалось проверить заказ.",
102
- });
104
+ }
103
105
  }
104
106
  }
105
107
 
106
- return { order: order };
108
+ return eventHelper.sendMessage(args.orderId, message);
109
+
110
+ return { message: message, order: order };
107
111
  } catch (e) {
108
112
  let message = {
109
113
  type: "error",
@@ -219,7 +223,7 @@ export default {
219
223
  // }
220
224
 
221
225
  await Order.order(order.id);
222
-
226
+ order = await Order.findOne({ id: data.orderId })
223
227
  const message = {
224
228
  type: "info",
225
229
  title: "Успешно",
@@ -27,7 +27,7 @@
27
27
  process.chdir(__dirname);
28
28
 
29
29
  process.env.DEV = "TRUE";
30
-
30
+ process.env.PORT = 42776;
31
31
 
32
32
  // Attempt to import `sails` dependency, as well as `rc` (for loading `.sailsrc` files).
33
33
  var sails;
@@ -0,0 +1,86 @@
1
+ const expect = require("chai").expect;
2
+ describe('Send order to restarant', function () {
3
+ it('Create new', async () => {
4
+ const graphql = require('./../fixture/node_modules/@webresto/graphql/src/graphql').default;
5
+ console.log(graphql);
6
+ const result = await graphql.getServer().executeOperation({
7
+ query: `
8
+ {order(orderId:"test-cart"){
9
+ id
10
+ total
11
+ }}`
12
+ });
13
+ expect(result.errors).to.be.undefined;
14
+ });
15
+ it('orderAddDish', async () => {
16
+ const graphql = require('./../fixture/node_modules/@webresto/graphql/src/graphql').default;
17
+ console.log(graphql);
18
+ const result = await graphql.getServer().executeOperation({
19
+ query: `
20
+ mutation{orderAddDish(dishId: "df19cbbc-c026-595e-9296-1c8a378da6b3", orderId:"test-cart", amount: 1){
21
+ dishes {
22
+ dish {
23
+ id
24
+ name
25
+ price
26
+ }
27
+ itemTotal
28
+ }
29
+ total
30
+ }}
31
+ `
32
+ });
33
+ expect(result.data.orderAddDish.dishes[0].dish.id).to.equal("df19cbbc-c026-595e-9296-1c8a378da6b3");
34
+ });
35
+ it('checkOrder', async () => {
36
+ const graphql = require('./../fixture/node_modules/@webresto/graphql/src/graphql').default;
37
+ console.log(graphql);
38
+ const result = await graphql.getServer().executeOperation({
39
+ query: `
40
+ mutation{checkOrder(orderId:"test-cart", paymentMethodId: "", address: {city: "town", street: "test", home: "123"}, customer: {phone: {code: "+1", number: "0000000000"}, name: "Piter Parker"}) {
41
+ order {
42
+ id
43
+ state
44
+ dishes{
45
+ dish {
46
+ name
47
+ id
48
+ }
49
+ }
50
+ }
51
+ message {
52
+ title
53
+ message
54
+ }
55
+ }}
56
+ `
57
+ });
58
+ expect(result.data.checkOrder.order.dishes[0].dish.id).to.equal("df19cbbc-c026-595e-9296-1c8a378da6b3");
59
+ });
60
+ it('sendOrder', async () => {
61
+ const graphql = require('./../fixture/node_modules/@webresto/graphql/src/graphql').default;
62
+ console.log(graphql);
63
+ const result = await graphql.getServer().executeOperation({
64
+ query: `
65
+ mutation{sendOrder(orderId:"test-cart") {
66
+ order {
67
+ id
68
+ state
69
+ dishes{
70
+ dish {
71
+ name
72
+ id
73
+ }
74
+ amount
75
+ }
76
+ }
77
+ message {
78
+ title
79
+ message
80
+ }
81
+ }}
82
+ `
83
+ });
84
+ expect(result.data.sendOrder.order.state).to.equal("ORDER");
85
+ });
86
+ });
@@ -0,0 +1,108 @@
1
+ const expect = require("chai").expect;
2
+
3
+ describe('Send order to restarant', function () {
4
+ it('Create new', async () => {
5
+
6
+ const graphql = require('./../fixture/node_modules/@webresto/graphql/src/graphql').default;
7
+ console.log(graphql)
8
+ const result = await graphql.getServer().executeOperation({
9
+ query: `
10
+ {order(orderId:"test-cart"){
11
+ id
12
+ total
13
+ }}`
14
+ });
15
+
16
+ expect(result.errors).to.be.undefined;
17
+ });
18
+
19
+
20
+
21
+
22
+ it('orderAddDish', async () => {
23
+
24
+ const graphql = require('./../fixture/node_modules/@webresto/graphql/src/graphql').default;
25
+ console.log(graphql)
26
+ const result = await graphql.getServer().executeOperation({
27
+ query: `
28
+ mutation{orderAddDish(dishId: "df19cbbc-c026-595e-9296-1c8a378da6b3", orderId:"test-cart", amount: 1){
29
+ dishes {
30
+ dish {
31
+ id
32
+ name
33
+ price
34
+ }
35
+ itemTotal
36
+ }
37
+ total
38
+ }}
39
+ `
40
+ });
41
+ expect(result.data.orderAddDish.dishes[0].dish.id).to.equal("df19cbbc-c026-595e-9296-1c8a378da6b3");
42
+ });
43
+
44
+
45
+ it('checkOrder', async () => {
46
+
47
+ const graphql = require('./../fixture/node_modules/@webresto/graphql/src/graphql').default;
48
+ console.log(graphql)
49
+ const result = await graphql.getServer().executeOperation({
50
+ query: `
51
+ mutation{checkOrder(orderId:"test-cart", paymentMethodId: "", address: {city: "town", street: "test", home: "123"}, customer: {phone: {code: "+1", number: "0000000000"}, name: "Piter Parker"}) {
52
+ order {
53
+ id
54
+ state
55
+ dishes{
56
+ dish {
57
+ name
58
+ id
59
+ }
60
+ }
61
+ }
62
+ message {
63
+ title
64
+ message
65
+ }
66
+ }}
67
+ `
68
+ });
69
+ expect(result.data.checkOrder.order.dishes[0].dish.id).to.equal("df19cbbc-c026-595e-9296-1c8a378da6b3");
70
+ });
71
+
72
+
73
+
74
+ it('sendOrder', async () => {
75
+
76
+ const graphql = require('./../fixture/node_modules/@webresto/graphql/src/graphql').default;
77
+ console.log(graphql)
78
+ const result = await graphql.getServer().executeOperation({
79
+ query: `
80
+ mutation{sendOrder(orderId:"test-cart") {
81
+ order {
82
+ id
83
+ state
84
+ dishes{
85
+ dish {
86
+ name
87
+ id
88
+ }
89
+ amount
90
+ }
91
+ }
92
+ message {
93
+ title
94
+ message
95
+ }
96
+ }}
97
+ `
98
+ });
99
+
100
+ expect(result.data.sendOrder.order.state).to.equal("ORDER");
101
+ });
102
+
103
+
104
+
105
+
106
+ });
107
+
108
+