diff --git a/src/printer-graphql.js b/src/printer-graphql.js index 33540a0f0..a45c83d4a 100644 --- a/src/printer-graphql.js +++ b/src/printer-graphql.js @@ -4,6 +4,7 @@ const docBuilders = require("./doc-builders"); const concat = docBuilders.concat; const join = docBuilders.join; const hardline = docBuilders.hardline; +const line = docBuilders.line; const softline = docBuilders.softline; const group = docBuilders.group; const indent = docBuilders.indent; @@ -225,6 +226,23 @@ function genericPrint(path, options, print) { ]); } + case "UnionTypeDefinition": { + return group( + concat([ + "union ", + path.call(print, "name"), + " =", + ifBreak("", " "), + indent( + concat([ + ifBreak(concat([line, " "])), + join(concat([line, "| "]), path.map(print, "types")) + ]) + ) + ]) + ); + } + default: throw new Error("unknown graphql type: " + JSON.stringify(n.kind)); } diff --git a/tests/graphql_union_types/__snapshots__/jsfmt.spec.js.snap b/tests/graphql_union_types/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 000000000..016a74fc9 --- /dev/null +++ b/tests/graphql_union_types/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,56 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`union_types.graphql 1`] = ` +union myUnion = ATypeName | ASecondTypeName + +union tooLongNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee = A | B + +union mySecondUnion = ATypeName | ASecondTypeName | AThirdTypeName + +union myThirdUnion = AVeryVeryVeryLongNamedTypeName | ASecondVeryVeryVeryLongedNameTypeName + +union longUnion = A | B | C | D | E | F | G | H | I | J | K | L | A | B | C | D | E | F | G | H | I | J | K | L + +# comment +# comment2 +union union = B | C | D~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +union myUnion = ATypeName | ASecondTypeName + +union tooLongNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee = + A + | B + +union mySecondUnion = ATypeName | ASecondTypeName | AThirdTypeName + +union myThirdUnion = + AVeryVeryVeryLongNamedTypeName + | ASecondVeryVeryVeryLongedNameTypeName + +union longUnion = + A + | B + | C + | D + | E + | F + | G + | H + | I + | J + | K + | L + | A + | B + | C + | D + | E + | F + | G + | H + | I + | J + | K + | L + +union union = B | C | D +`; diff --git a/tests/graphql_union_types/jsfmt.spec.js b/tests/graphql_union_types/jsfmt.spec.js new file mode 100644 index 000000000..335646fb2 --- /dev/null +++ b/tests/graphql_union_types/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname, { parser: "graphql" }); diff --git a/tests/graphql_union_types/union_types.graphql b/tests/graphql_union_types/union_types.graphql new file mode 100644 index 000000000..6823a860a --- /dev/null +++ b/tests/graphql_union_types/union_types.graphql @@ -0,0 +1,13 @@ +union myUnion = ATypeName | ASecondTypeName + +union tooLongNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee = A | B + +union mySecondUnion = ATypeName | ASecondTypeName | AThirdTypeName + +union myThirdUnion = AVeryVeryVeryLongNamedTypeName | ASecondVeryVeryVeryLongedNameTypeName + +union longUnion = A | B | C | D | E | F | G | H | I | J | K | L | A | B | C | D | E | F | G | H | I | J | K | L + +# comment +# comment2 +union union = B | C | D \ No newline at end of file