Skip to content

Commit

Permalink
fix: add field directives on interface types
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-advantitge committed Mar 21, 2021
1 parent 549dd5d commit 41ad53a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
22 changes: 16 additions & 6 deletions lib/schema-builder/factories/interface-definition.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,15 @@ export class InterfaceDefinitionFactory {
return () => {
let fields: GraphQLFieldConfigMap<any, any> = {};
metadata.properties.forEach((field) => {
const type = this.outputTypeFactory.create(
field.name,
field.typeFn(),
options,
field.options,
);
fields[field.schemaName] = {
description: field.description,
type: this.outputTypeFactory.create(
field.name,
field.typeFn(),
options,
field.options,
),
type,
args: this.argsFactory.create(field.methodArgs, options),
resolve: (root: object) => {
const value = root[field.name];
Expand All @@ -131,6 +132,15 @@ export class InterfaceDefinitionFactory {
: value;
},
deprecationReason: field.deprecationReason,
/**
* AST node has to be manually created in order to define directives
* (more on this topic here: https://github.com/graphql/graphql-js/issues/1343)
*/
astNode: this.astDefinitionNodeFactory.createFieldNode(
field.name,
type,
field.directives,
),
extensions: {
complexity: field.complexity,
...field.extensions,
Expand Down
5 changes: 5 additions & 0 deletions tests/code-first-federation/recipe/recipe.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
Directive,
Field,
ID,
InterfaceType,
Expand All @@ -19,6 +20,10 @@ export abstract class Base {
export abstract class IRecipe extends Base {
@Field()
title: string;

@Field()
@Directive('@external')
externalField: string;
}

@ObjectType({ implements: IRecipe })
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/code-first-federation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('Code-first - Federation', () => {
expect(response.data).toEqual({
_service: {
sdl:
'"""Search result description"""\nunion FederationSearchResultUnion = Post | User\n\ninterface IRecipe {\n id: ID!\n title: String!\n}\n\ntype Post @key(fields: "id") {\n id: ID!\n title: String!\n authorId: Int!\n}\n\ntype Query {\n findPost(id: Float!): Post!\n getPosts: [Post!]!\n search: [FederationSearchResultUnion!]! @deprecated(reason: "test")\n recipe: IRecipe!\n}\n\ntype Recipe implements IRecipe {\n id: ID!\n title: String!\n description: String!\n}\n\ntype User @extends @key(fields: "id") {\n id: ID! @external\n posts: [Post!]!\n}\n',
'"""Search result description"""\nunion FederationSearchResultUnion = Post | User\n\ninterface IRecipe {\n id: ID!\n title: String!\n externalField: String! @external\n}\n\ntype Post @key(fields: "id") {\n id: ID!\n title: String!\n authorId: Int!\n}\n\ntype Query {\n findPost(id: Float!): Post!\n getPosts: [Post!]!\n search: [FederationSearchResultUnion!]! @deprecated(reason: "test")\n recipe: IRecipe!\n}\n\ntype Recipe implements IRecipe {\n id: ID!\n title: String!\n externalField: String! @external\n description: String!\n}\n\ntype User @extends @key(fields: "id") {\n id: ID! @external\n posts: [Post!]!\n}\n',
},
});
});
Expand Down

0 comments on commit 41ad53a

Please sign in to comment.