@rsdk/graphql 1.0.4 → 1.0.6
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 +8 -0
- package/dist/graphql.plugin.d.ts +12 -2
- package/dist/graphql.plugin.js +52 -44
- package/dist/graphql.plugin.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +8 -10
- package/src/graphql.plugin.ts +85 -51
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rsdk/graphql",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.6",
|
|
4
4
|
"description": "Plugin for graphql",
|
|
5
5
|
"license": "Apache License 2.0",
|
|
6
6
|
"publishConfig": {
|
|
@@ -10,17 +10,15 @@
|
|
|
10
10
|
"url": "https://github.com/R-Vision/rsdk"
|
|
11
11
|
},
|
|
12
12
|
"main": "dist/index.js",
|
|
13
|
-
"
|
|
13
|
+
"peerDependencies": {
|
|
14
14
|
"@nestjs/apollo": "^11.0.3",
|
|
15
|
-
"@
|
|
16
|
-
"@
|
|
17
|
-
"@rsdk/
|
|
15
|
+
"@nestjs/common": "^9.0.0",
|
|
16
|
+
"@nestjs/graphql": "^11.0.0",
|
|
17
|
+
"@rsdk/common": "^1.0.6",
|
|
18
|
+
"@rsdk/core": "^1.0.6",
|
|
19
|
+
"@rsdk/logging": "^1.0.6",
|
|
18
20
|
"graphql": "^16.6.0",
|
|
19
21
|
"graphql-subscriptions": "^2.0.0"
|
|
20
22
|
},
|
|
21
|
-
"
|
|
22
|
-
"@nestjs/common": "^9.0.0",
|
|
23
|
-
"@nestjs/graphql": "^11.0.0"
|
|
24
|
-
},
|
|
25
|
-
"gitHead": "43e6e42cda9becf15826a38e79ddd4276ac9e1e0"
|
|
23
|
+
"gitHead": "9358fea08177b72c4779d05935011fbd5fa4abc9"
|
|
26
24
|
}
|
package/src/graphql.plugin.ts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { join } from 'path';
|
|
2
|
-
|
|
3
1
|
import type { ApolloDriverConfig } from '@nestjs/apollo';
|
|
4
2
|
import { ApolloDriver } from '@nestjs/apollo';
|
|
5
3
|
import type { MiddlewareConsumer } from '@nestjs/common';
|
|
@@ -25,22 +23,52 @@ const onConnect = (connectionParams: any): any => {
|
|
|
25
23
|
headers.authorization =
|
|
26
24
|
connectionParams?.Authorization || connectionParams?.authorization;
|
|
27
25
|
}
|
|
28
|
-
|
|
29
26
|
return { req: { ...out, headers } };
|
|
30
27
|
};
|
|
31
28
|
|
|
32
|
-
export
|
|
29
|
+
export type Approach = 'schema-first' | 'code-first';
|
|
30
|
+
|
|
31
|
+
export interface GraphQLPluginOptions<T extends Approach> {
|
|
32
|
+
approach: T;
|
|
33
|
+
|
|
33
34
|
path?: string;
|
|
34
35
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
35
36
|
middleware?: Function[];
|
|
36
37
|
}
|
|
37
38
|
|
|
39
|
+
// Schema first
|
|
40
|
+
|
|
41
|
+
export interface GraphQLPluginSchemaFirstOptions
|
|
42
|
+
extends GraphQLPluginOptions<'schema-first'> {
|
|
43
|
+
typePaths: string[];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export const isSchemaFirst = (
|
|
47
|
+
options: GraphQLPluginOptions<Approach>,
|
|
48
|
+
): options is GraphQLPluginSchemaFirstOptions =>
|
|
49
|
+
options.approach === 'schema-first';
|
|
50
|
+
|
|
51
|
+
// Code first
|
|
52
|
+
|
|
53
|
+
export interface GraphQLPluginCodeFirstOptions
|
|
54
|
+
extends GraphQLPluginOptions<'code-first'> {
|
|
55
|
+
autoSchemaFile: string;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export const isCodeFirst = (
|
|
59
|
+
options: GraphQLPluginOptions<Approach>,
|
|
60
|
+
): options is GraphQLPluginCodeFirstOptions =>
|
|
61
|
+
options.approach === 'code-first';
|
|
62
|
+
|
|
38
63
|
export class GraphQLPlugin implements PlatformAppPlugin {
|
|
39
64
|
private readonly path: string;
|
|
40
65
|
|
|
41
|
-
constructor(
|
|
66
|
+
constructor(
|
|
67
|
+
private readonly options:
|
|
68
|
+
| GraphQLPluginCodeFirstOptions
|
|
69
|
+
| GraphQLPluginSchemaFirstOptions,
|
|
70
|
+
) {
|
|
42
71
|
this.path = options.path || '/graphql';
|
|
43
|
-
|
|
44
72
|
if (!this.path.startsWith('/')) {
|
|
45
73
|
this.path = '/' + this.path;
|
|
46
74
|
}
|
|
@@ -51,57 +79,63 @@ export class GraphQLPlugin implements PlatformAppPlugin {
|
|
|
51
79
|
}
|
|
52
80
|
|
|
53
81
|
modules(): any[] {
|
|
54
|
-
const config = PlatformConfigModule.resolve(GraphQLConfig);
|
|
55
|
-
/**
|
|
56
|
-
* FIXME: В целом объект настроек взят из TDP с небольшими изменениями.
|
|
57
|
-
* Смысл некоторых настроек ен совсем понятен. Нужно разобраться, когда
|
|
58
|
-
* будем делать schema-first
|
|
59
|
-
*/
|
|
60
|
-
|
|
61
82
|
return [
|
|
62
|
-
GraphQLModule.
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
autoSchemaFile: join('schema.gql'),
|
|
66
|
-
buildSchemaOptions: {
|
|
67
|
-
numberScalarMode: 'integer',
|
|
68
|
-
},
|
|
69
|
-
// Что это?
|
|
70
|
-
context: ({ req, connection }) => ({ req: req ?? connection.context }),
|
|
83
|
+
GraphQLModule.forRootAsync<ApolloDriverConfig>({
|
|
84
|
+
imports: [PlatformConfigModule.forFeature(GraphQLConfig)], // TODO CHECK IT OUT
|
|
85
|
+
inject: [GraphQLConfig],
|
|
71
86
|
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
87
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
88
|
+
useFactory: (config: GraphQLConfig) => ({
|
|
89
|
+
path: this.path,
|
|
90
|
+
...(isCodeFirst(this.options) && {
|
|
91
|
+
autoSchemaFile: this.options.autoSchemaFile ?? 'schema.graqhql',
|
|
92
|
+
buildSchemaOptions: {
|
|
93
|
+
numberScalarMode: 'integer',
|
|
94
|
+
},
|
|
95
|
+
sortSchema: true,
|
|
96
|
+
}),
|
|
97
|
+
|
|
98
|
+
...(isSchemaFirst(this.options) && {
|
|
99
|
+
typePaths: this.options.typePaths,
|
|
100
|
+
}),
|
|
101
|
+
|
|
102
|
+
// Что это?
|
|
103
|
+
context: ({ req, connection }) => ({
|
|
104
|
+
req: req ?? connection.context,
|
|
105
|
+
}),
|
|
106
|
+
fieldResolverEnhancers: ['filters'],
|
|
107
|
+
formatError: GraphQLErrorsFormatter.finalFormat,
|
|
108
|
+
|
|
109
|
+
logger: new ApolloLoggerAdapter(LoggerFactory.create('Apollo')),
|
|
110
|
+
|
|
111
|
+
installSubscriptionHandlers: true,
|
|
112
|
+
introspection: config.playground,
|
|
113
|
+
playground: config.playground
|
|
114
|
+
? {
|
|
115
|
+
endpoint: this.path,
|
|
116
|
+
subscriptionEndpoint: this.path,
|
|
117
|
+
}
|
|
118
|
+
: false,
|
|
119
|
+
subscriptions: {
|
|
120
|
+
'graphql-ws': {
|
|
97
121
|
onConnect,
|
|
98
122
|
path: this.path,
|
|
99
123
|
},
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
124
|
+
|
|
125
|
+
// Этот вариант нужен (пока - на 23.02.2023) только для поддержки
|
|
126
|
+
// playground. Вероятно, при переходе на Apollo 4 надобность в
|
|
127
|
+
// этом отпадёты
|
|
128
|
+
...(config.playground && {
|
|
129
|
+
'subscriptions-transport-ws': {
|
|
130
|
+
onConnect,
|
|
131
|
+
path: this.path,
|
|
132
|
+
},
|
|
133
|
+
}),
|
|
134
|
+
},
|
|
135
|
+
useGlobalPrefix: true,
|
|
136
|
+
// https://github.com/nestjs/graphql/issues/295#issuecomment-511191060
|
|
137
|
+
// directiveResolvers: ['interceptors'],
|
|
138
|
+
}),
|
|
105
139
|
}),
|
|
106
140
|
];
|
|
107
141
|
}
|