@tinkerstack/graphql-annotation 0.0.0 → 0.0.2
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/dist/index.mjs
CHANGED
|
@@ -1,16 +1,5 @@
|
|
|
1
1
|
var __defProp = Object.defineProperty;
|
|
2
|
-
var
|
|
3
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
4
|
-
var __decorateClass = (decorators, target, key, kind) => {
|
|
5
|
-
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
|
|
6
|
-
for (var i = decorators.length - 1, decorator; i >= 0; i--)
|
|
7
|
-
if (decorator = decorators[i])
|
|
8
|
-
result = (kind ? decorator(target, key, result) : decorator(result)) || result;
|
|
9
|
-
if (kind && result) __defProp(target, key, result);
|
|
10
|
-
return result;
|
|
11
|
-
};
|
|
12
|
-
var __decorateParam = (index, decorator) => (target, key) => decorator(target, key, index);
|
|
13
|
-
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
14
3
|
|
|
15
4
|
// src/modules/core/annotation.decorators.ts
|
|
16
5
|
import { SetMetadata } from "@nestjs/common";
|
|
@@ -48,47 +37,38 @@ function Annotate(name, opts) {
|
|
|
48
37
|
case "number": {
|
|
49
38
|
const paramIndex = descriptorOrParameterIndex;
|
|
50
39
|
const paramName = opts?.parameter;
|
|
51
|
-
if (!paramName)
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
paramName
|
|
67
|
-
}
|
|
40
|
+
if (!paramName) throw new Error(`Unable to apply annotation "${annotationName}". Parameter name not specified.`);
|
|
41
|
+
Args({
|
|
42
|
+
name: paramName,
|
|
43
|
+
type: opts?.type,
|
|
44
|
+
description: opts?.description
|
|
45
|
+
})(target, propertyKey, paramIndex);
|
|
46
|
+
_updateAnnotation(target.constructor, (meta) => ({
|
|
47
|
+
...meta,
|
|
48
|
+
[`${annotationName}@${paramIndex}`]: {
|
|
49
|
+
annotation: annotationName,
|
|
50
|
+
data: annotationData,
|
|
51
|
+
target: {
|
|
52
|
+
type: "parameter",
|
|
53
|
+
paramIndex,
|
|
54
|
+
paramName
|
|
68
55
|
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
);
|
|
56
|
+
}
|
|
57
|
+
}), propertyKey);
|
|
72
58
|
break;
|
|
73
59
|
}
|
|
74
60
|
default:
|
|
75
|
-
throw new Error(
|
|
76
|
-
`Unable to attach annotation "${name}". Unsupported target.`
|
|
77
|
-
);
|
|
61
|
+
throw new Error(`Unable to attach annotation "${name}". Unsupported target.`);
|
|
78
62
|
}
|
|
79
63
|
};
|
|
80
64
|
}
|
|
65
|
+
__name(Annotate, "Annotate");
|
|
81
66
|
function _updateAnnotation(target, updateFn, propertyKey) {
|
|
82
67
|
const meta = Reflect.getMetadata(ANNOTATION_METADATA, target) ?? {};
|
|
83
|
-
if (propertyKey)
|
|
84
|
-
Reflect.defineMetadata(
|
|
85
|
-
ANNOTATION_METADATA,
|
|
86
|
-
updateFn(meta),
|
|
87
|
-
target,
|
|
88
|
-
propertyKey
|
|
89
|
-
);
|
|
68
|
+
if (propertyKey) Reflect.defineMetadata(ANNOTATION_METADATA, updateFn(meta), target, propertyKey);
|
|
90
69
|
else Reflect.defineMetadata(ANNOTATION_METADATA, updateFn(meta), target);
|
|
91
70
|
}
|
|
71
|
+
__name(_updateAnnotation, "_updateAnnotation");
|
|
92
72
|
function ResolveAnnotation(name) {
|
|
93
73
|
return (target, property, descriptor) => {
|
|
94
74
|
SetMetadata(ANNOTATION_RESOLVER_METADATA, {
|
|
@@ -96,43 +76,119 @@ function ResolveAnnotation(name) {
|
|
|
96
76
|
})(target, property, descriptor);
|
|
97
77
|
};
|
|
98
78
|
}
|
|
79
|
+
__name(ResolveAnnotation, "ResolveAnnotation");
|
|
99
80
|
|
|
100
81
|
// src/modules/core/annotation.loader.ts
|
|
101
82
|
import { stitchSchemas } from "@graphql-tools/stitch";
|
|
102
83
|
import { MapperKind, mapSchema } from "@graphql-tools/utils";
|
|
103
84
|
import { FilterRootFields } from "@graphql-tools/wrap";
|
|
104
|
-
import { Injectable as
|
|
105
|
-
import {
|
|
106
|
-
|
|
107
|
-
} from "@nestjs/graphql";
|
|
85
|
+
import { Injectable as Injectable3, Logger as Logger2 } from "@nestjs/common";
|
|
86
|
+
import { ExternalContextCreator, MetadataScanner as MetadataScanner2 } from "@nestjs/core";
|
|
87
|
+
import { PARAM_ARGS_METADATA } from "@nestjs/graphql";
|
|
108
88
|
import { GqlParamtype } from "@nestjs/graphql/dist/enums/gql-paramtype.enum";
|
|
109
89
|
import { GraphQLClient } from "graphql-request";
|
|
110
90
|
|
|
111
91
|
// src/modules/core/annotation.resolver.ts
|
|
112
|
-
import { Inject, Injectable, Logger } from "@nestjs/common";
|
|
92
|
+
import { Inject, Injectable as Injectable2, Logger } from "@nestjs/common";
|
|
93
|
+
import { MetadataScanner } from "@nestjs/core";
|
|
113
94
|
import { Field, ObjectType, Query, Resolver } from "@nestjs/graphql";
|
|
114
95
|
import { GraphQLJSONObject } from "graphql-scalars";
|
|
115
96
|
import "reflect-metadata";
|
|
97
|
+
|
|
98
|
+
// src/modules/core/resolver.explorer.ts
|
|
99
|
+
import { Injectable } from "@nestjs/common";
|
|
100
|
+
import { DiscoveryService } from "@nestjs/core";
|
|
101
|
+
import { RESOLVER_TYPE_METADATA } from "@nestjs/graphql";
|
|
102
|
+
function _ts_decorate(decorators, target, key, desc) {
|
|
103
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
104
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
105
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
106
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
107
|
+
}
|
|
108
|
+
__name(_ts_decorate, "_ts_decorate");
|
|
109
|
+
function _ts_metadata(k, v) {
|
|
110
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
111
|
+
}
|
|
112
|
+
__name(_ts_metadata, "_ts_metadata");
|
|
113
|
+
var ResolverDiscoveryService = class {
|
|
114
|
+
static {
|
|
115
|
+
__name(this, "ResolverDiscoveryService");
|
|
116
|
+
}
|
|
117
|
+
discoveryService;
|
|
118
|
+
constructor(discoveryService) {
|
|
119
|
+
this.discoveryService = discoveryService;
|
|
120
|
+
}
|
|
121
|
+
explore() {
|
|
122
|
+
const wrappedProviders = this.discoveryService.getProviders();
|
|
123
|
+
return wrappedProviders.flatMap((wrapped) => {
|
|
124
|
+
const { instance } = wrapped;
|
|
125
|
+
if (!instance) return [];
|
|
126
|
+
const isResolver = Reflect.hasMetadata(RESOLVER_TYPE_METADATA, instance.constructor);
|
|
127
|
+
if (!isResolver) return [];
|
|
128
|
+
return wrapped;
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
ResolverDiscoveryService = _ts_decorate([
|
|
133
|
+
Injectable(),
|
|
134
|
+
_ts_metadata("design:type", Function),
|
|
135
|
+
_ts_metadata("design:paramtypes", [
|
|
136
|
+
typeof DiscoveryService === "undefined" ? Object : DiscoveryService
|
|
137
|
+
])
|
|
138
|
+
], ResolverDiscoveryService);
|
|
139
|
+
|
|
140
|
+
// src/modules/core/annotation.resolver.ts
|
|
141
|
+
function _ts_decorate2(decorators, target, key, desc) {
|
|
142
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
143
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
144
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
145
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
146
|
+
}
|
|
147
|
+
__name(_ts_decorate2, "_ts_decorate");
|
|
148
|
+
function _ts_metadata2(k, v) {
|
|
149
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
150
|
+
}
|
|
151
|
+
__name(_ts_metadata2, "_ts_metadata");
|
|
152
|
+
function _ts_param(paramIndex, decorator) {
|
|
153
|
+
return function(target, key) {
|
|
154
|
+
decorator(target, key, paramIndex);
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
__name(_ts_param, "_ts_param");
|
|
116
158
|
var GraphQLAnnotationMeta = class {
|
|
159
|
+
static {
|
|
160
|
+
__name(this, "GraphQLAnnotationMeta");
|
|
161
|
+
}
|
|
117
162
|
name;
|
|
118
163
|
resolvers;
|
|
119
164
|
};
|
|
120
|
-
|
|
121
|
-
Field(() => String)
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
165
|
+
_ts_decorate2([
|
|
166
|
+
Field(() => String),
|
|
167
|
+
_ts_metadata2("design:type", String)
|
|
168
|
+
], GraphQLAnnotationMeta.prototype, "name", void 0);
|
|
169
|
+
_ts_decorate2([
|
|
170
|
+
Field(() => GraphQLJSONObject, {
|
|
171
|
+
name: "resolvers"
|
|
172
|
+
}),
|
|
173
|
+
_ts_metadata2("design:type", typeof GraphQLResolversAnnotations === "undefined" ? Object : GraphQLResolversAnnotations)
|
|
174
|
+
], GraphQLAnnotationMeta.prototype, "resolvers", void 0);
|
|
175
|
+
GraphQLAnnotationMeta = _ts_decorate2([
|
|
127
176
|
ObjectType()
|
|
128
177
|
], GraphQLAnnotationMeta);
|
|
178
|
+
var ANNOTATION_QUERY_NAME = "_annotations";
|
|
129
179
|
var GraphQLAnnotationResolver = class {
|
|
180
|
+
static {
|
|
181
|
+
__name(this, "GraphQLAnnotationResolver");
|
|
182
|
+
}
|
|
183
|
+
metadataScanner;
|
|
184
|
+
resolverDiscoveryService;
|
|
185
|
+
meta;
|
|
186
|
+
_logger = new Logger("GraphQLAnnotationModule");
|
|
130
187
|
constructor(metadataScanner, resolverDiscoveryService, meta) {
|
|
131
188
|
this.metadataScanner = metadataScanner;
|
|
132
189
|
this.resolverDiscoveryService = resolverDiscoveryService;
|
|
133
190
|
this.meta = meta;
|
|
134
191
|
}
|
|
135
|
-
_logger = new Logger("GraphQLAnnotationModule");
|
|
136
192
|
async getGraphQLAnnotations() {
|
|
137
193
|
return {
|
|
138
194
|
...this.meta,
|
|
@@ -146,41 +202,36 @@ var GraphQLAnnotationResolver = class {
|
|
|
146
202
|
compileResolversAnnotations() {
|
|
147
203
|
const instances = this.resolverDiscoveryService.explore();
|
|
148
204
|
const resolversAnnotations = {};
|
|
149
|
-
for (const { instance } of instances)
|
|
150
|
-
|
|
151
|
-
Object.
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
Reflect.getMetadata(
|
|
159
|
-
ANNOTATION_METADATA,
|
|
160
|
-
instance.constructor,
|
|
161
|
-
methodName
|
|
162
|
-
) ?? {}
|
|
163
|
-
)
|
|
164
|
-
];
|
|
165
|
-
if (annotations.length <= 0) continue;
|
|
166
|
-
resolversAnnotations[methodName] = annotations;
|
|
167
|
-
this._logger.log(
|
|
168
|
-
`Discovered annotated resolver "${instance.constructor.name}.${methodName}" with ${annotations.length} annotation(s)`
|
|
169
|
-
);
|
|
170
|
-
}
|
|
205
|
+
for (const { instance } of instances) for (const methodName of this.metadataScanner.getAllMethodNames(Object.getPrototypeOf(instance))) {
|
|
206
|
+
const annotations = [
|
|
207
|
+
...Object.values(Reflect.getMetadata(ANNOTATION_METADATA, instance[methodName]) ?? {}),
|
|
208
|
+
...Object.values(Reflect.getMetadata(ANNOTATION_METADATA, instance.constructor, methodName) ?? {})
|
|
209
|
+
];
|
|
210
|
+
if (annotations.length <= 0) continue;
|
|
211
|
+
resolversAnnotations[methodName] = annotations;
|
|
212
|
+
this._logger.log(`Discovered annotated resolver "${instance.constructor.name}.${methodName}" with ${annotations.length} annotation(s)`);
|
|
213
|
+
}
|
|
171
214
|
return resolversAnnotations;
|
|
172
215
|
}
|
|
173
216
|
};
|
|
174
|
-
|
|
175
|
-
__decorateClass([
|
|
217
|
+
_ts_decorate2([
|
|
176
218
|
Query(() => GraphQLAnnotationMeta, {
|
|
177
|
-
name:
|
|
178
|
-
})
|
|
179
|
-
|
|
180
|
-
|
|
219
|
+
name: ANNOTATION_QUERY_NAME
|
|
220
|
+
}),
|
|
221
|
+
_ts_metadata2("design:type", Function),
|
|
222
|
+
_ts_metadata2("design:paramtypes", []),
|
|
223
|
+
_ts_metadata2("design:returntype", Promise)
|
|
224
|
+
], GraphQLAnnotationResolver.prototype, "getGraphQLAnnotations", null);
|
|
225
|
+
GraphQLAnnotationResolver = _ts_decorate2([
|
|
181
226
|
Resolver(),
|
|
182
|
-
|
|
183
|
-
|
|
227
|
+
Injectable2(),
|
|
228
|
+
_ts_param(2, Inject(ANNOTATION_RESOLVER_METADATA)),
|
|
229
|
+
_ts_metadata2("design:type", Function),
|
|
230
|
+
_ts_metadata2("design:paramtypes", [
|
|
231
|
+
typeof MetadataScanner === "undefined" ? Object : MetadataScanner,
|
|
232
|
+
typeof ResolverDiscoveryService === "undefined" ? Object : ResolverDiscoveryService,
|
|
233
|
+
typeof GraphQLAnnotationResolverMetadata === "undefined" ? Object : GraphQLAnnotationResolverMetadata
|
|
234
|
+
])
|
|
184
235
|
], GraphQLAnnotationResolver);
|
|
185
236
|
|
|
186
237
|
// src/modules/core/annotation.utils.ts
|
|
@@ -193,9 +244,9 @@ async function loadGraphQLSchema(url) {
|
|
|
193
244
|
return {
|
|
194
245
|
schema: await schemaFromExecutor(executor),
|
|
195
246
|
executor
|
|
196
|
-
// Has to be included.
|
|
197
247
|
};
|
|
198
248
|
}
|
|
249
|
+
__name(loadGraphQLSchema, "loadGraphQLSchema");
|
|
199
250
|
function split(array, predicate) {
|
|
200
251
|
const match = [];
|
|
201
252
|
const rest = [];
|
|
@@ -209,71 +260,64 @@ function split(array, predicate) {
|
|
|
209
260
|
rest
|
|
210
261
|
];
|
|
211
262
|
}
|
|
263
|
+
__name(split, "split");
|
|
212
264
|
function shallowMerge(objects) {
|
|
213
265
|
const res = {};
|
|
214
266
|
objects.forEach((o) => Object.assign(res, o));
|
|
215
267
|
return res;
|
|
216
268
|
}
|
|
269
|
+
__name(shallowMerge, "shallowMerge");
|
|
217
270
|
function isPlainObject(obj) {
|
|
218
271
|
const prototype = Object.getPrototypeOf(obj);
|
|
219
272
|
return prototype === Object.getPrototypeOf({}) || prototype === null;
|
|
220
273
|
}
|
|
274
|
+
__name(isPlainObject, "isPlainObject");
|
|
221
275
|
|
|
222
276
|
// src/modules/core/annotation.loader.ts
|
|
223
277
|
import { instanceToPlain } from "class-transformer";
|
|
278
|
+
function _ts_decorate3(decorators, target, key, desc) {
|
|
279
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
280
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
281
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
282
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
283
|
+
}
|
|
284
|
+
__name(_ts_decorate3, "_ts_decorate");
|
|
285
|
+
function _ts_metadata3(k, v) {
|
|
286
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
287
|
+
}
|
|
288
|
+
__name(_ts_metadata3, "_ts_metadata");
|
|
224
289
|
var GraphQLAnnotatedSchemaLoader = class {
|
|
290
|
+
static {
|
|
291
|
+
__name(this, "GraphQLAnnotatedSchemaLoader");
|
|
292
|
+
}
|
|
293
|
+
resolverDiscoveryService;
|
|
294
|
+
metadataScanner;
|
|
295
|
+
externalContextCreator;
|
|
296
|
+
_logger = new Logger2("GraphQLAnnotationModule");
|
|
225
297
|
constructor(resolverDiscoveryService, metadataScanner, externalContextCreator) {
|
|
226
298
|
this.resolverDiscoveryService = resolverDiscoveryService;
|
|
227
299
|
this.metadataScanner = metadataScanner;
|
|
228
300
|
this.externalContextCreator = externalContextCreator;
|
|
229
301
|
}
|
|
230
|
-
_logger = new Logger2("GraphQLAnnotationModule");
|
|
231
302
|
async load(sources) {
|
|
232
303
|
const loadedSources = await this.batchLoadSources(sources);
|
|
233
304
|
const schema = stitchSchemas({
|
|
234
305
|
subschemas: loadedSources.map((s) => s.subschema)
|
|
235
306
|
});
|
|
236
|
-
const annotations = shallowMerge(
|
|
237
|
-
loadedSources.map((s) => s.annotation)
|
|
238
|
-
);
|
|
307
|
+
const annotations = shallowMerge(loadedSources.map((s) => s.annotation));
|
|
239
308
|
const annotationResolvers = await this.discoverAnnotationResolvers();
|
|
240
|
-
return this.embedAnnotationResolversToSchema(
|
|
241
|
-
schema,
|
|
242
|
-
annotations,
|
|
243
|
-
annotationResolvers
|
|
244
|
-
);
|
|
309
|
+
return this.embedAnnotationResolversToSchema(schema, annotations, annotationResolvers);
|
|
245
310
|
}
|
|
246
311
|
async discoverAnnotationResolvers() {
|
|
247
312
|
const annotationResolvers = /* @__PURE__ */ new Map();
|
|
248
313
|
const paramFactory = new GraphQLAnnotationResolverParamsFactory();
|
|
249
314
|
const instances = this.resolverDiscoveryService.explore();
|
|
250
|
-
for (const { instance } of instances)
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
instance[methodName]
|
|
257
|
-
);
|
|
258
|
-
if (!resolverMeta) continue;
|
|
259
|
-
this._logger.log(
|
|
260
|
-
`Discovered resolver for annotation "${resolverMeta.annotation}"`
|
|
261
|
-
);
|
|
262
|
-
annotationResolvers.set(
|
|
263
|
-
resolverMeta.annotation,
|
|
264
|
-
this.externalContextCreator.create(
|
|
265
|
-
instance,
|
|
266
|
-
instance[methodName],
|
|
267
|
-
methodName,
|
|
268
|
-
PARAM_ARGS_METADATA,
|
|
269
|
-
paramFactory,
|
|
270
|
-
void 0,
|
|
271
|
-
void 0,
|
|
272
|
-
void 0,
|
|
273
|
-
"graphql"
|
|
274
|
-
)
|
|
275
|
-
);
|
|
276
|
-
}
|
|
315
|
+
for (const { instance } of instances) for (const methodName of this.metadataScanner.getAllMethodNames(Object.getPrototypeOf(instance))) {
|
|
316
|
+
const resolverMeta = Reflect.getMetadata(ANNOTATION_RESOLVER_METADATA, instance[methodName]);
|
|
317
|
+
if (!resolverMeta) continue;
|
|
318
|
+
this._logger.log(`Discovered resolver for annotation "${resolverMeta.annotation}"`);
|
|
319
|
+
annotationResolvers.set(resolverMeta.annotation, this.externalContextCreator.create(instance, instance[methodName], methodName, PARAM_ARGS_METADATA, paramFactory, void 0, void 0, void 0, "graphql"));
|
|
320
|
+
}
|
|
277
321
|
return annotationResolvers;
|
|
278
322
|
}
|
|
279
323
|
async batchLoadSources(sources) {
|
|
@@ -288,9 +332,7 @@ var GraphQLAnnotatedSchemaLoader = class {
|
|
|
288
332
|
annotation: await this.loadAnnotations(url)
|
|
289
333
|
};
|
|
290
334
|
loaded2.subschema.transforms = [
|
|
291
|
-
new FilterRootFields(
|
|
292
|
-
(op, fieldName) => !fieldName.endsWith(GraphQLAnnotationResolver.QUERY_NAME)
|
|
293
|
-
)
|
|
335
|
+
new FilterRootFields((op, fieldName) => !fieldName.endsWith(ANNOTATION_QUERY_NAME))
|
|
294
336
|
];
|
|
295
337
|
this._logger.log(`Loaded schema for ${loadId}`);
|
|
296
338
|
return loaded2;
|
|
@@ -299,14 +341,8 @@ var GraphQLAnnotatedSchemaLoader = class {
|
|
|
299
341
|
throw err;
|
|
300
342
|
}
|
|
301
343
|
});
|
|
302
|
-
const [loaded, failed] = split(
|
|
303
|
-
|
|
304
|
-
(q) => q.status === "fulfilled"
|
|
305
|
-
);
|
|
306
|
-
if (failed.length > 0)
|
|
307
|
-
throw new Error(
|
|
308
|
-
`Failed to load ${failed.length} schema(s) from sources.`
|
|
309
|
-
);
|
|
344
|
+
const [loaded, failed] = split(await Promise.allSettled(pending), (q) => q.status === "fulfilled");
|
|
345
|
+
if (failed.length > 0) throw new Error(`Failed to load ${failed.length} schema(s) from sources.`);
|
|
310
346
|
return loaded.map((l) => l.value);
|
|
311
347
|
}
|
|
312
348
|
async loadAnnotations(url) {
|
|
@@ -314,14 +350,14 @@ var GraphQLAnnotatedSchemaLoader = class {
|
|
|
314
350
|
try {
|
|
315
351
|
const res = await client.rawRequest(`
|
|
316
352
|
query IntrospectAnnotations {
|
|
317
|
-
${
|
|
353
|
+
${ANNOTATION_QUERY_NAME} {
|
|
318
354
|
name
|
|
319
355
|
resolvers
|
|
320
356
|
}
|
|
321
357
|
}
|
|
322
358
|
`);
|
|
323
359
|
this._logger.log(`Loaded annotations for ${url}`);
|
|
324
|
-
return res.data[
|
|
360
|
+
return res.data[ANNOTATION_QUERY_NAME];
|
|
325
361
|
} catch (err) {
|
|
326
362
|
this._logger.warn(`Failed to load annotations at ${url}, assuming empty`);
|
|
327
363
|
return {};
|
|
@@ -333,65 +369,58 @@ var GraphQLAnnotatedSchemaLoader = class {
|
|
|
333
369
|
const fieldAnnotations = (annotations.resolvers[name] ?? []).map((info) => ({
|
|
334
370
|
...info,
|
|
335
371
|
resolver: annotationResolvers.get(info.annotation)
|
|
336
|
-
// NOTE: should be validated in filter
|
|
337
372
|
})).filter((info) => {
|
|
338
373
|
const annotationHasResolver = info.resolver !== void 0;
|
|
339
|
-
if (!annotationHasResolver)
|
|
340
|
-
this._logger.warn(
|
|
341
|
-
`Found unhandled annotation "${info.annotation}", skipping linking process`
|
|
342
|
-
);
|
|
374
|
+
if (!annotationHasResolver) this._logger.warn(`Found unhandled annotation "${info.annotation}", skipping linking process`);
|
|
343
375
|
return annotationHasResolver;
|
|
344
376
|
});
|
|
345
377
|
if (fieldAnnotations.length <= 0) return fieldSchema;
|
|
346
|
-
for (const annotation of fieldAnnotations)
|
|
347
|
-
if (annotation.target.type === "parameter" && fieldSchema.args)
|
|
348
|
-
delete fieldSchema.args[annotation.target.paramName];
|
|
378
|
+
for (const annotation of fieldAnnotations) if (annotation.target.type === "parameter" && fieldSchema.args) delete fieldSchema.args[annotation.target.paramName];
|
|
349
379
|
const defaultResolver = fieldSchema.resolve;
|
|
350
380
|
fieldSchema.resolve = async function(parent, args, context, info) {
|
|
351
|
-
const annotationCallbacks = await Promise.all(
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
},
|
|
372
|
-
{}
|
|
373
|
-
);
|
|
374
|
-
return defaultResolver(
|
|
375
|
-
parent,
|
|
376
|
-
{ ...args, ...resolvedParams },
|
|
377
|
-
context,
|
|
378
|
-
info
|
|
379
|
-
);
|
|
381
|
+
const annotationCallbacks = await Promise.all(fieldAnnotations.map(async (annotation) => ({
|
|
382
|
+
annotation,
|
|
383
|
+
return: await annotation.resolver(...[
|
|
384
|
+
info.rootValue,
|
|
385
|
+
annotation.data,
|
|
386
|
+
context,
|
|
387
|
+
info
|
|
388
|
+
])
|
|
389
|
+
})));
|
|
390
|
+
const resolvedParams = annotationCallbacks.reduce((params, call) => {
|
|
391
|
+
if (call.annotation.target.type !== "parameter") return params;
|
|
392
|
+
const returnedClassType = typeof call.return === "object" && !isPlainObject(call.return);
|
|
393
|
+
const returnValue = returnedClassType ? instanceToPlain(call.return) : call.return;
|
|
394
|
+
params[call.annotation.target.paramName] = returnValue;
|
|
395
|
+
return params;
|
|
396
|
+
}, {});
|
|
397
|
+
return defaultResolver(parent, {
|
|
398
|
+
...args,
|
|
399
|
+
...resolvedParams
|
|
400
|
+
}, context, info);
|
|
380
401
|
};
|
|
381
402
|
return fieldSchema;
|
|
382
403
|
}
|
|
383
404
|
});
|
|
384
405
|
}
|
|
385
406
|
};
|
|
386
|
-
GraphQLAnnotatedSchemaLoader =
|
|
387
|
-
|
|
407
|
+
GraphQLAnnotatedSchemaLoader = _ts_decorate3([
|
|
408
|
+
Injectable3(),
|
|
409
|
+
_ts_metadata3("design:type", Function),
|
|
410
|
+
_ts_metadata3("design:paramtypes", [
|
|
411
|
+
typeof ResolverDiscoveryService === "undefined" ? Object : ResolverDiscoveryService,
|
|
412
|
+
typeof MetadataScanner2 === "undefined" ? Object : MetadataScanner2,
|
|
413
|
+
typeof ExternalContextCreator === "undefined" ? Object : ExternalContextCreator
|
|
414
|
+
])
|
|
388
415
|
], GraphQLAnnotatedSchemaLoader);
|
|
389
|
-
var GraphQLAnnotationResolverParamsFactory = class {
|
|
416
|
+
var GraphQLAnnotationResolverParamsFactory = class GraphQLAnnotationResolverParamsFactory2 {
|
|
417
|
+
static {
|
|
418
|
+
__name(this, "GraphQLAnnotationResolverParamsFactory");
|
|
419
|
+
}
|
|
390
420
|
// This param factory extends graphql's own just with different inputs from annotations instead.
|
|
391
421
|
// https://github.com/nestjs/graphql/blob/master/packages/graphql/lib/factories/params.factory.ts#L9
|
|
392
422
|
exchangeKeyForValue(type, possibleKey, argsContext) {
|
|
393
423
|
if (!argsContext) return null;
|
|
394
|
-
console.log(type, possibleKey, argsContext);
|
|
395
424
|
const args = {
|
|
396
425
|
parentValue: argsContext[0],
|
|
397
426
|
data: argsContext[1],
|
|
@@ -416,61 +445,54 @@ var GraphQLAnnotationResolverParamsFactory = class {
|
|
|
416
445
|
// src/modules/core/annotation.module.ts
|
|
417
446
|
import { Module } from "@nestjs/common";
|
|
418
447
|
import { DiscoveryModule } from "@nestjs/core";
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
return wrappedProviders.flatMap((wrapped) => {
|
|
430
|
-
const { instance } = wrapped;
|
|
431
|
-
if (!instance) return [];
|
|
432
|
-
const isResolver = Reflect.hasMetadata(
|
|
433
|
-
RESOLVER_TYPE_METADATA,
|
|
434
|
-
instance.constructor
|
|
435
|
-
);
|
|
436
|
-
if (!isResolver) return [];
|
|
437
|
-
return wrapped;
|
|
438
|
-
});
|
|
448
|
+
function _ts_decorate4(decorators, target, key, desc) {
|
|
449
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
450
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
451
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
452
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
453
|
+
}
|
|
454
|
+
__name(_ts_decorate4, "_ts_decorate");
|
|
455
|
+
var GraphQLAnnotationModule = class _GraphQLAnnotationModule {
|
|
456
|
+
static {
|
|
457
|
+
__name(this, "GraphQLAnnotationModule");
|
|
439
458
|
}
|
|
440
|
-
};
|
|
441
|
-
ResolverDiscoveryService = __decorateClass([
|
|
442
|
-
Injectable3()
|
|
443
|
-
], ResolverDiscoveryService);
|
|
444
|
-
|
|
445
|
-
// src/modules/core/annotation.module.ts
|
|
446
|
-
var GraphQLAnnotationModule = class {
|
|
447
459
|
static forRoot(opts) {
|
|
448
|
-
if (opts?.serveAnnotations)
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
460
|
+
if (opts?.serveAnnotations) return {
|
|
461
|
+
global: true,
|
|
462
|
+
module: _GraphQLAnnotationModule,
|
|
463
|
+
imports: [
|
|
464
|
+
DiscoveryModule
|
|
465
|
+
],
|
|
466
|
+
providers: [
|
|
467
|
+
ResolverDiscoveryService,
|
|
468
|
+
GraphQLAnnotatedSchemaLoader,
|
|
469
|
+
{
|
|
470
|
+
provide: ANNOTATION_RESOLVER_METADATA,
|
|
471
|
+
useValue: opts.serveConfig
|
|
472
|
+
},
|
|
473
|
+
GraphQLAnnotationResolver
|
|
474
|
+
],
|
|
475
|
+
exports: [
|
|
476
|
+
GraphQLAnnotatedSchemaLoader
|
|
477
|
+
]
|
|
478
|
+
};
|
|
464
479
|
return {
|
|
465
480
|
global: true,
|
|
466
|
-
module:
|
|
467
|
-
imports: [
|
|
468
|
-
|
|
469
|
-
|
|
481
|
+
module: _GraphQLAnnotationModule,
|
|
482
|
+
imports: [
|
|
483
|
+
DiscoveryModule
|
|
484
|
+
],
|
|
485
|
+
providers: [
|
|
486
|
+
ResolverDiscoveryService,
|
|
487
|
+
GraphQLAnnotatedSchemaLoader
|
|
488
|
+
],
|
|
489
|
+
exports: [
|
|
490
|
+
GraphQLAnnotatedSchemaLoader
|
|
491
|
+
]
|
|
470
492
|
};
|
|
471
493
|
}
|
|
472
494
|
};
|
|
473
|
-
GraphQLAnnotationModule =
|
|
495
|
+
GraphQLAnnotationModule = _ts_decorate4([
|
|
474
496
|
Module({})
|
|
475
497
|
], GraphQLAnnotationModule);
|
|
476
498
|
export {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tinkerstack/graphql-annotation",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.2",
|
|
4
4
|
"description": "Better type-safe-able graphql directives",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -17,6 +17,9 @@
|
|
|
17
17
|
"class-transformer": "^0.5.1",
|
|
18
18
|
"graphql-request": "^7.2.0"
|
|
19
19
|
},
|
|
20
|
+
"publishConfig": {
|
|
21
|
+
"access": "public"
|
|
22
|
+
},
|
|
20
23
|
"peerDependencies": {
|
|
21
24
|
"@nestjs/common": "^10 || ^11",
|
|
22
25
|
"@nestjs/core": "^10 || ^11",
|
|
@@ -36,6 +39,7 @@
|
|
|
36
39
|
"@nestjs/graphql": "^13.2.3",
|
|
37
40
|
"@nestjs/mapped-types": "^2.1.0",
|
|
38
41
|
"@nestjs/platform-fastify": "^11.1.11",
|
|
42
|
+
"@swc/core": "^1.15.11",
|
|
39
43
|
"class-validator": "^0.14.2",
|
|
40
44
|
"concurrently": "^9.2.1",
|
|
41
45
|
"eslint": "^9.37.0",
|