diff --git a/changelog_unreleased/graphql/19171.md b/changelog_unreleased/graphql/19171.md new file mode 100644 index 000000000..32e43bcd0 --- /dev/null +++ b/changelog_unreleased/graphql/19171.md @@ -0,0 +1,17 @@ +#### Support directives on directive definitions (#19171 by @YBJ0000) + +[Directives on directive definitions](https://github.com/graphql/graphql-js/pull/4521) (and `extend directive` extensions) are now supported. + + +```gql +# Input +directive @a @b on QUERY +extend directive @a @b + +# Prettier stable +Error: Syntax Error: Expected "on", found "@". + +# Prettier main +directive @a @b on QUERY +extend directive @a @b +``` diff --git a/src/language-graphql/parser-graphql.js b/src/language-graphql/parser-graphql.js index 4d318ce06..dd9fe9c82 100644 --- a/src/language-graphql/parser-graphql.js +++ b/src/language-graphql/parser-graphql.js @@ -17,6 +17,7 @@ function parseComments(ast) { const parseOptions = { allowLegacyFragmentVariables: true, + experimentalDirectivesOnDirectiveDefinitions: true, }; function createParseError(error) { diff --git a/src/language-graphql/printer-graphql.js b/src/language-graphql/printer-graphql.js index 38014911e..c4ec20b17 100644 --- a/src/language-graphql/printer-graphql.js +++ b/src/language-graphql/printer-graphql.js @@ -290,11 +290,19 @@ function genericPrint(path, options, print) { ")", ]) : "", + printDirectives(path, print), node.repeatable ? " repeatable" : "", " on ", ...join(" | ", path.map(print, "locations")), ]; + case "DirectiveExtension": + return [ + "extend directive @", + print("name"), + printDirectives(path, print), + ]; + case "EnumTypeExtension": case "EnumTypeDefinition": return [ diff --git a/src/language-graphql/visitor-keys.js b/src/language-graphql/visitor-keys.js index ed7a0e3dc..1c8b081b8 100644 --- a/src/language-graphql/visitor-keys.js +++ b/src/language-graphql/visitor-keys.js @@ -13,10 +13,4 @@ for (const kind of [ delete visitorKeys[kind]; } -// Not supported yet -visitorKeys.DirectiveDefinition = visitorKeys.DirectiveDefinition.filter( - (key) => key !== "directives", -); -delete visitorKeys.DirectiveExtension; - export default visitorKeys; diff --git a/tests/format/graphql/directive-decl/__snapshots__/format.test.js.snap b/tests/format/graphql/directive-decl/__snapshots__/format.test.js.snap index 2838670e6..b4fb3abf8 100644 --- a/tests/format/graphql/directive-decl/__snapshots__/format.test.js.snap +++ b/tests/format/graphql/directive-decl/__snapshots__/format.test.js.snap @@ -42,3 +42,30 @@ directive @a(as: String = 1) on QUERY ================================================================================ `; + +exports[`directives-on-directive-definitions.graphql format 1`] = ` +====================================options===================================== +parsers: ["graphql"] + printWidth: 80 (default) | +=====================================input====================================== +directive @a @b on QUERY +directive @a(x: Int) @b @c on QUERY +directive @a @b repeatable on QUERY +directive @a(x: Int) @b repeatable on QUERY | MUTATION +directive @a @b(reason: "deprecated") on QUERY + +extend directive @a @b +extend directive @a @b @c + +=====================================output===================================== +directive @a @b on QUERY +directive @a(x: Int) @b @c on QUERY +directive @a @b repeatable on QUERY +directive @a(x: Int) @b repeatable on QUERY | MUTATION +directive @a @b(reason: "deprecated") on QUERY + +extend directive @a @b +extend directive @a @b @c + +================================================================================ +`; diff --git a/tests/format/graphql/directive-decl/directives-on-directive-definitions.graphql b/tests/format/graphql/directive-decl/directives-on-directive-definitions.graphql new file mode 100644 index 000000000..68eae7198 --- /dev/null +++ b/tests/format/graphql/directive-decl/directives-on-directive-definitions.graphql @@ -0,0 +1,8 @@ +directive @a @b on QUERY +directive @a(x: Int) @b @c on QUERY +directive @a @b repeatable on QUERY +directive @a(x: Int) @b repeatable on QUERY | MUTATION +directive @a @b(reason: "deprecated") on QUERY + +extend directive @a @b +extend directive @a @b @c diff --git a/tests/unit/__snapshots__/visitor-keys.js.snap b/tests/unit/__snapshots__/visitor-keys.js.snap index 1388d7b75..1e4f03015 100644 --- a/tests/unit/__snapshots__/visitor-keys.js.snap +++ b/tests/unit/__snapshots__/visitor-keys.js.snap @@ -1294,9 +1294,14 @@ exports[`visitor keys graphql 1`] = ` "DirectiveDefinition": [ "arguments", "description", + "directives", "locations", "name", ], + "DirectiveExtension": [ + "directives", + "name", + ], "Document": [ "definitions", ],