Skip to content

Commit

Permalink
fix(inheritance): stop throwing error while extending non args class
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalLytek committed Feb 7, 2019
1 parent ea9c887 commit 315d7d5
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 32 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
## Fixes
- fix calling return type getter function `@Field(type => Foo)` before finishing module evaluation (allow for extending circular classes using `require`)
- fix nullifying other custom method decorators - call the method on target instance, not the stored reference to original function (#247)
- fix throwing error when extending non args class in the `@ArgsType()` class

## v0.16.0
### Features
Expand Down
4 changes: 3 additions & 1 deletion src/schema/schema-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,9 @@ export abstract class SchemaGenerator {
const superArgumentType = getMetadataStorage().argumentTypes.find(
it => it.target === superClass,
)!;
this.mapArgFields(superArgumentType, args);
if (superArgumentType) {
this.mapArgFields(superArgumentType, args);
}
superClass = Object.getPrototypeOf(superClass);
}
this.mapArgFields(argumentType, args);
Expand Down
83 changes: 52 additions & 31 deletions tests/functional/interfaces-and-inheritance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
Mutation,
buildSchema,
Int,
Resolver,
} from "../../src";

describe("Interfaces and inheritance", () => {
Expand Down Expand Up @@ -350,39 +351,37 @@ describe("Interfaces and inheritance", () => {
expect(baseInputFieldType.name).toEqual("String");
expect(extendingInputFieldType.name).toEqual("Boolean");
});
});

describe("Errors", () => {
beforeEach(() => {
it("shouldn't throw error when extending wrong class type", async () => {
getMetadataStorage().clear();
});

it("should throw error when extending wrong class type", async () => {
expect.assertions(1);
try {
@InputType()
class SampleInput {
@Field()
inputField: string;
}
@ArgsType()
class SampleArgs extends SampleInput {
@Field()
argField: string;
}
class SampleResolver {
@Query()
sampleQuery(@Args() args: SampleArgs): boolean {
return true;
}
@InputType()
class SampleInput {
@Field()
inputField: string;
}
@ArgsType()
class SampleArgs extends SampleInput {
@Field()
argField: string;
}
@Resolver()
class SampleResolver {
@Query()
sampleQuery(@Args() args: SampleArgs): boolean {
return true;
}
await buildSchema({
resolvers: [SampleResolver],
});
} catch (err) {
// TODO: test for more meaningfull error message
expect(err).toBeDefined();
}
const schema = await buildSchema({
resolvers: [SampleResolver],
});
expect(schema).toBeDefined();
});
});

describe("Errors", () => {
beforeEach(() => {
getMetadataStorage().clear();
});

it("should throw error when field type doesn't match with interface", async () => {
Expand Down Expand Up @@ -424,6 +423,8 @@ describe("Interfaces and inheritance", () => {
let schema: GraphQLSchema;
let queryArgs: any;
let mutationInput: any;
let inputFieldValue: any;
let argsFieldValue: any;

beforeEach(() => {
queryArgs = undefined;
Expand Down Expand Up @@ -483,13 +484,23 @@ describe("Interfaces and inheritance", () => {
return "sampleStaticMethod";
}
}

@ObjectType()
class SampleExtendingNormalClassObject extends SampleBaseClass {
@Field()
sampleField: string;
}
@InputType()
class SampleExtendingNormalClassInput extends SampleBaseClass {
@Field()
sampleField: string;
}
@ArgsType()
class SampleExtendingNormalClassArgs extends SampleBaseClass {
@Field()
sampleField: string;
}

@Resolver()
class InterfacesResolver {
@Query()
getInterfacePlainObject(): BaseInterface {
Expand All @@ -516,7 +527,12 @@ describe("Interfaces and inheritance", () => {
}

@Query()
baseClassQuery(): string {
baseClassQuery(
@Arg("input") input: SampleExtendingNormalClassInput,
@Args() args: SampleExtendingNormalClassArgs,
): string {
inputFieldValue = input.sampleField;
argsFieldValue = args.sampleField;
return SampleExtendingNormalClassObject.sampleStaticMethod();
}
}
Expand Down Expand Up @@ -621,12 +637,17 @@ describe("Interfaces and inheritance", () => {

it("should correctly extends non-TypeGraphQL class", async () => {
const query = `query {
baseClassQuery
baseClassQuery(
input: { sampleField: "sampleInputValue" }
sampleField: "sampleArgValue"
)
}`;

const { data } = await graphql(schema, query);

expect(data!.baseClassQuery).toEqual("sampleStaticMethod");
expect(inputFieldValue).toEqual("sampleInputValue");
expect(argsFieldValue).toEqual("sampleArgValue");
});
});
});

0 comments on commit 315d7d5

Please sign in to comment.