diff --git a/src/printer-graphql.js b/src/printer-graphql.js index c32fa4dd8..79a2cd4a6 100644 --- a/src/printer-graphql.js +++ b/src/printer-graphql.js @@ -27,7 +27,24 @@ function genericPrint(path, options, print) { return concat([ n.name === null ? "" : concat([n.operation, " "]), path.call(print, "name"), - n.name ? " " : "", + n.variableDefinitions && n.variableDefinitions.length + ? group( + concat([ + "(", + indent( + concat([ + softline, + join( + concat([",", ifBreak("", " "), softline]), + path.map(print, "variableDefinitions") + ) + ]) + ), + softline, + ") " + ]) + ) + : n.name ? " " : "", path.call(print, "selectionSet") ]); } @@ -74,10 +91,9 @@ function genericPrint(path, options, print) { case "StringValue": { return concat(['"', n.value, '"']); } - case "IntValue": { - return n.value; - } - case "FloatValue": { + case "IntValue": + case "FloatValue": + case "EnumValue": { return n.value; } case "BooleanValue": { @@ -112,6 +128,19 @@ function genericPrint(path, options, print) { ]); } + case "NamedType": { + return path.call(print, "name"); + } + + case "VariableDefinition": { + return concat([ + path.call(print, "variable"), + ": ", + path.call(print, "type"), + n.defaultValue ? concat([" = ", path.call(print, "defaultValue")]) : "" + ]); + } + default: throw new Error("unknown graphql type: " + JSON.stringify(n.kind)); } diff --git a/tests/graphql_variable_definitions/__snapshots__/jsfmt.spec.js.snap b/tests/graphql_variable_definitions/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 000000000..4dbf93c98 --- /dev/null +++ b/tests/graphql_variable_definitions/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,25 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`variable_definitions.graphql 1`] = ` +query short($foo:ComplexType, $site : Site = MOBILE) { + hello +} + +query long($foo: ComplexType, $site: Float = 124241.12312, +$bar: String = "Long string here", $arg: String = "Hello world!",,,,) { + hello +} +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +query short($foo: ComplexType, $site: Site = MOBILE) { + hello +} + +query long( + $foo: ComplexType, + $site: Float = 124241.12312, + $bar: String = "Long string here", + $arg: String = "Hello world!" +) { + hello +} +`; diff --git a/tests/graphql_variable_definitions/jsfmt.spec.js b/tests/graphql_variable_definitions/jsfmt.spec.js new file mode 100644 index 000000000..335646fb2 --- /dev/null +++ b/tests/graphql_variable_definitions/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname, { parser: "graphql" }); diff --git a/tests/graphql_variable_definitions/variable_definitions.graphql b/tests/graphql_variable_definitions/variable_definitions.graphql new file mode 100644 index 000000000..d50a2f1b1 --- /dev/null +++ b/tests/graphql_variable_definitions/variable_definitions.graphql @@ -0,0 +1,8 @@ +query short($foo:ComplexType, $site : Site = MOBILE) { + hello +} + +query long($foo: ComplexType, $site: Float = 124241.12312, +$bar: String = "Long string here", $arg: String = "Hello world!",,,,) { + hello +}