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(schema): Fix directives cannot be extends #626

Merged
merged 5 commits into from
May 7, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<!-- here goes all the unreleased changes descriptions -->
### Features
- expose `createResolversMap` utility that generates apollo-like resolvers object
### Fixes
- properly inherit directives while extending `@InputType` or `@ObjectType` classes (#626)

## v1.0.0-rc.1
### Features
Expand Down
2 changes: 2 additions & 0 deletions src/schema/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export function getFieldMetadataFromInputType(type: GraphQLInputObjectType) {
const superField = fieldInfo[fieldName];
fieldsMap[fieldName] = {
type: superField.type,
astNode: superField.astNode,
description: superField.description,
defaultValue: superField.defaultValue,
};
Expand All @@ -36,6 +37,7 @@ export function getFieldMetadataFromObjectType(type: GraphQLObjectType | GraphQL
argMap[name] = arg;
return argMap;
}, {}),
astNode: superField.astNode,
resolve: superField.resolve,
description: superField.description,
deprecationReason: superField.deprecationReason,
Expand Down
68 changes: 67 additions & 1 deletion tests/functional/directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ describe("Directives", () => {
append: string;
}

@InputType()
class SubDirectiveOnFieldInput extends DirectiveOnFieldInput {}

@InputType()
@Directive("@upper")
class DirectiveOnClassInput {
Expand Down Expand Up @@ -97,6 +100,14 @@ describe("Directives", () => {
}
}

@ObjectType()
class SubSampleObjectType extends SampleObjectType {
@Field()
withInput(@Arg("input") input: SubDirectiveOnFieldInput): string {
return `hello${input.append}`;
}
}

@Resolver()
class SampleResolver {
@Query(() => SampleObjectType)
Expand Down Expand Up @@ -206,8 +217,16 @@ describe("Directives", () => {
}
}

@Resolver(of => SubSampleObjectType)
class SubSampleResolver {
@Query(() => SubSampleObjectType)
subObjectType(): SubSampleObjectType {
return new SubSampleObjectType();
}
}

schema = await buildSchema({
resolvers: [SampleResolver, SampleObjectTypeResolver],
resolvers: [SampleResolver, SampleObjectTypeResolver, SubSampleResolver],
});

SchemaDirectiveVisitor.visitSchemaDirectives(schema, {
Expand Down Expand Up @@ -376,6 +395,16 @@ describe("Directives", () => {
expect(fields.append).toHaveProperty("astNode");
assertValidDirective(fields.append.astNode, "upper");
});

it("adds inherited field directives to input type fields while extending input type class", async () => {
const fields = (schema.getType(
"SubDirectiveOnFieldInput",
) as GraphQLInputObjectType).getFields();

expect(fields).toHaveProperty("append");
expect(fields.append).toHaveProperty("astNode");
assertValidDirective(fields.append.astNode, "upper");
});
});

describe("ObjectType", () => {
Expand Down Expand Up @@ -415,6 +444,43 @@ describe("Directives", () => {
fieldResolverWithAppendDefinition: "hello, world!",
});
});

it("call object type directives while extending field type class", async () => {
const query = `query {
subObjectType {
withDirective
# withDirectiveWithArgs
withUpper
withUpperDefinition
withAppend(append: ", world!")
withAppendDefinition(append: ", world!")
withUpperAndAppend(append: ", world!")
withInput(input: { append: ", world!" })
withInputUpper(input: { append: ", world!" })
withInputOnClass(input: { append: ", world!" })
withInputUpperOnClass(input: { append: ", world!" })
fieldResolverWithAppendDefinition(append: ", world!")
}
}`;

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

expect(data).toHaveProperty("subObjectType");
expect(data!.subObjectType).toEqual({
withDirective: "withDirective",
// withDirectiveWithArgs: "withDirectiveWithArgs",
withUpper: "WITHUPPER",
withUpperDefinition: "WITHUPPERDEFINITION",
withAppend: "hello, world!",
withAppendDefinition: "hello, world!",
withUpperAndAppend: "HELLO, WORLD!",
withInput: "hello, WORLD!",
withInputUpper: "HELLO, WORLD!",
withInputOnClass: "hello, WORLD!",
withInputUpperOnClass: "HELLO, WORLD!",
fieldResolverWithAppendDefinition: "hello, world!",
});
});
});
});

Expand Down