Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(code-first): Use base model field options with ResolveField() #2337

Merged
merged 1 commit into from
Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions packages/apollo/tests/code-first/recipes/ingredients.resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { ID, ResolveField, Resolver, Parent } from '@nestjs/graphql';
import { Ingredient } from './models/ingredient';

@Resolver((of) => Ingredient)
export class IngredientsResolver {
@ResolveField(() => ID)
id(@Parent() { id }: Ingredient): string {
return id;
}

@ResolveField()
name(@Parent() { name }: Ingredient): string {
return name;
}

@ResolveField()
baseName(@Parent() { name }: Ingredient): string {
return name;
}
}
26 changes: 23 additions & 3 deletions packages/apollo/tests/code-first/recipes/models/ingredient.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
import { Field, ID, ObjectType } from '@nestjs/graphql';
@ObjectType()
export class Ingredient {
@Field((type) => ID)

class NodeIngredient {
// This does not apply since the class is not decorated with the @ObjectType decorator
@Field((type) => ID, {
defaultValue: 'Not applied',
description: 'Not applied',
deprecationReason: 'Not applied',
})
id: string;
}

@ObjectType({ isAbstract: true })
class BaseIngredient extends NodeIngredient {
@Field({
defaultValue: 'default',
deprecationReason: 'is deprecated',
description: 'ingredient base name',
nullable: true,
})
baseName: string;
}

@ObjectType()
export class Ingredient extends BaseIngredient {
@Field({
defaultValue: 'default',
deprecationReason: 'is deprecated',
Expand All @@ -13,6 +32,7 @@ export class Ingredient {
name: string;

constructor(ingredient: Partial<Ingredient>) {
super();
Object.assign(this, ingredient);
}
}
2 changes: 2 additions & 0 deletions packages/apollo/tests/code-first/recipes/recipes.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import { Module } from '@nestjs/common';
import { APP_FILTER } from '@nestjs/core';
import { UnauthorizedFilter } from '../common/filters/unauthorized.filter';
import { DateScalar } from '../common/scalars/date.scalar';
import { IngredientsResolver } from './ingredients.resolver';
import { IRecipesResolver } from './irecipes.resolver';
import { RecipesResolver } from './recipes.resolver';
import { RecipesService } from './recipes.service';

@Module({
providers: [
IngredientsResolver,
RecipesResolver,
IRecipesResolver,
RecipesService,
Expand Down
14 changes: 14 additions & 0 deletions packages/apollo/tests/e2e/code-first-schema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { DirectionsResolver } from '../code-first/directions/directions.resolver
import { SampleOrphanedEnum } from '../code-first/enums/sample-orphaned.enum';
import { AbstractResolver } from '../code-first/other/abstract.resolver';
import { SampleOrphanedType } from '../code-first/other/sample-orphaned.type';
import { IngredientsResolver } from '../code-first/recipes/ingredients.resolver';
import { IRecipesResolver } from '../code-first/recipes/irecipes.resolver';
import { Recipe } from '../code-first/recipes/models/recipe';
import { RecipesResolver } from '../code-first/recipes/recipes.resolver';
Expand Down Expand Up @@ -48,6 +49,7 @@ describe('Code-first - schema factory', () => {
beforeAll(async () => {
schema = await schemaFactory.create(
[
IngredientsResolver,
RecipesResolver,
DirectionsResolver,
AbstractResolver,
Expand Down Expand Up @@ -212,6 +214,18 @@ describe('Code-first - schema factory', () => {
ofType: null,
},
},
{
args: [],
deprecationReason: 'is deprecated',
description: 'ingredient base name',
isDeprecated: true,
name: 'baseName',
type: {
kind: TypeKind.SCALAR,
name: 'String',
ofType: null,
},
},
]),
}),
);
Expand Down
18 changes: 11 additions & 7 deletions packages/apollo/tests/utils/printed-schema.snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,19 @@ type SampleOrphanedType {
averageRating: Float!
}

type Category {
name: String!
description: String!
tags: [String!]!
}

type Ingredient {
id: ID!
"""ingredient base name"""
baseName: String @deprecated(reason: "is deprecated")

"""ingredient name"""
name: String @deprecated(reason: "is deprecated")
id: ID!
}

type Category {
name: String!
description: String!
tags: [String!]!
}

"""orphaned enum"""
Expand Down Expand Up @@ -141,6 +143,8 @@ interface IRecipe {
}

type Ingredient {
"""ingredient base name"""
baseName: String @deprecated(reason: "is deprecated")
id: ID!

"""ingredient name"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,24 +338,8 @@ export class TypeMetadataStorageHost {
}

private compileExternalFieldResolverMetadata(item: FieldResolverMetadata) {
const objectTypeRef = this.metadataByTargetCollection
.get(item.target)
.resolver.typeFn();

const objectOrInterfaceTypeMetadata =
this.metadataByTargetCollection.get(objectTypeRef).objectType ||
this.metadataByTargetCollection.get(objectTypeRef).interface;

if (!objectOrInterfaceTypeMetadata) {
throw new CannotDetermineHostTypeError(
item.schemaName,
objectTypeRef?.name,
);
}
const objectOrInterfaceTypeField =
objectOrInterfaceTypeMetadata.properties.find(
(fieldDef) => fieldDef.name === item.methodName,
);
const [target, objectOrInterfaceTypeMetadata, objectOrInterfaceTypeField] =
this.findModelFieldMetadata(item);
if (!objectOrInterfaceTypeField) {
if (!item.typeFn || !item.typeOptions) {
throw new UndefinedTypeError(item.target.name, item.methodName);
Expand All @@ -366,7 +350,7 @@ export class TypeMetadataStorageHost {
deprecationReason: item.deprecationReason,
description: item.description,
typeFn: item.typeFn,
target: objectTypeRef,
target,
options: item.typeOptions,
methodArgs: item.methodArgs,
directives: item.directives,
Expand Down Expand Up @@ -394,6 +378,49 @@ export class TypeMetadataStorageHost {
}
}

private findModelFieldMetadata(
item: FieldResolverMetadata,
): [Function, ClassMetadata, PropertyMetadata | undefined] {
let objectTypeRef = this.metadataByTargetCollection
.get(item.target)
.resolver.typeFn();
const getTypeMetadata = (target: any) => {
const metadata = this.metadataByTargetCollection.get(target);
return metadata.objectType || metadata.interface;
};
let objectOrInterfaceTypeMetadata = getTypeMetadata(objectTypeRef);
if (!objectOrInterfaceTypeMetadata) {
throw new CannotDetermineHostTypeError(
item.schemaName,
objectTypeRef?.name,
);
}
let objectOrInterfaceTypeField =
objectOrInterfaceTypeMetadata.properties.find(
(fieldDef) => fieldDef.name === item.methodName,
);
for (
let _objectTypeRef = objectTypeRef;
!objectOrInterfaceTypeField && _objectTypeRef?.prototype;
_objectTypeRef = Object.getPrototypeOf(_objectTypeRef)
) {
const possibleTypeMetadata = getTypeMetadata(_objectTypeRef);
mattleff marked this conversation as resolved.
Show resolved Hide resolved
objectOrInterfaceTypeField = possibleTypeMetadata?.properties.find(
(fieldDef) => fieldDef.name === item.methodName,
);
if (objectOrInterfaceTypeField) {
objectTypeRef = _objectTypeRef;
objectOrInterfaceTypeMetadata = possibleTypeMetadata;
break;
}
}
return [
objectTypeRef,
objectOrInterfaceTypeMetadata,
objectOrInterfaceTypeField,
];
}

private compileExtendedResolversMetadata() {
this.metadataByTargetCollection.all.resolver.forEach((item) => {
let parentClass = Object.getPrototypeOf(item.target);
Expand Down