From 6d00644c7820462f0e4e8bbd6eb3f8bbbfa880c3 Mon Sep 17 00:00:00 2001 From: Sashko Stubailo Date: Tue, 6 Jun 2017 08:26:04 -0700 Subject: [PATCH] Add GraphQL variable definition support (#1995) --- src/printer-graphql.js | 39 ++++++++++++++++--- .../__snapshots__/jsfmt.spec.js.snap | 25 ++++++++++++ .../jsfmt.spec.js | 1 + .../variable_definitions.graphql | 8 ++++ 4 files changed, 68 insertions(+), 5 deletions(-) create mode 100644 tests/graphql_variable_definitions/__snapshots__/jsfmt.spec.js.snap create mode 100644 tests/graphql_variable_definitions/jsfmt.spec.js create mode 100644 tests/graphql_variable_definitions/variable_definitions.graphql diff --git a/src/printer-graphql.js b/src/printer-graphql.js index c32fa4dd..79a2cd4a 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 00000000..4dbf93c9 --- /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 00000000..335646fb --- /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 00000000..d50a2f1b --- /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 +}