From fd180fb57500eddde74be36adb5c6367a61098f6 Mon Sep 17 00:00:00 2001 From: Hafiz Ismail Date: Tue, 8 Mar 2016 12:04:26 +0800 Subject: [PATCH] Commit: 6741c3192d0c0d5a1f6a9185adcf083b01699935 [6741c31] Parents: 72e90c0db1 Author: Hyohyeon Jeong Date: 9 February 2016 at 3:16:33 AM SGT Committer: Lee Byron Commit Date: 9 February 2016 at 4:15:57 PM SGT Provides a correct OperationType without name in GraphQLPrinter --- language/printer/printer.go | 15 ++++---- language/printer/printer_test.go | 62 ++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 6 deletions(-) diff --git a/language/printer/printer.go b/language/printer/printer.go index 56c36dda..4cca90e4 100644 --- a/language/printer/printer.go +++ b/language/printer/printer.go @@ -147,35 +147,38 @@ var printDocASTReducer = map[string]visitor.VisitFunc{ op := node.Operation name := fmt.Sprintf("%v", node.Name) - defs := wrap("(", join(toSliceString(node.VariableDefinitions), ", "), ")") + varDefs := wrap("(", join(toSliceString(node.VariableDefinitions), ", "), ")") directives := join(toSliceString(node.Directives), " ") selectionSet := fmt.Sprintf("%v", node.SelectionSet) + // Anonymous queries with no directives or variable definitions can use + // the query short form. str := "" - if name == "" { + if name == "" && directives == "" && varDefs == "" && op == "query" { str = selectionSet } else { str = join([]string{ op, - join([]string{name, defs}, ""), + join([]string{name, varDefs}, ""), directives, selectionSet, }, " ") } return visitor.ActionUpdate, str case map[string]interface{}: + op := getMapValueString(node, "Operation") name := getMapValueString(node, "Name") - defs := wrap("(", join(toSliceString(getMapValue(node, "VariableDefinitions")), ", "), ")") + varDefs := wrap("(", join(toSliceString(getMapValue(node, "VariableDefinitions")), ", "), ")") directives := join(toSliceString(getMapValue(node, "Directives")), " ") selectionSet := getMapValueString(node, "SelectionSet") str := "" - if name == "" { + if name == "" && directives == "" && varDefs == "" && op == "query" { str = selectionSet } else { str = join([]string{ op, - join([]string{name, defs}, ""), + join([]string{name, varDefs}, ""), directives, selectionSet, }, " ") diff --git a/language/printer/printer_test.go b/language/printer/printer_test.go index 9190802a..bd4782e5 100644 --- a/language/printer/printer_test.go +++ b/language/printer/printer_test.go @@ -59,6 +59,68 @@ func TestPrinter_PrintsMinimalAST(t *testing.T) { } } +// TestPrinter_ProducesHelpfulErrorMessages +// Skipped, can't figure out how to pass in an invalid astDoc, which is already strongly-typed + +func TestPrinter_CorrectlyPrintsNonQueryOperationsWithoutName(t *testing.T) { + + // Test #1 + queryAstShorthanded := `query { id, name }` + expected := `{ + id + name +} +` + astDoc := parse(t, queryAstShorthanded) + results := printer.Print(astDoc) + + if !reflect.DeepEqual(expected, results) { + t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(results, expected)) + } + + // Test #2 + mutationAst := `mutation { id, name }` + expected = `mutation { + id + name +} +` + astDoc = parse(t, mutationAst) + results = printer.Print(astDoc) + + if !reflect.DeepEqual(expected, results) { + t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(results, expected)) + } + + // Test #3 + queryAstWithArtifacts := `query ($foo: TestType) @testDirective { id, name }` + expected = `query ($foo: TestType) @testDirective { + id + name +} +` + astDoc = parse(t, queryAstWithArtifacts) + results = printer.Print(astDoc) + + if !reflect.DeepEqual(expected, results) { + t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(results, expected)) + } + + // Test #4 + mutationAstWithArtifacts := `mutation ($foo: TestType) @testDirective { id, name }` + expected = `mutation ($foo: TestType) @testDirective { + id + name +} +` + astDoc = parse(t, mutationAstWithArtifacts) + results = printer.Print(astDoc) + + if !reflect.DeepEqual(expected, results) { + t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(results, expected)) + } +} + func TestPrinter_PrintsKitchenSink(t *testing.T) { b, err := ioutil.ReadFile("../../kitchen-sink.graphql") if err != nil {