Skip to content

Commit 82bc783

Browse files
committed
Adjust style and formatting for consistence
1 parent ba5c43b commit 82bc783

2 files changed

Lines changed: 50 additions & 48 deletions

File tree

src/metadata/metadata-storage.ts

Lines changed: 35 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -206,23 +206,23 @@ export class MetadataStorage {
206206

207207
if (this.params?.length) {
208208
this.params.forEach(param => {
209-
const key = `${param.target}-${param.methodName}`;
209+
const key = `${param.target}#${param.methodName}`;
210210
if (!this.paramsCache.has(key)) {
211211
this.paramsCache.set(key, []);
212212
}
213-
this.paramsCache.get(key)?.push(param);
213+
this.paramsCache.get(key)!.push(param);
214214
});
215215
}
216216

217217
if (this.middlewares?.length) {
218218
this.middlewares.forEach(middleware => {
219-
const key = `${middleware.target}-${middleware.fieldName}`;
219+
const key = `${middleware.target}#${middleware.fieldName}`;
220220
if (!this.middlewaresByTargetAndFieldCache.has(key)) {
221221
this.middlewaresByTargetAndFieldCache.set(key, new Set());
222222
}
223223

224-
if (!this.middlewaresByTargetAndFieldCache.get(key)?.has(middleware)) {
225-
this.middlewaresByTargetAndFieldCache.get(key)?.add(middleware);
224+
if (!this.middlewaresByTargetAndFieldCache.get(key)!.has(middleware)) {
225+
this.middlewaresByTargetAndFieldCache.get(key)!.add(middleware);
226226
}
227227
});
228228
}
@@ -234,19 +234,19 @@ export class MetadataStorage {
234234
this.resolverMiddlewaresByTargetCache.set(key, new Set());
235235
}
236236

237-
if (!this.resolverMiddlewaresByTargetCache.get(key)?.has(middleware)) {
238-
this.resolverMiddlewaresByTargetCache.get(key)?.add(middleware);
237+
if (!this.resolverMiddlewaresByTargetCache.get(key)!.has(middleware)) {
238+
this.resolverMiddlewaresByTargetCache.get(key)!.add(middleware);
239239
}
240240
});
241241
}
242242

243243
if (this.fieldDirectives?.length) {
244244
this.fieldDirectives.forEach(directive => {
245-
const key = `${directive.target}-${directive.fieldName}`;
245+
const key = `${directive.target}#${directive.fieldName}`;
246246
if (!this.fieldDirectivesByTargetAndFieldCache.has(key)) {
247247
this.fieldDirectivesByTargetAndFieldCache.set(key, []);
248248
}
249-
this.fieldDirectivesByTargetAndFieldCache.get(key)?.push(directive);
249+
this.fieldDirectivesByTargetAndFieldCache.get(key)!.push(directive);
250250
});
251251
}
252252

@@ -256,13 +256,13 @@ export class MetadataStorage {
256256
if (!this.classDirectivesByTargetCache.has(key)) {
257257
this.classDirectivesByTargetCache.set(key, []);
258258
}
259-
this.classDirectivesByTargetCache.get(key)?.push(directive);
259+
this.classDirectivesByTargetCache.get(key)!.push(directive);
260260
});
261261
}
262262

263263
if (this.authorizedFields?.length) {
264264
this.authorizedFields.forEach(field => {
265-
const key = `${field.target}-${field.fieldName}`;
265+
const key = `${field.target}#${field.fieldName}`;
266266
if (!this.authorizedFieldsByTargetAndFieldCache.has(key)) {
267267
this.authorizedFieldsByTargetAndFieldCache.set(key, field);
268268
}
@@ -283,7 +283,7 @@ export class MetadataStorage {
283283
if (!this.fieldsCache.has(field.target)) {
284284
this.fieldsCache.set(field.target, []);
285285
}
286-
this.fieldsCache.get(field.target)?.push(field);
286+
this.fieldsCache.get(field.target)!.push(field);
287287
});
288288
}
289289

@@ -368,30 +368,26 @@ export class MetadataStorage {
368368
const fields = this.fieldsCache.get(def.target) || [];
369369
fields.forEach(field => {
370370
field.roles = this.findFieldRoles(field.target, field.name);
371-
372-
const paramKey = `${field.target}-${field.name}`;
373-
field.params = this.paramsCache.get(paramKey) || [];
374-
375-
const resolverMiddlewares = this.resolverMiddlewaresByTargetCache.get(field.target) || [];
376-
const middlewaresKey = `${field.target}-${field.name}`;
377-
const fieldMiddlewares = this.middlewaresByTargetAndFieldCache.get(middlewaresKey) || [];
378-
371+
field.params = this.paramsCache.get(`${field.target}#${field.name}`) || [];
379372
field.middlewares = [
380-
...mapMiddlewareMetadataToArray(Array.from(resolverMiddlewares)),
381-
...mapMiddlewareMetadataToArray(Array.from(fieldMiddlewares)),
373+
...mapMiddlewareMetadataToArray([
374+
...(this.resolverMiddlewaresByTargetCache.get(field.target) || []),
375+
]),
376+
...mapMiddlewareMetadataToArray([
377+
...(this.middlewaresByTargetAndFieldCache.get(`${field.target}#${field.name}`) || []),
378+
]),
382379
];
383-
384-
const directives =
385-
this.fieldDirectivesByTargetAndFieldCache.get(`${field.target}-${field.name}`) || [];
386-
field.directives = directives.map(it => it.directive);
387-
380+
field.directives = (
381+
this.fieldDirectivesByTargetAndFieldCache.get(`${field.target}#${field.name}`) || []
382+
).map(it => it.directive);
388383
field.extensions = this.findExtensions(field.target, field.name);
389384
});
390385
def.fields = fields;
391386
}
392387
if (!def.directives) {
393-
const directives = this.classDirectivesByTargetCache.get(def.target) || [];
394-
def.directives = directives.map(directive => directive.directive);
388+
def.directives = (this.classDirectivesByTargetCache.get(def.target) || []).map(
389+
it => it.directive,
390+
);
395391
}
396392
if (!def.extensions) {
397393
def.extensions = this.findExtensions(def.target);
@@ -402,19 +398,19 @@ export class MetadataStorage {
402398
private buildResolversMetadata(definitions: BaseResolverMetadata[]) {
403399
definitions.forEach(def => {
404400
def.resolverClassMetadata = this.resolverClassesCache.get(def.target);
405-
def.params = this.paramsCache.get(`${def.target}-${def.methodName}`) || [];
401+
def.params = this.paramsCache.get(`${def.target}#${def.methodName}`) || [];
406402
def.roles = this.findFieldRoles(def.target, def.methodName);
407-
408-
const resolverMiddlewares = this.resolverMiddlewaresByTargetCache.get(def.target) || [];
409-
const fieldMiddlewares =
410-
this.middlewaresByTargetAndFieldCache.get(`${def.target}-${def.methodName}`) || [];
411403
def.middlewares = [
412-
...mapMiddlewareMetadataToArray(Array.from(resolverMiddlewares)),
413-
...mapMiddlewareMetadataToArray(Array.from(fieldMiddlewares)),
404+
...mapMiddlewareMetadataToArray([
405+
...(this.resolverMiddlewaresByTargetCache.get(def.target) || []),
406+
]),
407+
...mapMiddlewareMetadataToArray([
408+
...(this.middlewaresByTargetAndFieldCache.get(`${def.target}#${def.methodName}`) || []),
409+
]),
414410
];
415411

416412
def.directives = (
417-
this.fieldDirectivesByTargetAndFieldCache.get(`${def.target}-${def.methodName}`) || []
413+
this.fieldDirectivesByTargetAndFieldCache.get(`${def.target}#${def.methodName}`) || []
418414
).map(it => it.directive);
419415
def.extensions = this.findExtensions(def.target, def.methodName);
420416
});
@@ -428,7 +424,7 @@ export class MetadataStorage {
428424
definitions.forEach(def => {
429425
def.roles = this.findFieldRoles(def.target, def.methodName);
430426
def.directives = (
431-
this.fieldDirectivesByTargetAndFieldCache.get(`${def.target}-${def.methodName}`) || []
427+
this.fieldDirectivesByTargetAndFieldCache.get(`${def.target}#${def.methodName}`) || []
432428
).map(it => it.directive);
433429
def.extensions = this.findExtensions(def.target, def.methodName);
434430
def.getObjectType =
@@ -437,9 +433,6 @@ export class MetadataStorage {
437433
: () => def.target as ClassType;
438434
if (def.kind === "external") {
439435
const typeClass = this.resolverClassesCache.get(def.target)!.getObjectType!();
440-
if (!typeClass) {
441-
throw new Error(`Unable to find type class for external resolver ${def.target.name}`);
442-
}
443436
const typeMetadata =
444437
this.objectTypesCache.get(typeClass) || this.interfaceTypesCache.get(typeClass);
445438
if (!typeMetadata) {
@@ -520,7 +513,7 @@ export class MetadataStorage {
520513

521514
private findFieldRoles(target: Function, fieldName: string): any[] | undefined {
522515
const authorizedField =
523-
this.authorizedFieldsByTargetAndFieldCache.get(`${target}-${fieldName}`) ||
516+
this.authorizedFieldsByTargetAndFieldCache.get(`${target}#${fieldName}`) ||
524517
this.authorizedResolverByTargetCache.get(target);
525518
if (!authorizedField) {
526519
return undefined;

src/schema/schema-generator.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ export abstract class SchemaGenerator {
132132
static generateFromMetadata(options: SchemaGeneratorOptions): GraphQLSchema {
133133
this.metadataStorage = Object.assign(new MetadataStorage(), getMetadataStorage());
134134
this.metadataStorage.build(options);
135+
135136
this.checkForErrors(options);
136137
BuildContext.create(options);
137138

@@ -159,6 +160,7 @@ export abstract class SchemaGenerator {
159160
throw new GeneratingSchemaError(errors);
160161
}
161162
}
163+
162164
return finalSchema;
163165
}
164166

@@ -213,7 +215,7 @@ export abstract class SchemaGenerator {
213215
);
214216
return unionObjectTypesInfo.map(it => it.type);
215217
};
216-
const unionType = {
218+
const unionTypeInfo: UnionTypeInfo = {
217219
unionSymbol: unionMetadata.symbol,
218220
type: new GraphQLUnionType({
219221
name: unionMetadata.name,
@@ -241,9 +243,9 @@ export abstract class SchemaGenerator {
241243
}),
242244
};
243245

244-
this.unionTypesInfoMap.set(unionMetadata.symbol, unionType);
246+
this.unionTypesInfoMap.set(unionMetadata.symbol, unionTypeInfo);
245247

246-
return unionType;
248+
return unionTypeInfo;
247249
});
248250

249251
this.enumTypesInfo = this.metadataStorage.enums.map<EnumTypeInfo>(enumMetadata => {
@@ -277,7 +279,7 @@ export abstract class SchemaGenerator {
277279
return superClassTypeInfo ? superClassTypeInfo.type : undefined;
278280
};
279281
const interfaceClasses = objectType.interfaceClasses || [];
280-
const objectTypeInfo = {
282+
const objectTypeInfo: ObjectTypeInfo = {
281283
metadata: objectType,
282284
target: objectType.target,
283285
type: new GraphQLObjectType({
@@ -379,9 +381,12 @@ export abstract class SchemaGenerator {
379381
},
380382
}),
381383
};
384+
382385
this.objectTypesInfoMap.set(objectType.target, objectTypeInfo);
386+
383387
return objectTypeInfo;
384388
});
389+
385390
this.interfaceTypesInfo = this.metadataStorage.interfaceTypes.map<InterfaceTypeInfo>(
386391
interfaceType => {
387392
const interfaceSuperClass = Object.getPrototypeOf(interfaceType.target);
@@ -405,7 +410,7 @@ export abstract class SchemaGenerator {
405410
implementingObjectTypesTargets.includes(objectTypesInfo.target),
406411
);
407412

408-
const interfaceTypeInfo = {
413+
const interfaceTypeInfo: InterfaceTypeInfo = {
409414
metadata: interfaceType,
410415
target: interfaceType.target,
411416
type: new GraphQLInterfaceType({
@@ -500,7 +505,9 @@ export abstract class SchemaGenerator {
500505
},
501506
}),
502507
};
508+
503509
this.interfaceTypesInfoMap.set(interfaceType.target, interfaceTypeInfo);
510+
504511
return interfaceTypeInfo;
505512
},
506513
);
@@ -511,7 +518,7 @@ export abstract class SchemaGenerator {
511518
return superClassTypeInfo ? superClassTypeInfo.type : undefined;
512519
};
513520
const inputInstance = new (inputType.target as any)();
514-
const inputTypeInfo = {
521+
const inputTypeInfo: InputObjectTypeInfo = {
515522
target: inputType.target,
516523
type: new GraphQLInputObjectType({
517524
name: inputType.name,
@@ -557,7 +564,9 @@ export abstract class SchemaGenerator {
557564
astNode: getInputObjectTypeDefinitionNode(inputType.name, inputType.directives),
558565
}),
559566
};
567+
560568
this.inputTypesInfoMap.set(inputType.target, inputTypeInfo);
569+
561570
return inputTypeInfo;
562571
});
563572
}

0 commit comments

Comments
 (0)