@rsdk/graphql 0.14.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/.eslintignore +4 -0
- package/.eslintrc.js +4 -0
- package/.prettierignore +1 -0
- package/.prettierrc.json +1 -0
- package/dist/apollo-logger.adapter.d.ts +10 -0
- package/dist/apollo-logger.adapter.js +23 -0
- package/dist/apollo-logger.adapter.js.map +1 -0
- package/dist/error-handling/graphql-errors.formatter.d.ts +17 -0
- package/dist/error-handling/graphql-errors.formatter.js +69 -0
- package/dist/error-handling/graphql-errors.formatter.js.map +1 -0
- package/dist/error-handling/graphql-errors.sender.d.ts +7 -0
- package/dist/error-handling/graphql-errors.sender.js +14 -0
- package/dist/error-handling/graphql-errors.sender.js.map +1 -0
- package/dist/error-handling/index.d.ts +2 -0
- package/dist/error-handling/index.js +19 -0
- package/dist/error-handling/index.js.map +1 -0
- package/dist/graphql.config.d.ts +4 -0
- package/dist/graphql.config.js +28 -0
- package/dist/graphql.config.js.map +1 -0
- package/dist/graphql.plugin.d.ts +16 -0
- package/dist/graphql.plugin.js +98 -0
- package/dist/graphql.plugin.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +18 -0
- package/dist/index.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +42 -0
- package/src/apollo-logger.adapter.ts +22 -0
- package/src/error-handling/graphql-errors.formatter.ts +82 -0
- package/src/error-handling/graphql-errors.sender.ts +14 -0
- package/src/error-handling/index.ts +2 -0
- package/src/graphql.config.ts +10 -0
- package/src/graphql.plugin.ts +122 -0
- package/src/index.ts +1 -0
- package/tsconfig.json +9 -0
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@rsdk/graphql",
|
|
3
|
+
"version": "0.14.0",
|
|
4
|
+
"description": "Plugin for graphql",
|
|
5
|
+
"license": "Apache License 2.0",
|
|
6
|
+
"publishConfig": {
|
|
7
|
+
"access": "public"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"url": "https://github.com/R-Vision/rsdk"
|
|
11
|
+
},
|
|
12
|
+
"main": "dist/index.js",
|
|
13
|
+
"scripts": {
|
|
14
|
+
"build": "rm -rf dist/ && npm run compile",
|
|
15
|
+
"build:watch": "rm -rf dist/ && npm run compile:watch",
|
|
16
|
+
"compile": "tsc",
|
|
17
|
+
"compile:watch": "npm run compile -- -w",
|
|
18
|
+
"clean": "rm -rf dist/ node_modules/",
|
|
19
|
+
"prepublishOnly": "npm run build",
|
|
20
|
+
"lint": "eslint \"src/**/*.ts\" -f codeframe",
|
|
21
|
+
"lint:fix": "eslint \"src/**/*.ts\" -f codeframe --fix"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@nestjs/apollo": "^11.0.3",
|
|
25
|
+
"@rsdk/common": "^0.14.0",
|
|
26
|
+
"@rsdk/core": "^0.14.0",
|
|
27
|
+
"@rsdk/logging": "^0.14.0",
|
|
28
|
+
"graphql": "^16.6.0",
|
|
29
|
+
"graphql-subscriptions": "^2.0.0"
|
|
30
|
+
},
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"@nestjs/common": "^9.0.0",
|
|
33
|
+
"@nestjs/graphql": "^11.0.0"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@rsdk/prettier-config": "*",
|
|
37
|
+
"@rsdk/tsconfig": "*",
|
|
38
|
+
"@types/graphql-upload": "^15.0.2",
|
|
39
|
+
"eslint-config-rsdk": "*"
|
|
40
|
+
},
|
|
41
|
+
"gitHead": "e05337f5e9aa6242fe74f7ca5a4b3122765a77c0"
|
|
42
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { Logger } from '@apollo/utils.logger';
|
|
2
|
+
import type { ILogger } from '@rsdk/logging';
|
|
3
|
+
|
|
4
|
+
export class ApolloLoggerAdapter implements Logger {
|
|
5
|
+
constructor(private readonly logger: ILogger) {}
|
|
6
|
+
|
|
7
|
+
debug(message?: any): void {
|
|
8
|
+
this.logger.debug(message);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
info(message?: any): void {
|
|
12
|
+
this.logger.info(message);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
warn(message?: any): void {
|
|
16
|
+
this.logger.warn(message);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
error(message?: any): void {
|
|
20
|
+
this.logger.error(message);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import type { ArgumentsHost } from '@nestjs/common';
|
|
2
|
+
import { normalizer, sortErrorKeys } from '@rsdk/common';
|
|
3
|
+
import type { IErrorsFormatter } from '@rsdk/core';
|
|
4
|
+
import { ExceptionKind, PipelineException } from '@rsdk/core';
|
|
5
|
+
import type { GraphQLFormattedError } from 'graphql';
|
|
6
|
+
import { GraphQLError } from 'graphql';
|
|
7
|
+
|
|
8
|
+
const ALWAYS_VERBOSE = [ExceptionKind.INPUT];
|
|
9
|
+
const DEFAULT_MESSAGE = 'Something went wrong';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Форматирование ошибок происходит в 2 этапа:
|
|
13
|
+
* 1) в GlobalExceptionFilter при помощи данного форматтера. На этом этапе в extentions
|
|
14
|
+
* кладётся вся необходимая информация в зависимости от параметра VERBOSE_ERRORS.
|
|
15
|
+
* 2) во встроенном в Apollo обработчике formatError (finalFormat) - там происходит удаление
|
|
16
|
+
* лишних данных (которые apollo неизбежно добавляет уже после обработки ошибки
|
|
17
|
+
* в ErrorsFormatter) и нормализация объектов.
|
|
18
|
+
*/
|
|
19
|
+
export class GraphQLErrorsFormatter implements IErrorsFormatter {
|
|
20
|
+
static finalFormat(
|
|
21
|
+
formattedError: GraphQLFormattedError,
|
|
22
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
23
|
+
error: unknown,
|
|
24
|
+
): GraphQLFormattedError {
|
|
25
|
+
delete formattedError.extensions?.exception;
|
|
26
|
+
|
|
27
|
+
return {
|
|
28
|
+
message: formattedError.message,
|
|
29
|
+
...(formattedError.path && {
|
|
30
|
+
path: formattedError.path,
|
|
31
|
+
}),
|
|
32
|
+
|
|
33
|
+
...(formattedError.locations && {
|
|
34
|
+
locations: formattedError.locations,
|
|
35
|
+
}),
|
|
36
|
+
|
|
37
|
+
extensions: normalizer()(formattedError.extensions, {
|
|
38
|
+
// This means stacktrace will never be sent to user
|
|
39
|
+
filterKeys: ['stack'],
|
|
40
|
+
sortKeys: sortErrorKeys,
|
|
41
|
+
}) as Record<string, unknown>,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
match(host: ArgumentsHost): boolean {
|
|
46
|
+
return host.getType<'graphql'>() === 'graphql';
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
format(ex: unknown, verbose?: boolean): GraphQLError {
|
|
50
|
+
if (ex instanceof PipelineException) {
|
|
51
|
+
const formatVerbose = verbose || ALWAYS_VERBOSE.includes(ex.kind);
|
|
52
|
+
|
|
53
|
+
const { kind, code, details, cause } = ex;
|
|
54
|
+
|
|
55
|
+
const message = formatVerbose
|
|
56
|
+
? [ex.message, ...PipelineException.innerMessages(ex)].join(' > ')
|
|
57
|
+
: DEFAULT_MESSAGE;
|
|
58
|
+
|
|
59
|
+
return new GraphQLError(message, {
|
|
60
|
+
extensions: {
|
|
61
|
+
kind: ExceptionKind[kind],
|
|
62
|
+
|
|
63
|
+
code,
|
|
64
|
+
/**
|
|
65
|
+
* We don't normalize object here because it fill be normalized
|
|
66
|
+
* in formatError method of apollo server
|
|
67
|
+
*/
|
|
68
|
+
...(verbose && {
|
|
69
|
+
cause,
|
|
70
|
+
details,
|
|
71
|
+
}),
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const message = verbose
|
|
77
|
+
? (ex as any).message ?? JSON.stringify(ex)
|
|
78
|
+
: DEFAULT_MESSAGE;
|
|
79
|
+
|
|
80
|
+
return new GraphQLError(message);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { ArgumentsHost } from '@nestjs/common';
|
|
2
|
+
import type { IErrorsSender } from '@rsdk/core';
|
|
3
|
+
import type { Observable } from 'rxjs';
|
|
4
|
+
|
|
5
|
+
export class GraphQLErrorsSender implements IErrorsSender {
|
|
6
|
+
match(host: ArgumentsHost): boolean {
|
|
7
|
+
return host.getType<'graphql'>() === 'graphql';
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
send(host: ArgumentsHost, ex: unknown): Observable<never> {
|
|
11
|
+
// Only throwing will provision exception data to apollo-server
|
|
12
|
+
throw ex;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { BoolParser, Config, ConfigSection, Property } from '@rsdk/core';
|
|
2
|
+
|
|
3
|
+
@ConfigSection()
|
|
4
|
+
export class GraphQLConfig extends Config {
|
|
5
|
+
@Property('GRAPHQL_ENABLE_PLAYGROUND', new BoolParser(), {
|
|
6
|
+
defaultValue: false,
|
|
7
|
+
description: 'Enables GraphQL playground and introspection',
|
|
8
|
+
})
|
|
9
|
+
playground!: boolean;
|
|
10
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { join } from 'path';
|
|
2
|
+
|
|
3
|
+
import type { ApolloDriverConfig } from '@nestjs/apollo';
|
|
4
|
+
import { ApolloDriver } from '@nestjs/apollo';
|
|
5
|
+
import type { MiddlewareConsumer } from '@nestjs/common';
|
|
6
|
+
import { GraphQLModule } from '@nestjs/graphql';
|
|
7
|
+
import type {
|
|
8
|
+
IErrorsFormatter,
|
|
9
|
+
IErrorsSender,
|
|
10
|
+
PlatformAppPlugin,
|
|
11
|
+
} from '@rsdk/core';
|
|
12
|
+
import { PlatformConfigModule } from '@rsdk/core';
|
|
13
|
+
import { LoggerFactory } from '@rsdk/logging';
|
|
14
|
+
|
|
15
|
+
import { ApolloLoggerAdapter } from './apollo-logger.adapter';
|
|
16
|
+
import { GraphQLErrorsFormatter, GraphQLErrorsSender } from './error-handling';
|
|
17
|
+
import { GraphQLConfig } from './graphql.config';
|
|
18
|
+
|
|
19
|
+
const onConnect = (connectionParams: any): any => {
|
|
20
|
+
const out = connectionParams ?? {};
|
|
21
|
+
const headers = out.headers ?? {};
|
|
22
|
+
|
|
23
|
+
// workaround for playground
|
|
24
|
+
if (connectionParams?.Authorization || connectionParams?.authorization) {
|
|
25
|
+
headers.authorization =
|
|
26
|
+
connectionParams?.Authorization || connectionParams?.authorization;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return { req: { ...out, headers } };
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export interface GraphQLPluginOptions {
|
|
33
|
+
path?: string;
|
|
34
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
35
|
+
middleware?: Function[];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export class GraphQLPlugin implements PlatformAppPlugin {
|
|
39
|
+
private readonly path: string;
|
|
40
|
+
|
|
41
|
+
constructor(private readonly options: GraphQLPluginOptions = {}) {
|
|
42
|
+
this.path = options.path || '/graphql';
|
|
43
|
+
|
|
44
|
+
if (!this.path.startsWith('/')) {
|
|
45
|
+
this.path = '/' + this.path;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
forTransports(): Readonly<string[]> {
|
|
50
|
+
return ['http'];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
modules(): any[] {
|
|
54
|
+
const config = PlatformConfigModule.resolve(GraphQLConfig);
|
|
55
|
+
/**
|
|
56
|
+
* FIXME: В целом объект настроек взят из TDP с небольшими изменениями.
|
|
57
|
+
* Смысл некоторых настроек ен совсем понятен. Нужно разобраться, когда
|
|
58
|
+
* будем делать schema-first
|
|
59
|
+
*/
|
|
60
|
+
|
|
61
|
+
return [
|
|
62
|
+
GraphQLModule.forRoot<ApolloDriverConfig>({
|
|
63
|
+
path: this.path,
|
|
64
|
+
|
|
65
|
+
autoSchemaFile: join('schema.gql'),
|
|
66
|
+
buildSchemaOptions: {
|
|
67
|
+
numberScalarMode: 'integer',
|
|
68
|
+
},
|
|
69
|
+
// Что это?
|
|
70
|
+
context: ({ req, connection }) => ({ req: req ?? connection.context }),
|
|
71
|
+
driver: ApolloDriver,
|
|
72
|
+
fieldResolverEnhancers: ['filters'],
|
|
73
|
+
formatError: GraphQLErrorsFormatter.finalFormat,
|
|
74
|
+
sortSchema: true,
|
|
75
|
+
|
|
76
|
+
logger: new ApolloLoggerAdapter(LoggerFactory.create('Apollo')),
|
|
77
|
+
|
|
78
|
+
installSubscriptionHandlers: true,
|
|
79
|
+
introspection: config.playground,
|
|
80
|
+
playground: config.playground
|
|
81
|
+
? {
|
|
82
|
+
endpoint: this.path,
|
|
83
|
+
subscriptionEndpoint: this.path,
|
|
84
|
+
}
|
|
85
|
+
: false,
|
|
86
|
+
subscriptions: {
|
|
87
|
+
'graphql-ws': {
|
|
88
|
+
onConnect,
|
|
89
|
+
path: this.path,
|
|
90
|
+
},
|
|
91
|
+
|
|
92
|
+
// Этот вариант нужен (пока - на 23.02.2023) только для поддержки
|
|
93
|
+
// playground. Вероятно, при переходе на Apollo 4 надобность в
|
|
94
|
+
// этом отпадёты
|
|
95
|
+
...(config.playground && {
|
|
96
|
+
'subscriptions-transport-ws': {
|
|
97
|
+
onConnect,
|
|
98
|
+
path: this.path,
|
|
99
|
+
},
|
|
100
|
+
}),
|
|
101
|
+
},
|
|
102
|
+
useGlobalPrefix: true,
|
|
103
|
+
// https://github.com/nestjs/graphql/issues/295#issuecomment-511191060
|
|
104
|
+
// directiveResolvers: ['interceptors'],
|
|
105
|
+
}),
|
|
106
|
+
];
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
configureMiddleware(consumer: MiddlewareConsumer): void {
|
|
110
|
+
for (const middleware of this.options.middleware || []) {
|
|
111
|
+
consumer.apply(middleware).forRoutes(this.path);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
errorFormatter(): IErrorsFormatter {
|
|
116
|
+
return new GraphQLErrorsFormatter();
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
errorSender(): IErrorsSender {
|
|
120
|
+
return new GraphQLErrorsSender();
|
|
121
|
+
}
|
|
122
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './graphql.plugin';
|