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