From 16ac5fa9f2255548ba9164f2922d60d73f8d4ba3 Mon Sep 17 00:00:00 2001 From: Lee Byron Date: Thu, 29 Mar 2018 13:16:13 -0700 Subject: [PATCH] Fix printing of ASTs of argument definitions with descriptions. Descriptions were not separated on their own lines, which made for technically parseable but hard to read output. Fixes #1285 --- src/language/__tests__/schema-kitchen-sink.graphql | 10 +++++++++- src/language/__tests__/schema-printer-test.js | 10 +++++++++- src/language/printer.js | 8 ++++++-- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/language/__tests__/schema-kitchen-sink.graphql b/src/language/__tests__/schema-kitchen-sink.graphql index f94f47c8e5..5ce4ca6a06 100644 --- a/src/language/__tests__/schema-kitchen-sink.graphql +++ b/src/language/__tests__/schema-kitchen-sink.graphql @@ -14,7 +14,15 @@ of the `Foo` type. """ type Foo implements Bar & Baz { one: Type - two(argument: InputType!): Type + """ + This is a description of the `two` field. + """ + two( + """ + This is a description of the `argument` argument. + """ + argument: InputType! + ): Type three(argument: InputType, other: String): Int four(argument: String = "string"): String five(argument: [String] = ["string", "string"]): String diff --git a/src/language/__tests__/schema-printer-test.js b/src/language/__tests__/schema-printer-test.js index cb3a3cd4c6..00dd5d70b3 100644 --- a/src/language/__tests__/schema-printer-test.js +++ b/src/language/__tests__/schema-printer-test.js @@ -58,7 +58,15 @@ describe('Printer: SDL document', () => { """ type Foo implements Bar & Baz { one: Type - two(argument: InputType!): Type + """ + This is a description of the \`two\` field. + """ + two( + """ + This is a description of the \`argument\` argument. + """ + argument: InputType! + ): Type three(argument: InputType, other: String): Int four(argument: String = "string"): String five(argument: [String] = ["string", "string"]): String diff --git a/src/language/printer.js b/src/language/printer.js index daf984d7e8..558b0f7e0e 100644 --- a/src/language/printer.js +++ b/src/language/printer.js @@ -131,7 +131,9 @@ const printDocASTReducer = { FieldDefinition: addDescription( ({ name, arguments: args, type, directives }) => name + - wrap('(', join(args, ', '), ')') + + (args.every(arg => arg.indexOf('\n') === -1) + ? wrap('(', join(args, ', '), ')') + : wrap('(\n', indent(join(args, '\n')), '\n)')) + ': ' + type + wrap(' ', join(directives, ' ')), @@ -212,7 +214,9 @@ const printDocASTReducer = { ({ name, arguments: args, locations }) => 'directive @' + name + - wrap('(', join(args, ', '), ')') + + (args.every(arg => arg.indexOf('\n') === -1) + ? wrap('(', join(args, ', '), ')') + : wrap('(\n', indent(join(args, '\n')), '\n)')) + ' on ' + join(locations, ' | '), ),