@application-kit/graphql 0.4.0 → 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 CHANGED
@@ -1,5 +1,11 @@
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
+
3
9
  ## 0.4.0
4
10
 
5
11
  ### Minor Changes
package/README.md CHANGED
@@ -19,7 +19,7 @@ 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
24
  | `@application-kit/graphql/testing` | спеки потребителя: фейк клиента и ошибки ответа | только `@apollo/client` |
25
25
 
@@ -47,6 +47,22 @@ export class BusinessGraphQLError extends BaseBusinessGraphQLError<MessageCode>
47
47
  export type BusinessErrorOptions = BusinessErrorOptionsInterface<MessageCode>
48
48
  ```
49
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
+
50
66
  ## Сервер Mercurius (`/nest`)
51
67
 
52
68
  - `createMercuriusOptions({ schemaPath, maxDepth, playground, resolveSupportId })` — опции `MercuriusDriver`:
@@ -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';
@@ -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,3 @@
1
+ export declare const requireApolloData: <Data>(result: {
2
+ data: Data | undefined;
3
+ }) => Data;
@@ -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,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';
@@ -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,3 @@
1
+ export declare const requireApolloData: <Data>(result: {
2
+ data: Data | undefined;
3
+ }) => Data;
@@ -0,0 +1,6 @@
1
+ export const requireApolloData = (result) => {
2
+ if (result.data === undefined) {
3
+ throw new Error('No data returned from Apollo Client');
4
+ }
5
+ return result.data;
6
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@application-kit/graphql",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "description": "Контракт ошибок GraphQL (сервер и клиент) и типы курсорного листинга для NestJS",
5
5
  "keywords": [
6
6
  "apollo",