Skip to content

Commit

Permalink
fix(): fix abstract resolvers, field resolver args
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Mar 18, 2020
1 parent 90de332 commit fd4e471
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 4 deletions.
15 changes: 13 additions & 2 deletions lib/decorators/resolver.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ export function Resolver(): MethodDecorator & ClassDecorator;
* Object resolver decorator.
*/
export function Resolver(name: string): MethodDecorator & ClassDecorator;
/**
* Object resolver decorator.
*/
export function Resolver(
options: ResolverOptions,
): MethodDecorator & ClassDecorator;
/**
* Object resolver decorator.
*/
Expand All @@ -62,14 +68,19 @@ export function Resolver(
* Object resolver decorator.
*/
export function Resolver(
nameOrType?: string | ResolverTypeFn | Type<any>,
nameOrTypeOrOptions?: string | ResolverTypeFn | Type<any> | ResolverOptions,
options?: ResolverOptions,
): MethodDecorator & ClassDecorator {
return (
target: Object | Function,
key?: string | symbol,
descriptor?: any,
) => {
const [nameOrType, resolverOptions] =
typeof nameOrTypeOrOptions === 'object' && nameOrTypeOrOptions !== null
? [undefined, nameOrTypeOrOptions]
: [nameOrTypeOrOptions as string | ResolverTypeFn | Type<any>, options];

let name = nameOrType && getClassName(nameOrType);

if (isFunction(nameOrType)) {
Expand All @@ -85,7 +96,7 @@ export function Resolver(
TypeMetadataStorage.addResolverMetadata({
target: target as Function,
typeFn: typeFn,
isAbstract: (options && options.isAbstract) || false,
isAbstract: (resolverOptions && resolverOptions.isAbstract) || false,
});
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ObjectTypeMetadata } from '../metadata/object-type.metadata';
import { OrphanedReferenceRegistry } from '../services/orphaned-reference.registry';
import { TypeFieldsAccessor } from '../services/type-fields.accessor';
import { TypeDefinitionsStorage } from '../storages/type-definitions.storage';
import { ArgsFactory } from './args.factory';
import { AstDefinitionNodeFactory } from './ast-definition-node.factory';
import { OutputTypeFactory } from './output-type.factory';

Expand All @@ -28,6 +29,7 @@ export class ObjectTypeDefinitionFactory {
private readonly typeFieldsAccessor: TypeFieldsAccessor,
private readonly astDefinitionNodeFactory: AstDefinitionNodeFactory,
private readonly orphanedReferenceRegistry: OrphanedReferenceRegistry,
private readonly argsFactory: ArgsFactory,
) {}

public create(
Expand Down Expand Up @@ -105,6 +107,7 @@ export class ObjectTypeDefinitionFactory {
);
fields[field.schemaName] = {
type,
args: this.argsFactory.create(field.methodArgs, options),
resolve: undefined,
description: field.description,
deprecationReason: field.deprecationReason,
Expand Down
25 changes: 24 additions & 1 deletion lib/schema-builder/storages/lazy-metadata.storage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Type } from '@nestjs/common';
import { flatten, Type } from '@nestjs/common';
import { isUndefined } from '@nestjs/common/utils/shared.utils';

interface LazyMetadataHost {
func: Function;
Expand All @@ -19,6 +20,7 @@ export class LazyMetadataStorageHost {
}

load(types: Function[] = []) {
types = this.concatPrototypes(types);
this.storage.forEach(({ func, target }) => {
if (target && types.includes(target)) {
func();
Expand All @@ -27,6 +29,27 @@ export class LazyMetadataStorageHost {
}
});
}

private concatPrototypes(types: Function[]): Function[] {
const typesWithPrototypes = types
.filter(type => type && type.prototype)
.map(type => {
const parentTypes = [];

let parent: Function = type;
while (!isUndefined(parent.prototype)) {
parent = Object.getPrototypeOf(parent);
if (parent === Function.prototype) {
break;
}
parentTypes.push(parent);
}
parentTypes.push(type);
return parentTypes;
});

return flatten(typesWithPrototypes);
}
}

export const LazyMetadataStorage = new LazyMetadataStorageHost();
2 changes: 1 addition & 1 deletion tests/code-first/other/abstract.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Recipe } from './../recipes/models/recipe';
@Resolver(() => Recipe, { isAbstract: true })
export class AbstractResolver {
@Query(returns => [Recipe])
absractRecipes(@Args() recipesArgs: RecipesArgs): Recipe[] {
abstractRecipes(@Args() recipesArgs: RecipesArgs): Recipe[] {
return [];
}
}
10 changes: 10 additions & 0 deletions tests/code-first/recipes/dto/filter-recipes-count.args.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ArgsType, Field } from '../../../../lib';

@ArgsType()
export class FilterRecipesCountArgs {
@Field({ nullable: true })
type?: string;

@Field({ nullable: true })
status?: string;
}
6 changes: 6 additions & 0 deletions tests/code-first/recipes/recipes.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
Subscription,
} from '../../../lib';
import { AuthGuard } from '../common/guards/auth.guard';
import { FilterRecipesCountArgs } from './dto/filter-recipes-count.args';
import { NewRecipeInput } from './dto/new-recipe.input';
import { RecipesArgs } from './dto/recipes.args';
import { Ingredient } from './models/ingredient';
Expand Down Expand Up @@ -70,6 +71,11 @@ export class RecipesResolver {
return [new Ingredient({ name: 'cherry' })];
}

@ResolveField(type => Number)
count(@Args() filters: FilterRecipesCountArgs) {
return 10;
}

@ResolveField()
rating(): number {
return 10;
Expand Down
25 changes: 25 additions & 0 deletions tests/e2e/code-first-schema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,31 @@ describe('Code-first - schema factory', () => {
},
},
},
{
args: [
{
defaultValue: null,
description: null,
name: 'type',
type: { kind: TypeKind.SCALAR, name: 'String', ofType: null },
},
{
defaultValue: null,
description: null,
name: 'status',
type: { kind: TypeKind.SCALAR, name: 'String', ofType: null },
},
],
deprecationReason: null,
description: null,
isDeprecated: false,
name: 'count',
type: {
kind: TypeKind.NON_NULL,
name: null,
ofType: { kind: TypeKind.SCALAR, name: 'Float', ofType: null },
},
},
{
args: [],
deprecationReason: null,
Expand Down
1 change: 1 addition & 0 deletions tests/utils/printed-schema.snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type Recipe implements IRecipe {
lastRate: Float
tags: [String!]!
ingredients: [Ingredient!]!
count(type: String, status: String): Float!
rating: Float!
}
Expand Down

0 comments on commit fd4e471

Please sign in to comment.