Skip to content

Commit

Permalink
fix(graphql): fix directives on arguments
Browse files Browse the repository at this point in the history
There has been a lot of issues with directives not being applied
and registered. Most of those cases have been fixed by simply forcing
the IDL (like explained here graphql/graphql-js#1343).
Unfortunately, arguments have been forgotten, meaning that no directive
applied on arguments were actually run or even detected. This PR stands
to correct that and properly add directive to needed fields.
  • Loading branch information
Arno500 committed Nov 3, 2023
1 parent cb3b0b8 commit 376c94d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
26 changes: 20 additions & 6 deletions packages/graphql/lib/schema-builder/factories/args.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ import { getDefaultValue } from '../helpers/get-default-value.helper';
import { ClassMetadata, MethodArgsMetadata } from '../metadata';
import { TypeMetadataStorage } from '../storages/type-metadata.storage';
import { InputTypeFactory } from './input-type.factory';
import { AstDefinitionNodeFactory } from './ast-definition-node.factory';

@Injectable()
export class ArgsFactory {
constructor(private readonly inputTypeFactory: InputTypeFactory) {}
constructor(
private readonly inputTypeFactory: InputTypeFactory,
private readonly astDefinitionNodeFactory: AstDefinitionNodeFactory,
) {}

public create(
args: MethodArgsMetadata[],
Expand Down Expand Up @@ -73,15 +77,25 @@ export class ArgsFactory {
);

const { schemaName } = field;
const type = this.inputTypeFactory.create(
field.name,
field.typeFn(),
options,
field.options,
);
fieldConfigMap[schemaName] = {
description: field.description,
type: this.inputTypeFactory.create(
type,
defaultValue: field.options.defaultValue,
/**
* AST node has to be manually created in order to define directives
* (more on this topic here: https:/graphql/graphql-js/issues/1343)
*/
astNode: this.astDefinitionNodeFactory.createArgNode(
field.name,
field.typeFn(),
options,
field.options,
type,
field.directives,
),
defaultValue: field.options.defaultValue,
};
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,32 @@ export class AstDefinitionNodeFactory {
};
}

createArgNode(
name: string,
type: GraphQLInputType,
directiveMetadata?: DirectiveMetadata[],
): InputValueDefinitionNode | undefined {
if (isEmpty(directiveMetadata)) {
return;
}

return {
kind: Kind.INPUT_VALUE_DEFINITION,
type: {
kind: Kind.NAMED_TYPE,
name: {
kind: Kind.NAME,
value: type.toString(),
},
},
name: {
kind: Kind.NAME,
value: name,
},
directives: directiveMetadata.map(this.createDirectiveNode),
};
}

private createDirectiveNode(
directive: DirectiveMetadata,
): ConstDirectiveNode {
Expand Down

0 comments on commit 376c94d

Please sign in to comment.