From 570c3180b95801fdd43d0730d5825c1facec817f Mon Sep 17 00:00:00 2001 From: Trevor Scheer Date: Sat, 26 Mar 2022 08:59:04 -0700 Subject: [PATCH] [graphql] Add support for printing `SchemaExtension` nodes (#12519) * Add support for printing SchemaExtension nodes * Add changelog entry * Lint * Update 12519.md Co-authored-by: fisker Cheung --- changelog_unreleased/graphql/12519.md | 17 ++++++++++++++ src/language-graphql/printer-graphql.js | 23 +++++++++++++++++++ .../__snapshots__/jsfmt.spec.js.snap | 8 +++++++ .../kitchen-sink/schema_kitchen_sink.graphql | 4 ++++ .../schema/__snapshots__/jsfmt.spec.js.snap | 15 ++++++++++++ tests/format/graphql/schema/schema.graphql | 7 ++++++ 6 files changed, 74 insertions(+) create mode 100644 changelog_unreleased/graphql/12519.md diff --git a/changelog_unreleased/graphql/12519.md b/changelog_unreleased/graphql/12519.md new file mode 100644 index 000000000..16e13ba3f --- /dev/null +++ b/changelog_unreleased/graphql/12519.md @@ -0,0 +1,17 @@ +#### Add support for printing SchemaExtension nodes (#12519 by @trevor-scheer) + + +```graphql +# Input +extend schema { subscription: Subscription } +extend schema @directive + +# Prettier stable +N/A - throws error + +# Prettier main +extend schema { + subscription: Subscription +} +extend schema @directive +``` diff --git a/src/language-graphql/printer-graphql.js b/src/language-graphql/printer-graphql.js index 883927577..a2043a5bc 100644 --- a/src/language-graphql/printer-graphql.js +++ b/src/language-graphql/printer-graphql.js @@ -402,6 +402,29 @@ function genericPrint(path, options, print) { ]; } + case "SchemaExtension": { + return [ + "extend schema", + printDirectives(path, print, node), + ...(node.operationTypes.length > 0 + ? [ + " {", + indent([ + hardline, + join( + hardline, + path.call( + (opsPath) => printSequence(opsPath, options, print), + "operationTypes" + ) + ), + ]), + hardline, + "}", + ] + : []), + ]; + } case "SchemaDefinition": { return [ print("description"), diff --git a/tests/format/graphql/kitchen-sink/__snapshots__/jsfmt.spec.js.snap b/tests/format/graphql/kitchen-sink/__snapshots__/jsfmt.spec.js.snap index c930d15b2..bc9c3d3e3 100644 --- a/tests/format/graphql/kitchen-sink/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/graphql/kitchen-sink/__snapshots__/jsfmt.spec.js.snap @@ -149,6 +149,10 @@ schema { mutation: MutationType } +extend schema @directive { + subscription: Subscription +} + """ This is a description of the \`Foo\` type. @@ -271,6 +275,10 @@ schema { mutation: MutationType } +extend schema @directive { + subscription: Subscription +} + """ This is a description of the \`Foo\` type. diff --git a/tests/format/graphql/kitchen-sink/schema_kitchen_sink.graphql b/tests/format/graphql/kitchen-sink/schema_kitchen_sink.graphql index 0c72dcca2..e4a5327bf 100644 --- a/tests/format/graphql/kitchen-sink/schema_kitchen_sink.graphql +++ b/tests/format/graphql/kitchen-sink/schema_kitchen_sink.graphql @@ -8,6 +8,10 @@ schema { mutation: MutationType } +extend schema @directive { + subscription: Subscription +} + """ This is a description of the `Foo` type. diff --git a/tests/format/graphql/schema/__snapshots__/jsfmt.spec.js.snap b/tests/format/graphql/schema/__snapshots__/jsfmt.spec.js.snap index 250af029e..280b7dafd 100644 --- a/tests/format/graphql/schema/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/graphql/schema/__snapshots__/jsfmt.spec.js.snap @@ -13,6 +13,13 @@ schema { subscription: Subscription } +extend schema { subscription: Subscription } + +# \`SchemaExtension\`s don't require any operation types and doesn't print +# curlies in their abscence. +extend schema + @directive + =====================================output===================================== """ Schema definition description @@ -23,5 +30,13 @@ schema { subscription: Subscription } +extend schema { + subscription: Subscription +} + +# \`SchemaExtension\`s don't require any operation types and doesn't print +# curlies in their abscence. +extend schema @directive + ================================================================================ `; diff --git a/tests/format/graphql/schema/schema.graphql b/tests/format/graphql/schema/schema.graphql index 4ffc3dcfe..f426bfad4 100644 --- a/tests/format/graphql/schema/schema.graphql +++ b/tests/format/graphql/schema/schema.graphql @@ -4,3 +4,10 @@ schema { mutation: Mutation subscription: Subscription } + +extend schema { subscription: Subscription } + +# `SchemaExtension`s don't require any operation types and doesn't print +# curlies in their abscence. +extend schema + @directive