Skip to content

Commit

Permalink
Support deprecated directive on enum values (#4351)
Browse files Browse the repository at this point in the history
  • Loading branch information
CarsonF committed Apr 1, 2022
1 parent 20fec9d commit 904c084
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/grumpy-dragons-cry.md
@@ -0,0 +1,5 @@
---
'@graphql-tools/utils': patch
---

Support deprecated directive on enum values
4 changes: 2 additions & 2 deletions packages/utils/src/print-schema-with-directives.ts
Expand Up @@ -256,7 +256,7 @@ export function getDirectiveNodes(
}

export function getDeprecatableDirectiveNodes(
entity: GraphQLArgument | GraphQLField<any, any> | GraphQLInputField,
entity: GraphQLArgument | GraphQLField<any, any> | GraphQLInputField | GraphQLEnumValue,
schema?: GraphQLSchema,
pathToDirectivesInExtensions?: Array<string>
): Array<DirectiveNode> {
Expand Down Expand Up @@ -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,
};
}

Expand Down
28 changes: 28 additions & 0 deletions packages/utils/tests/print-schema-with-directives.spec.ts
Expand Up @@ -4,6 +4,7 @@ import { stitchSchemas } from '@graphql-tools/stitch';
import {
buildSchema,
GraphQLDirective,
GraphQLEnumType,
printSchema,
GraphQLSchema,
specifiedDirectives,
Expand Down Expand Up @@ -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 */ `
Expand Down

0 comments on commit 904c084

Please sign in to comment.