diff --git a/.changeset/ninety-plums-explode.md b/.changeset/ninety-plums-explode.md new file mode 100644 index 00000000000..42c1bf1bc58 --- /dev/null +++ b/.changeset/ninety-plums-explode.md @@ -0,0 +1,5 @@ +--- +'@graphql-tools/utils': patch +--- + +Add all args from extensions to the AST even if they don't exist in the directive def diff --git a/packages/utils/src/print-schema-with-directives.ts b/packages/utils/src/print-schema-with-directives.ts index 59610ad73ed..70a8098da40 100644 --- a/packages/utils/src/print-schema-with-directives.ts +++ b/packages/utils/src/print-schema-with-directives.ts @@ -45,6 +45,7 @@ import { TypeDefinitionNode, TypeExtensionNode, UnionTypeDefinitionNode, + ValueNode, } from 'graphql'; import { astFromType } from './astFromType.js'; import { astFromValue } from './astFromValue.js'; @@ -519,38 +520,27 @@ export function makeDirectiveNode( ): DirectiveNode { const directiveArguments: Array = []; - if (directive != null) { - for (const arg of directive.args) { - const argName = arg.name; - const argValue = args?.[argName]; - if (argValue !== undefined) { - const value = astFromValue(argValue, arg.type); - if (value) { - directiveArguments.push({ - kind: Kind.ARGUMENT, - name: { - kind: Kind.NAME, - value: argName, - }, - value, - }); - } + for (const argName in args) { + const argValue = args[argName]; + let value: Maybe; + if (directive != null) { + const arg = directive.args.find(arg => arg.name === argName); + if (arg) { + value = astFromValue(argValue, arg.type); } } - } else { - for (const argName in args) { - const argValue = args[argName]; - const value = astFromValueUntyped(argValue); - if (value) { - directiveArguments.push({ - kind: Kind.ARGUMENT, - name: { - kind: Kind.NAME, - value: argName, - }, - value, - }); - } + if (value == null) { + value = astFromValueUntyped(argValue); + } + if (value != null) { + directiveArguments.push({ + kind: Kind.ARGUMENT, + name: { + kind: Kind.NAME, + value: argName, + }, + value, + }); } }