diff --git a/.changeset/grumpy-dragons-cry.md b/.changeset/grumpy-dragons-cry.md new file mode 100644 index 00000000000..2463f579c46 --- /dev/null +++ b/.changeset/grumpy-dragons-cry.md @@ -0,0 +1,5 @@ +--- +'@graphql-tools/utils': patch +--- + +Support deprecated directive on enum values diff --git a/packages/utils/src/print-schema-with-directives.ts b/packages/utils/src/print-schema-with-directives.ts index 1a844408cd9..4ffa9223523 100644 --- a/packages/utils/src/print-schema-with-directives.ts +++ b/packages/utils/src/print-schema-with-directives.ts @@ -256,7 +256,7 @@ export function getDirectiveNodes( } export function getDeprecatableDirectiveNodes( - entity: GraphQLArgument | GraphQLField | GraphQLInputField, + entity: GraphQLArgument | GraphQLField | GraphQLInputField | GraphQLEnumValue, schema?: GraphQLSchema, pathToDirectivesInExtensions?: Array ): Array { @@ -574,7 +574,7 @@ export function astFromEnumValue( value: value.name, }, // ConstXNode has been introduced in v16 but it is not compatible with XNode so we do `as any` for backwards compatibility - directives: getDirectiveNodes(value, schema, pathToDirectivesInExtensions) as any, + directives: getDeprecatableDirectiveNodes(value, schema, pathToDirectivesInExtensions) as any, }; } diff --git a/packages/utils/tests/print-schema-with-directives.spec.ts b/packages/utils/tests/print-schema-with-directives.spec.ts index ecbc00ba4f0..fe6574b181c 100644 --- a/packages/utils/tests/print-schema-with-directives.spec.ts +++ b/packages/utils/tests/print-schema-with-directives.spec.ts @@ -4,6 +4,7 @@ import { stitchSchemas } from '@graphql-tools/stitch'; import { buildSchema, GraphQLDirective, + GraphQLEnumType, printSchema, GraphQLSchema, specifiedDirectives, @@ -249,6 +250,33 @@ describe('printSchemaWithDirectives', () => { expect(output).toContain('directive @dummy on QUERY'); }); + it(`Should print enum value deprecations correctly if they don't have astNode`, () => { + const schema = new GraphQLSchema({ + query: new GraphQLObjectType({ + name: 'Query', + fields: {}, + }), + types: [ + new GraphQLEnumType({ + name: 'Color', + values: { + RED: { + value: 'RED', + deprecationReason: 'No longer supported', + }, + BLUE: { + value: 'BLUE', + }, + }, + }), + ], + }); + + const output = printSchemaWithDirectives(schema); + + expect(output).toContain('RED @deprecated(reason: "No longer supported")'); + }); + it('should print comments', () => { const schema = makeExecutableSchema({ typeDefs: /* GraphQL */ `