@webresto/graphql 1.3.7 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- package/.gitattributes +2 -0
- package/.gitlab-ci.yml +18 -0
- package/.vscode/extensions.json +5 -0
- package/docs/actions.md +25 -0
- package/docs/authorization.md +215 -0
- package/docs/captcha.md +71 -0
- package/docs/device-id.md +30 -0
- package/docs/messages.md +10 -0
- package/docs/user.md +54 -0
- package/index.d.ts +0 -1
- package/index.js +6 -2
- package/index.ts +2 -2
- package/lib/afterHook.js +8 -0
- package/lib/afterHook.ts +9 -0
- package/lib/bindTranslations.d.ts +1 -0
- package/lib/bindTranslations.js +40 -0
- package/lib/bindTranslations.ts +39 -0
- package/lib/defaults.d.ts +1 -0
- package/lib/defaults.js +49 -10
- package/lib/defaults.ts +55 -0
- package/lib/eventHelper.d.ts +14 -5
- package/lib/eventHelper.js +28 -9
- package/lib/eventHelper.ts +41 -8
- package/lib/getRecomended.d.ts +1 -0
- package/lib/getRecomended.js +29 -0
- package/lib/getRecomended.ts +31 -0
- package/lib/graphqlHelper.d.ts +3 -4
- package/lib/graphqlHelper.js +184 -72
- package/lib/graphqlHelper.ts +329 -185
- package/lib/jwt.d.ts +10 -0
- package/lib/jwt.js +43 -0
- package/lib/jwt.ts +61 -0
- package/package.json +15 -7
- package/src/additionalResolvers.d.ts +72 -9
- package/src/additionalResolvers.js +93 -24
- package/src/additionalResolvers.ts +105 -34
- package/src/graphql.d.ts +5 -3
- package/src/graphql.js +170 -37
- package/src/graphql.ts +210 -60
- package/src/resolvers/bonusProgram.d.ts +32 -0
- package/src/resolvers/bonusProgram.js +65 -0
- package/src/resolvers/bonusProgram.ts +79 -0
- package/src/resolvers/captcha.d.ts +11 -0
- package/src/resolvers/captcha.js +19 -0
- package/src/resolvers/captcha.ts +16 -0
- package/src/resolvers/checkout.d.ts +35 -16
- package/src/resolvers/checkout.js +171 -94
- package/src/resolvers/checkout.ts +214 -104
- package/src/resolvers/dishAndModifier.js +8 -4
- package/src/resolvers/dishAndModifier.ts +4 -0
- package/src/resolvers/error.d.ts +9 -0
- package/src/resolvers/error.js +21 -0
- package/src/resolvers/error.ts +21 -0
- package/src/resolvers/menu.d.ts +9 -0
- package/src/resolvers/menu.js +12 -0
- package/src/resolvers/menu.ts +10 -0
- package/src/resolvers/order.d.ts +527 -0
- package/src/resolvers/order.js +349 -0
- package/src/resolvers/order.ts +435 -0
- package/src/resolvers/paymentMethod.js +7 -3
- package/src/resolvers/paymentMethod.ts +9 -5
- package/src/resolvers/pickupPoint.d.ts +1 -0
- package/src/resolvers/pickupPoint.js +24 -0
- package/src/resolvers/pickupPoint.ts +23 -0
- package/src/resolvers/recomended.d.ts +13 -0
- package/src/resolvers/recomended.js +80 -0
- package/src/resolvers/recomended.ts +86 -0
- package/src/resolvers/restrictions.d.ts +37 -1
- package/src/resolvers/restrictions.js +100 -15
- package/src/resolvers/restrictions.ts +106 -14
- package/src/resolvers/streets.d.ts +1 -1
- package/src/resolvers/streets.js +1 -4
- package/src/resolvers/streets.ts +1 -3
- package/src/resolvers/subscriptions.d.ts +4 -4
- package/src/resolvers/subscriptions.js +49 -12
- package/src/resolvers/subscriptions.ts +59 -14
- package/src/resolvers/telemetry.d.ts +14 -0
- package/src/resolvers/telemetry.js +25 -0
- package/src/resolvers/telemetry.ts +24 -0
- package/src/resolvers/user.d.ts +82 -0
- package/src/resolvers/user.js +416 -0
- package/src/resolvers/user.ts +621 -0
- package/src/resolvers/userLocation.d.ts +53 -0
- package/src/resolvers/userLocation.js +74 -0
- package/src/resolvers/userLocation.ts +125 -0
- package/src/resolvers/userOTPrequest.d.ts +21 -0
- package/src/resolvers/userOTPrequest.js +57 -0
- package/src/resolvers/userOTPrequest.ts +75 -0
- package/test/e2e_helper.js +157 -0
- package/test/e2e_helper.ts +212 -0
- package/test/fixture/config/i18n.js +7 -20
- package/test/fixture/config/locales/de.json +1 -0
- package/test/fixture/config/locales/en.json +10 -0
- package/test/fixture/config/locales/es.json +3 -0
- package/test/fixture/config/locales/fr.json +1 -0
- package/test/fixture/config/log.js +1 -1
- package/test/fixture/package.json +5 -6
- package/test/fixture/patches/rttc+10.0.1.patch +17 -0
- package/test/integration/captcha.test.js +20 -0
- package/test/integration/captcha.test.ts +25 -0
- package/test/integration/dish.test.js +35 -0
- package/test/integration/dish.test.ts +43 -0
- package/test/integration/graphql.test.js +5 -2
- package/test/integration/graphql.test.ts +2 -4
- package/test/integration/images.test.js +35 -0
- package/test/integration/images.test.ts +40 -0
- package/test/integration/locale.test.js +26 -0
- package/test/integration/locale.test.ts +32 -0
- package/test/integration/order.test.js +56 -43
- package/test/integration/order.test.ts +59 -59
- package/test/integration/subscriptions.test.js +136 -0
- package/test/integration/subscriptions.test.ts +162 -0
- package/test/integration/user.test.js +249 -0
- package/test/integration/user.test.ts +299 -0
- package/test/unit/first.test.js +4 -2
- package/test/unit/first.test.ts +1 -1
- package/test/unit/get-recomended.test.js +56 -0
- package/test/unit/get-recomended.test.ts +63 -0
- package/translations/de.json +2 -0
- package/translations/en.json +3 -0
- package/translations/es.json +3 -0
- package/translations/fr.json +2 -0
- package/translations/ru.json +36 -0
- package/tsconfig.json +20 -5
- package/types/global.d.ts +30 -0
- package/types/global.js +2 -0
- package/types/global.ts +31 -0
- package/types/primitives.d.ts +19 -0
- package/types/references.d.ts +1 -0
- package/types/restoGraphQLConfig.d.ts +13 -0
- package/lib/afterHook.ts___graphql-transport-ws +0 -138
- package/lib/afterHook.ts___graphql-ws +0 -133
- package/lib/errorWrapper.d.ts +0 -4
- package/lib/errorWrapper.js +0 -13
- package/lib/errorWrapper.ts +0 -12
- package/notes.md +0 -1976
- package/src/resolvers/cart.d.ts +0 -343
- package/src/resolvers/cart.js +0 -196
- package/src/resolvers/cart.ts +0 -278
- package/test/fixture/config/connections.js +0 -9
- package/test/integration/sails_not_crash.test.js +0 -3
- package/test/integration/sails_not_crash.test.ts +0 -3
@@ -0,0 +1,74 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
// const userAuth = sails.config.restographql.authService;
|
4
|
+
const jwt_1 = require("../../lib/jwt");
|
5
|
+
const adapters_1 = require("@webresto/core/adapters");
|
6
|
+
let captchaAdapter = adapters_1.Captcha.getAdapter();
|
7
|
+
const graphqlHelper_1 = require("../../lib/graphqlHelper");
|
8
|
+
graphqlHelper_1.default.addType(`#graphql
|
9
|
+
input InputLocation {
|
10
|
+
street: String
|
11
|
+
streetId: String
|
12
|
+
home: String!
|
13
|
+
name: String
|
14
|
+
city: String
|
15
|
+
housing: String
|
16
|
+
isDefault: Boolean
|
17
|
+
index: String
|
18
|
+
entrance: String
|
19
|
+
floor: String
|
20
|
+
apartment: String
|
21
|
+
doorphone: String
|
22
|
+
comment: String
|
23
|
+
customFields: Json
|
24
|
+
}
|
25
|
+
`);
|
26
|
+
exports.default = {
|
27
|
+
Mutation: {
|
28
|
+
// Authentication required
|
29
|
+
locationCreate: {
|
30
|
+
def: `#graphql
|
31
|
+
locationCreate(
|
32
|
+
location: InputLocation!
|
33
|
+
): Boolean`,
|
34
|
+
fn: async (parent, payload, context) => {
|
35
|
+
const auth = await jwt_1.JWTAuth.verify(context.connectionParams.authorization);
|
36
|
+
if (!payload.location.streetId && !payload.location.street)
|
37
|
+
throw 'streetId or street are required';
|
38
|
+
const userLocation = {
|
39
|
+
...payload.location,
|
40
|
+
...{ street: payload.location.streetId }
|
41
|
+
};
|
42
|
+
await UserLocation.create({ ...userLocation, user: auth.userId }).fetch();
|
43
|
+
return true;
|
44
|
+
}
|
45
|
+
},
|
46
|
+
// Authentication required
|
47
|
+
locationSetIsDefault: {
|
48
|
+
def: `#graphql
|
49
|
+
locationSetIsDefault(
|
50
|
+
locationId: String!
|
51
|
+
): Boolean`,
|
52
|
+
fn: async (parent, payload, context) => {
|
53
|
+
const auth = await jwt_1.JWTAuth.verify(context.connectionParams.authorization);
|
54
|
+
const user = (await UserLocation.findOne({ id: payload.locationId })).user;
|
55
|
+
if (user !== auth.userId)
|
56
|
+
throw `User location not found`;
|
57
|
+
await UserLocation.update({ id: payload.locationId }, { isDefault: true }).fetch();
|
58
|
+
return true;
|
59
|
+
}
|
60
|
+
},
|
61
|
+
// Authentication required
|
62
|
+
locationDelete: {
|
63
|
+
def: `#graphql
|
64
|
+
locationDelete(
|
65
|
+
locationId: String!
|
66
|
+
): Boolean`,
|
67
|
+
fn: async (parent, payload, context) => {
|
68
|
+
const auth = await jwt_1.JWTAuth.verify(context.connectionParams.authorization);
|
69
|
+
await UserLocation.destroy({ id: payload.locationId }).fetch();
|
70
|
+
return true;
|
71
|
+
}
|
72
|
+
}
|
73
|
+
}
|
74
|
+
};
|
@@ -0,0 +1,125 @@
|
|
1
|
+
// const userAuth = sails.config.restographql.authService;
|
2
|
+
import { JWTAuth } from "../../lib/jwt";
|
3
|
+
import { Phone } from "@webresto/core/models/User";
|
4
|
+
import User from "@webresto/core/models/User";
|
5
|
+
import { Captcha } from "@webresto/core/adapters";
|
6
|
+
import { ResolvedCaptcha } from "@webresto/core/adapters/captcha/CaptchaAdapter";
|
7
|
+
import { Message, Action, Response } from "../../types/primitives";
|
8
|
+
let captchaAdapter = Captcha.getAdapter();
|
9
|
+
|
10
|
+
import graphqlHelper from "../../lib/graphqlHelper";
|
11
|
+
|
12
|
+
// define UserResponse
|
13
|
+
interface UserResponse extends Response {
|
14
|
+
user: User | undefined;
|
15
|
+
}
|
16
|
+
|
17
|
+
|
18
|
+
interface InputLocation {
|
19
|
+
street: string
|
20
|
+
streetId: string
|
21
|
+
home: string
|
22
|
+
name?: string
|
23
|
+
city?: string
|
24
|
+
housing?: string
|
25
|
+
isDefault?: boolean
|
26
|
+
index?: string
|
27
|
+
entrance?: string
|
28
|
+
floor?: string
|
29
|
+
apartment?: string
|
30
|
+
doorphone?: string
|
31
|
+
comment?: string
|
32
|
+
customData?: {
|
33
|
+
[key: string]: string | boolean | number;
|
34
|
+
}
|
35
|
+
}
|
36
|
+
|
37
|
+
graphqlHelper.addType(`#graphql
|
38
|
+
input InputLocation {
|
39
|
+
street: String
|
40
|
+
streetId: String
|
41
|
+
home: String!
|
42
|
+
name: String
|
43
|
+
city: String
|
44
|
+
housing: String
|
45
|
+
isDefault: Boolean
|
46
|
+
index: String
|
47
|
+
entrance: String
|
48
|
+
floor: String
|
49
|
+
apartment: String
|
50
|
+
doorphone: String
|
51
|
+
comment: String
|
52
|
+
customFields: Json
|
53
|
+
}
|
54
|
+
`);
|
55
|
+
|
56
|
+
export default {
|
57
|
+
Mutation: {
|
58
|
+
// Authentication required
|
59
|
+
locationCreate: {
|
60
|
+
def: `#graphql
|
61
|
+
locationCreate(
|
62
|
+
location: InputLocation!
|
63
|
+
): Boolean`,
|
64
|
+
fn: async (
|
65
|
+
parent: any,
|
66
|
+
payload: { location: InputLocation },
|
67
|
+
context: { connectionParams: { authorization: string } }
|
68
|
+
): Promise<boolean> => {
|
69
|
+
const auth = await JWTAuth.verify(
|
70
|
+
context.connectionParams.authorization
|
71
|
+
);
|
72
|
+
|
73
|
+
if (!payload.location.streetId && !payload.location.street) throw 'streetId or street are required'
|
74
|
+
|
75
|
+
const userLocation = {
|
76
|
+
...payload.location,
|
77
|
+
...{street: payload.location.streetId}
|
78
|
+
}
|
79
|
+
await UserLocation.create({...userLocation, user: auth.userId}).fetch()
|
80
|
+
return true
|
81
|
+
}
|
82
|
+
},
|
83
|
+
// Authentication required
|
84
|
+
locationSetIsDefault: {
|
85
|
+
def: `#graphql
|
86
|
+
locationSetIsDefault(
|
87
|
+
locationId: String!
|
88
|
+
): Boolean`,
|
89
|
+
fn: async (
|
90
|
+
parent: any,
|
91
|
+
payload: { locationId: string },
|
92
|
+
context: { connectionParams: { authorization: string } }
|
93
|
+
): Promise<boolean> => {
|
94
|
+
const auth = await JWTAuth.verify(
|
95
|
+
context.connectionParams.authorization
|
96
|
+
);
|
97
|
+
|
98
|
+
const user = (await UserLocation.findOne({id: payload.locationId})).user as string
|
99
|
+
if (user !== auth.userId) throw `User location not found`
|
100
|
+
|
101
|
+
await UserLocation.update({id: payload.locationId}, {isDefault: true}).fetch()
|
102
|
+
return true
|
103
|
+
}
|
104
|
+
},
|
105
|
+
// Authentication required
|
106
|
+
locationDelete: {
|
107
|
+
def: `#graphql
|
108
|
+
locationDelete(
|
109
|
+
locationId: String!
|
110
|
+
): Boolean`,
|
111
|
+
fn: async (
|
112
|
+
parent: any,
|
113
|
+
payload: { locationId: string },
|
114
|
+
context: { connectionParams: { authorization: string } }
|
115
|
+
): Promise<boolean> => {
|
116
|
+
const auth = await JWTAuth.verify(
|
117
|
+
context.connectionParams.authorization
|
118
|
+
);
|
119
|
+
|
120
|
+
await UserLocation.destroy({id: payload.locationId}).fetch()
|
121
|
+
return true
|
122
|
+
}
|
123
|
+
}
|
124
|
+
}
|
125
|
+
};
|
@@ -0,0 +1,21 @@
|
|
1
|
+
import { ResolvedCaptcha } from "@webresto/core/adapters/captcha/CaptchaAdapter";
|
2
|
+
import { Message, Action } from "../../types/primitives";
|
3
|
+
type OTPResponse = {
|
4
|
+
id: Number;
|
5
|
+
nextOTPSeconds: Number;
|
6
|
+
message: Message;
|
7
|
+
action?: Action;
|
8
|
+
};
|
9
|
+
type OTPPayload = {
|
10
|
+
login: string;
|
11
|
+
captcha: ResolvedCaptcha;
|
12
|
+
};
|
13
|
+
declare const _default: {
|
14
|
+
Mutation: {
|
15
|
+
OTPRequest: {
|
16
|
+
def: string;
|
17
|
+
fn: (parent: any, payload: OTPPayload, context: any) => Promise<OTPResponse>;
|
18
|
+
};
|
19
|
+
};
|
20
|
+
};
|
21
|
+
export default _default;
|
@@ -0,0 +1,57 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
// const userAuth = sails.config.restographql.authService;
|
4
|
+
const adapters_1 = require("@webresto/core/adapters");
|
5
|
+
const adapters_2 = require("@webresto/core/adapters");
|
6
|
+
let captchaAdapter = adapters_1.Captcha.getAdapter();
|
7
|
+
let OTPAdapter = adapters_2.OTP.getAdapter();
|
8
|
+
const graphqlHelper_1 = require("../../lib/graphqlHelper");
|
9
|
+
graphqlHelper_1.default.addType(`#graphql
|
10
|
+
type OTPResponse {
|
11
|
+
id: String
|
12
|
+
nextOTPAfterSeconds: Int
|
13
|
+
message: Message
|
14
|
+
action: Action
|
15
|
+
}
|
16
|
+
`);
|
17
|
+
exports.default = {
|
18
|
+
Mutation: {
|
19
|
+
OTPRequest: {
|
20
|
+
def: `#graphql
|
21
|
+
OTPRequest(
|
22
|
+
|
23
|
+
"Phone login is 'phone.code+phone.number' for phone only digits"
|
24
|
+
login: String!,
|
25
|
+
|
26
|
+
"Solved captcha for label 'otpRequest:%login%'"
|
27
|
+
captcha: Captcha!
|
28
|
+
|
29
|
+
): OTPResponse`,
|
30
|
+
fn: async (parent, payload, context) => {
|
31
|
+
console.log(!(await captchaAdapter).check(payload.captcha, `otprequest:${payload.login}`));
|
32
|
+
if (!(await captchaAdapter).check(payload.captcha, `otprequest:${payload.login}`)) {
|
33
|
+
throw `bad captcha`;
|
34
|
+
}
|
35
|
+
let otp = await (await OTPAdapter).get(payload.login);
|
36
|
+
let message = {
|
37
|
+
deviceId: null,
|
38
|
+
title: context.i18n.__("Success"),
|
39
|
+
type: "info",
|
40
|
+
message: context.i18n.__("OTP sended")
|
41
|
+
};
|
42
|
+
// Here should be Emmiter for midificate Action And Message
|
43
|
+
let action = {
|
44
|
+
deviceId: null,
|
45
|
+
type: "",
|
46
|
+
data: {}
|
47
|
+
};
|
48
|
+
return {
|
49
|
+
id: otp.id,
|
50
|
+
nextOTPSeconds: 300,
|
51
|
+
message: message,
|
52
|
+
action: action
|
53
|
+
};
|
54
|
+
},
|
55
|
+
}
|
56
|
+
}
|
57
|
+
};
|
@@ -0,0 +1,75 @@
|
|
1
|
+
// const userAuth = sails.config.restographql.authService;
|
2
|
+
import { Captcha } from "@webresto/core/adapters";
|
3
|
+
import { OTP } from "@webresto/core/adapters"
|
4
|
+
import { ResolvedCaptcha } from "@webresto/core/adapters/captcha/CaptchaAdapter"
|
5
|
+
import { Message, Action, Response } from "../../types/primitives";
|
6
|
+
let captchaAdapter = Captcha.getAdapter();
|
7
|
+
let OTPAdapter = OTP.getAdapter();
|
8
|
+
|
9
|
+
import graphqlHelper from "../../lib/graphqlHelper";
|
10
|
+
|
11
|
+
type OTPResponse = {
|
12
|
+
id: Number
|
13
|
+
nextOTPSeconds: Number
|
14
|
+
message: Message
|
15
|
+
action?: Action
|
16
|
+
}
|
17
|
+
|
18
|
+
graphqlHelper.addType(`#graphql
|
19
|
+
type OTPResponse {
|
20
|
+
id: String
|
21
|
+
nextOTPAfterSeconds: Int
|
22
|
+
message: Message
|
23
|
+
action: Action
|
24
|
+
}
|
25
|
+
`)
|
26
|
+
|
27
|
+
type OTPPayload = {
|
28
|
+
login: string
|
29
|
+
captcha: ResolvedCaptcha
|
30
|
+
}
|
31
|
+
|
32
|
+
export default {
|
33
|
+
Mutation: {
|
34
|
+
OTPRequest: {
|
35
|
+
def: `#graphql
|
36
|
+
OTPRequest(
|
37
|
+
|
38
|
+
"Phone login is 'phone.code+phone.number' for phone only digits"
|
39
|
+
login: String!,
|
40
|
+
|
41
|
+
"Solved captcha for label 'otpRequest:%login%'"
|
42
|
+
captcha: Captcha!
|
43
|
+
|
44
|
+
): OTPResponse`,
|
45
|
+
fn: async (parent: any, payload: OTPPayload, context: any): Promise<OTPResponse> => {
|
46
|
+
console.log(!(await captchaAdapter).check(payload.captcha, `otprequest:${payload.login}`))
|
47
|
+
if (!(await captchaAdapter).check(payload.captcha, `otprequest:${payload.login}`)) {
|
48
|
+
throw `bad captcha`
|
49
|
+
}
|
50
|
+
let otp = await (await OTPAdapter).get(payload.login);
|
51
|
+
let message: Message = {
|
52
|
+
deviceId: null,
|
53
|
+
title: context.i18n.__("Success"),
|
54
|
+
type: "info",
|
55
|
+
message: context.i18n.__("OTP sended")
|
56
|
+
}
|
57
|
+
|
58
|
+
// Here should be Emmiter for midificate Action And Message
|
59
|
+
let action: Action = {
|
60
|
+
deviceId: null,
|
61
|
+
type: "",
|
62
|
+
data: {}
|
63
|
+
}
|
64
|
+
|
65
|
+
|
66
|
+
return {
|
67
|
+
id: otp.id,
|
68
|
+
nextOTPSeconds: 300,
|
69
|
+
message: message,
|
70
|
+
action: action
|
71
|
+
};
|
72
|
+
},
|
73
|
+
}
|
74
|
+
}
|
75
|
+
};
|
@@ -0,0 +1,157 @@
|
|
1
|
+
"use strict";
|
2
|
+
//
|
3
|
+
// E2E Test Setup
|
4
|
+
// e2e_helpers.js
|
5
|
+
//
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
7
|
+
exports.runGQL = exports.createSubscription = exports.TestApolloClient = void 0;
|
8
|
+
const utilities_1 = require("@apollo/client/utilities");
|
9
|
+
const ws_1 = require("@apollo/client/link/ws");
|
10
|
+
const cross_fetch_1 = require("cross-fetch");
|
11
|
+
const ws = require("ws");
|
12
|
+
const axios_1 = require("axios");
|
13
|
+
const client_1 = require("@apollo/client");
|
14
|
+
const host = "127.0.0.1";
|
15
|
+
const port = process.env.PORT === undefined ? "42772" : process.env.PORT;
|
16
|
+
/**
|
17
|
+
* Our test apollo client <3
|
18
|
+
*
|
19
|
+
* @export
|
20
|
+
* @class TestApolloClient
|
21
|
+
* @extends {ApolloClient<any>}
|
22
|
+
*/
|
23
|
+
class TestApolloClient extends client_1.ApolloClient {
|
24
|
+
wsLink;
|
25
|
+
constructor(serverInfo) {
|
26
|
+
const { host } = serverInfo;
|
27
|
+
const wsLink = new ws_1.WebSocketLink({
|
28
|
+
// @TODO: Update WS url
|
29
|
+
uri: `ws://${host}:${port}/graphql`,
|
30
|
+
webSocketImpl: ws,
|
31
|
+
options: {
|
32
|
+
reconnect: false,
|
33
|
+
connectionParams: {
|
34
|
+
Authorization: "Bearer dummy",
|
35
|
+
},
|
36
|
+
},
|
37
|
+
});
|
38
|
+
const httpLink = new client_1.HttpLink({
|
39
|
+
// @TODO: update HTTP url
|
40
|
+
uri: `http://${host}:${port}/graphql`,
|
41
|
+
fetch: cross_fetch_1.default,
|
42
|
+
});
|
43
|
+
const splitLink = (0, client_1.split)(({ query }) => {
|
44
|
+
const definition = (0, utilities_1.getMainDefinition)(query);
|
45
|
+
return (definition.kind === "OperationDefinition" &&
|
46
|
+
definition.operation === "subscription");
|
47
|
+
}, wsLink, httpLink);
|
48
|
+
super({
|
49
|
+
link: splitLink,
|
50
|
+
cache: new client_1.InMemoryCache(),
|
51
|
+
});
|
52
|
+
this.wsLink = wsLink;
|
53
|
+
}
|
54
|
+
stop() {
|
55
|
+
super.stop();
|
56
|
+
//
|
57
|
+
// This is important, or else your test session will never close.
|
58
|
+
// Also the reason it inherits from ApolloClient instead of just creating it
|
59
|
+
// That way we can sneak in that WS closing
|
60
|
+
//
|
61
|
+
this.wsLink.subscriptionClient.close();
|
62
|
+
}
|
63
|
+
}
|
64
|
+
exports.TestApolloClient = TestApolloClient;
|
65
|
+
/**
|
66
|
+
* Helper to create a subscription with a bunch of handy methods
|
67
|
+
*
|
68
|
+
* @export
|
69
|
+
* @param {SubscriptionTestParams} params
|
70
|
+
* @returns
|
71
|
+
*/
|
72
|
+
function createSubscription(params) {
|
73
|
+
const results = [];
|
74
|
+
let error;
|
75
|
+
let observer = params.client
|
76
|
+
.subscribe({
|
77
|
+
errorPolicy: "all",
|
78
|
+
query: params.query,
|
79
|
+
variables: params.variables || {},
|
80
|
+
})
|
81
|
+
.subscribe({
|
82
|
+
next(data) {
|
83
|
+
results.push(data);
|
84
|
+
},
|
85
|
+
error: (err) => {
|
86
|
+
error = err;
|
87
|
+
},
|
88
|
+
});
|
89
|
+
return {
|
90
|
+
results,
|
91
|
+
observer,
|
92
|
+
disconnect() {
|
93
|
+
observer.unsubscribe();
|
94
|
+
},
|
95
|
+
wait(ms = 100) {
|
96
|
+
return new Promise((done) => {
|
97
|
+
setTimeout(() => {
|
98
|
+
done(null);
|
99
|
+
}, ms);
|
100
|
+
});
|
101
|
+
},
|
102
|
+
waitForResults(opts = {}) {
|
103
|
+
return new Promise((done, fail) => {
|
104
|
+
let step = 50;
|
105
|
+
let sum = 0;
|
106
|
+
let timeout = opts.timeout ?? 3000;
|
107
|
+
let len = opts.len ?? 1;
|
108
|
+
const interval = setInterval(() => {
|
109
|
+
if (!error && results.length >= len) {
|
110
|
+
clearInterval(interval);
|
111
|
+
return done(results);
|
112
|
+
}
|
113
|
+
if (error || sum >= timeout) {
|
114
|
+
error =
|
115
|
+
error ||
|
116
|
+
new Error(`Timeout: subscription did not receive the expected results after ${timeout}ms`);
|
117
|
+
clearInterval(interval);
|
118
|
+
return fail(error);
|
119
|
+
}
|
120
|
+
sum += step;
|
121
|
+
}, step);
|
122
|
+
});
|
123
|
+
},
|
124
|
+
get triggerCount() {
|
125
|
+
return results.length;
|
126
|
+
},
|
127
|
+
get error() {
|
128
|
+
return error;
|
129
|
+
},
|
130
|
+
};
|
131
|
+
}
|
132
|
+
exports.createSubscription = createSubscription;
|
133
|
+
async function runGQL(query, headersOrToken, variables) {
|
134
|
+
return new Promise((resolve, reject) => {
|
135
|
+
let headers = headersOrToken;
|
136
|
+
// Except stringToken
|
137
|
+
if (typeof headersOrToken === "string") {
|
138
|
+
headers = {
|
139
|
+
Authorization: `${headersOrToken}`,
|
140
|
+
};
|
141
|
+
}
|
142
|
+
axios_1.default
|
143
|
+
.post(`http://${host}:${port}/graphql`, { query, variables }, { headers })
|
144
|
+
.then((response) => {
|
145
|
+
if (response.data.errors) {
|
146
|
+
console.log(response.data.errors);
|
147
|
+
return resolve(response.data);
|
148
|
+
}
|
149
|
+
return resolve(response.data);
|
150
|
+
})
|
151
|
+
.catch((error) => {
|
152
|
+
console.log(error.response.data.errors);
|
153
|
+
return resolve(error.response.data);
|
154
|
+
});
|
155
|
+
});
|
156
|
+
}
|
157
|
+
exports.runGQL = runGQL;
|