@application-kit/graphql 0.3.4 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/README.md +30 -1
- package/dist/cjs/client/create-apollo-client.d.ts +8 -0
- package/dist/cjs/client/create-apollo-client.js +19 -0
- package/dist/cjs/client/index.d.ts +2 -0
- package/dist/cjs/client/index.js +2 -0
- package/dist/cjs/client/require-apollo-data.d.ts +3 -0
- package/dist/cjs/client/require-apollo-data.js +10 -0
- package/dist/cjs/testing/create-fake-apollo-client.d.ts +4 -0
- package/dist/cjs/testing/create-fake-apollo-client.js +5 -0
- package/dist/cjs/testing/create-fake-graphql-errors.d.ts +9 -0
- package/dist/cjs/testing/create-fake-graphql-errors.js +11 -0
- package/dist/cjs/testing/index.d.ts +2 -0
- package/dist/cjs/testing/index.js +18 -0
- package/dist/esm/client/create-apollo-client.d.ts +8 -0
- package/dist/esm/client/create-apollo-client.js +15 -0
- package/dist/esm/client/index.d.ts +2 -0
- package/dist/esm/client/index.js +2 -0
- package/dist/esm/client/require-apollo-data.d.ts +3 -0
- package/dist/esm/client/require-apollo-data.js +6 -0
- package/dist/esm/testing/create-fake-apollo-client.d.ts +4 -0
- package/dist/esm/testing/create-fake-apollo-client.js +1 -0
- package/dist/esm/testing/create-fake-graphql-errors.d.ts +9 -0
- package/dist/esm/testing/create-fake-graphql-errors.js +6 -0
- package/dist/esm/testing/index.d.ts +2 -0
- package/dist/esm/testing/index.js +2 -0
- package/package.json +13 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# @application-kit/graphql
|
|
2
2
|
|
|
3
|
+
## 0.5.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- a3e984e: Вход `/client`: фабрика `createApolloClient` с колбэком ошибки (флаг `SKIP_GLOBAL_ERROR_TOAST` проверяет сама) и `requireApolloData`.
|
|
8
|
+
|
|
9
|
+
## 0.4.0
|
|
10
|
+
|
|
11
|
+
### Minor Changes
|
|
12
|
+
|
|
13
|
+
- c5dab83: Вход `/testing` для спек потребителя: `createFakeApolloClient`, `createFakeGraphQLErrors`, `createFakeBusinessError`.
|
|
14
|
+
|
|
3
15
|
## 0.3.4
|
|
4
16
|
|
|
5
17
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -19,8 +19,9 @@ Peer-зависимости: `@nestjs/common`, `@nestjs/graphql`, `class-transfo
|
|
|
19
19
|
| Вход | Для чего | Тянет |
|
|
20
20
|
|------|----------|-------|
|
|
21
21
|
| `@application-kit/graphql` | сервер: ошибки, форматтер, типы листинга | `@nestjs/*`, `class-validator` |
|
|
22
|
-
| `@application-kit/graphql/client` | браузер и SSR: чтение `extensions` ответа | только `@apollo/client`, `graphql` (типы) |
|
|
22
|
+
| `@application-kit/graphql/client` | браузер и SSR: сборка клиента, чтение `extensions` ответа | только `@apollo/client`, `graphql` (типы) |
|
|
23
23
|
| `@application-kit/graphql/nest` | периметр Mercurius-сервера: опции драйвера, окружение, конфиг | `mercurius`, `@nestjs/mercurius` (типы), `@libs-for-dev/nestjs-config-module` |
|
|
24
|
+
| `@application-kit/graphql/testing` | спеки потребителя: фейк клиента и ошибки ответа | только `@apollo/client` |
|
|
24
25
|
|
|
25
26
|
Клиентский вход не содержит импортов серверных зависимостей — его можно собирать в браузерный бандл.
|
|
26
27
|
|
|
@@ -46,6 +47,22 @@ export class BusinessGraphQLError extends BaseBusinessGraphQLError<MessageCode>
|
|
|
46
47
|
export type BusinessErrorOptions = BusinessErrorOptionsInterface<MessageCode>
|
|
47
48
|
```
|
|
48
49
|
|
|
50
|
+
## Клиент (`/client`)
|
|
51
|
+
|
|
52
|
+
- `createApolloClient({ http, onError })` — клиент Apollo с `InMemoryCache`, `ErrorLink` и `HttpLink`. `http` — опции
|
|
53
|
+
`HttpLink` как есть. `onError(error)` зовётся на каждую ошибку операции, кроме помеченных флагом
|
|
54
|
+
`SKIP_GLOBAL_ERROR_TOAST` в контексте: их показывает сам вызывающий. Вызывающий получает ошибку в любом случае.
|
|
55
|
+
- `requireApolloData(result)` — данные ответа query; `undefined` вместо них — ошибка транспорта.
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
export const apolloClient = createApolloClient({
|
|
59
|
+
http: { uri: '/graphql' },
|
|
60
|
+
onError: (error) => {
|
|
61
|
+
showToast(messageCodeFromError(error), supportIdFromError(error))
|
|
62
|
+
},
|
|
63
|
+
})
|
|
64
|
+
```
|
|
65
|
+
|
|
49
66
|
## Сервер Mercurius (`/nest`)
|
|
50
67
|
|
|
51
68
|
- `createMercuriusOptions({ schemaPath, maxDepth, playground, resolveSupportId })` — опции `MercuriusDriver`:
|
|
@@ -76,3 +93,15 @@ GraphQLModule.forRootAsync({
|
|
|
76
93
|
|
|
77
94
|
Перевода в пакете нет: он отдаёт данные из ошибки (код сообщения, код обращения, отклонённые символы), а текст для
|
|
78
95
|
пользователя собирает приложение — код → ключ своего словаря → строка на языке пользователя → строка с кодом обращения.
|
|
96
|
+
|
|
97
|
+
## Спеки потребителя (`/testing`)
|
|
98
|
+
|
|
99
|
+
- `createFakeApolloClient(members)` — заглушка с типом настоящего клиента: имена членов проверяет компилятор, поведение
|
|
100
|
+
определяет спека. Приведение типа и его `eslint-disable` живут в пакете.
|
|
101
|
+
- `createFakeGraphQLErrors(...errors)` — настоящий `CombinedGraphQLErrors`, поля `extensions` — через wire-контракт.
|
|
102
|
+
- `createFakeBusinessError(messageCode, extensions?)` — одна бизнес-ошибка с кодом показываемого текста.
|
|
103
|
+
|
|
104
|
+
```ts
|
|
105
|
+
provideApolloClient(createFakeApolloClient({ mutate }))
|
|
106
|
+
mutate.mockRejectedValue(createFakeBusinessError('signIn.invalidCredentials'))
|
|
107
|
+
```
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { ErrorLike } from '@apollo/client/core';
|
|
2
|
+
import { ApolloClient } from '@apollo/client/core';
|
|
3
|
+
import { HttpLink } from '@apollo/client/link/http';
|
|
4
|
+
export interface ApolloClientOptionsInterface {
|
|
5
|
+
http: HttpLink.Options;
|
|
6
|
+
onError(error: ErrorLike): void;
|
|
7
|
+
}
|
|
8
|
+
export declare const createApolloClient: (options: ApolloClientOptionsInterface) => ApolloClient;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createApolloClient = void 0;
|
|
4
|
+
const core_1 = require("@apollo/client/core");
|
|
5
|
+
const error_1 = require("@apollo/client/link/error");
|
|
6
|
+
const http_1 = require("@apollo/client/link/http");
|
|
7
|
+
const request_error_js_1 = require("./request-error.js");
|
|
8
|
+
const createApolloClient = (options) => {
|
|
9
|
+
const errorLink = new error_1.ErrorLink(({ error, operation }) => {
|
|
10
|
+
if (!(0, request_error_js_1.shouldSkipGlobalErrorToast)(operation.getContext())) {
|
|
11
|
+
options.onError(error);
|
|
12
|
+
}
|
|
13
|
+
});
|
|
14
|
+
return new core_1.ApolloClient({
|
|
15
|
+
cache: new core_1.InMemoryCache(),
|
|
16
|
+
link: core_1.ApolloLink.from([errorLink, new http_1.HttpLink(options.http)]),
|
|
17
|
+
});
|
|
18
|
+
};
|
|
19
|
+
exports.createApolloClient = createApolloClient;
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
export * from './create-apollo-client.js';
|
|
1
2
|
export * from './message-code-from-error.js';
|
|
2
3
|
export * from './rejected-characters-from-error.js';
|
|
3
4
|
export * from './request-error.js';
|
|
5
|
+
export * from './require-apollo-data.js';
|
|
4
6
|
export * from './support-id-from-error.js';
|
package/dist/cjs/client/index.js
CHANGED
|
@@ -14,7 +14,9 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./create-apollo-client.js"), exports);
|
|
17
18
|
__exportStar(require("./message-code-from-error.js"), exports);
|
|
18
19
|
__exportStar(require("./rejected-characters-from-error.js"), exports);
|
|
19
20
|
__exportStar(require("./request-error.js"), exports);
|
|
21
|
+
__exportStar(require("./require-apollo-data.js"), exports);
|
|
20
22
|
__exportStar(require("./support-id-from-error.js"), exports);
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.requireApolloData = void 0;
|
|
4
|
+
const requireApolloData = (result) => {
|
|
5
|
+
if (result.data === undefined) {
|
|
6
|
+
throw new Error('No data returned from Apollo Client');
|
|
7
|
+
}
|
|
8
|
+
return result.data;
|
|
9
|
+
};
|
|
10
|
+
exports.requireApolloData = requireApolloData;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { CombinedGraphQLErrors } from '@apollo/client/errors';
|
|
2
|
+
import type { GraphQLErrorWireExtensions } from '../contract/graphql-error-contract.js';
|
|
3
|
+
interface FakeWireErrorInterface {
|
|
4
|
+
extensions?: GraphQLErrorWireExtensions;
|
|
5
|
+
message: string;
|
|
6
|
+
}
|
|
7
|
+
export declare const createFakeGraphQLErrors: (...errors: FakeWireErrorInterface[]) => CombinedGraphQLErrors;
|
|
8
|
+
export declare const createFakeBusinessError: (messageCode: string, extensions?: GraphQLErrorWireExtensions) => CombinedGraphQLErrors;
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createFakeBusinessError = exports.createFakeGraphQLErrors = void 0;
|
|
4
|
+
const errors_1 = require("@apollo/client/errors");
|
|
5
|
+
const createFakeGraphQLErrors = (...errors) => new errors_1.CombinedGraphQLErrors({ errors });
|
|
6
|
+
exports.createFakeGraphQLErrors = createFakeGraphQLErrors;
|
|
7
|
+
const createFakeBusinessError = (messageCode, extensions = {}) => (0, exports.createFakeGraphQLErrors)({
|
|
8
|
+
extensions: { ...extensions, messageCode },
|
|
9
|
+
message: 'technical message',
|
|
10
|
+
});
|
|
11
|
+
exports.createFakeBusinessError = createFakeBusinessError;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./create-fake-apollo-client.js"), exports);
|
|
18
|
+
__exportStar(require("./create-fake-graphql-errors.js"), exports);
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { ErrorLike } from '@apollo/client/core';
|
|
2
|
+
import { ApolloClient } from '@apollo/client/core';
|
|
3
|
+
import { HttpLink } from '@apollo/client/link/http';
|
|
4
|
+
export interface ApolloClientOptionsInterface {
|
|
5
|
+
http: HttpLink.Options;
|
|
6
|
+
onError(error: ErrorLike): void;
|
|
7
|
+
}
|
|
8
|
+
export declare const createApolloClient: (options: ApolloClientOptionsInterface) => ApolloClient;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { ApolloClient, ApolloLink, InMemoryCache } from '@apollo/client/core';
|
|
2
|
+
import { ErrorLink } from '@apollo/client/link/error';
|
|
3
|
+
import { HttpLink } from '@apollo/client/link/http';
|
|
4
|
+
import { shouldSkipGlobalErrorToast } from './request-error.js';
|
|
5
|
+
export const createApolloClient = (options) => {
|
|
6
|
+
const errorLink = new ErrorLink(({ error, operation }) => {
|
|
7
|
+
if (!shouldSkipGlobalErrorToast(operation.getContext())) {
|
|
8
|
+
options.onError(error);
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
return new ApolloClient({
|
|
12
|
+
cache: new InMemoryCache(),
|
|
13
|
+
link: ApolloLink.from([errorLink, new HttpLink(options.http)]),
|
|
14
|
+
});
|
|
15
|
+
};
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
export * from './create-apollo-client.js';
|
|
1
2
|
export * from './message-code-from-error.js';
|
|
2
3
|
export * from './rejected-characters-from-error.js';
|
|
3
4
|
export * from './request-error.js';
|
|
5
|
+
export * from './require-apollo-data.js';
|
|
4
6
|
export * from './support-id-from-error.js';
|
package/dist/esm/client/index.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
export * from './create-apollo-client.js';
|
|
1
2
|
export * from './message-code-from-error.js';
|
|
2
3
|
export * from './rejected-characters-from-error.js';
|
|
3
4
|
export * from './request-error.js';
|
|
5
|
+
export * from './require-apollo-data.js';
|
|
4
6
|
export * from './support-id-from-error.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const createFakeApolloClient = (members = {}) => members;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { CombinedGraphQLErrors } from '@apollo/client/errors';
|
|
2
|
+
import type { GraphQLErrorWireExtensions } from '../contract/graphql-error-contract.js';
|
|
3
|
+
interface FakeWireErrorInterface {
|
|
4
|
+
extensions?: GraphQLErrorWireExtensions;
|
|
5
|
+
message: string;
|
|
6
|
+
}
|
|
7
|
+
export declare const createFakeGraphQLErrors: (...errors: FakeWireErrorInterface[]) => CombinedGraphQLErrors;
|
|
8
|
+
export declare const createFakeBusinessError: (messageCode: string, extensions?: GraphQLErrorWireExtensions) => CombinedGraphQLErrors;
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { CombinedGraphQLErrors } from '@apollo/client/errors';
|
|
2
|
+
export const createFakeGraphQLErrors = (...errors) => new CombinedGraphQLErrors({ errors });
|
|
3
|
+
export const createFakeBusinessError = (messageCode, extensions = {}) => createFakeGraphQLErrors({
|
|
4
|
+
extensions: { ...extensions, messageCode },
|
|
5
|
+
message: 'technical message',
|
|
6
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@application-kit/graphql",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Контракт ошибок GraphQL (сервер и клиент) и типы курсорного листинга для NestJS",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"apollo",
|
|
@@ -55,7 +55,17 @@
|
|
|
55
55
|
"default": "./dist/cjs/nest/index.js"
|
|
56
56
|
}
|
|
57
57
|
},
|
|
58
|
-
"./package.json": "./package.json"
|
|
58
|
+
"./package.json": "./package.json",
|
|
59
|
+
"./testing": {
|
|
60
|
+
"import": {
|
|
61
|
+
"types": "./dist/esm/testing/index.d.ts",
|
|
62
|
+
"default": "./dist/esm/testing/index.js"
|
|
63
|
+
},
|
|
64
|
+
"require": {
|
|
65
|
+
"types": "./dist/cjs/testing/index.d.ts",
|
|
66
|
+
"default": "./dist/cjs/testing/index.js"
|
|
67
|
+
}
|
|
68
|
+
}
|
|
59
69
|
},
|
|
60
70
|
"main": "./dist/cjs/index.js",
|
|
61
71
|
"module": "./dist/esm/index.js",
|
|
@@ -64,7 +74,7 @@
|
|
|
64
74
|
"dist"
|
|
65
75
|
],
|
|
66
76
|
"scripts": {
|
|
67
|
-
"build": "rm -rf ./dist && node ../../scripts/barrels.mjs create ./src --entry-only=client,nest && tsc --project tsconfig.build.json && tsc --project tsconfig.build.cjs.json && node ../../scripts/barrels.mjs clean ./src && node ../../scripts/mark-cjs.mjs ./dist/cjs",
|
|
77
|
+
"build": "rm -rf ./dist && node ../../scripts/barrels.mjs create ./src --entry-only=client,nest,testing && tsc --project tsconfig.build.json && tsc --project tsconfig.build.cjs.json && node ../../scripts/barrels.mjs clean ./src && node ../../scripts/mark-cjs.mjs ./dist/cjs",
|
|
68
78
|
"lint:tsc": "tsc --noEmit --pretty --project .",
|
|
69
79
|
"test:mutation": "stryker run",
|
|
70
80
|
"test:unit": "vitest run"
|