diff --git a/language/printer/printer.go b/language/printer/printer.go index eba872bb..ac771ba6 100644 --- a/language/printer/printer.go +++ b/language/printer/printer.go @@ -2,6 +2,7 @@ package printer import ( "fmt" + "strconv" "strings" "reflect" @@ -372,7 +373,7 @@ var printDocASTReducer = map[string]visitor.VisitFunc{ "StringValue": func(p visitor.VisitFuncParams) (string, interface{}) { switch node := p.Node.(type) { case *ast.StringValue: - return visitor.ActionUpdate, `"` + fmt.Sprintf("%v", node.Value) + `"` + return visitor.ActionUpdate, strconv.Quote(node.Value) case map[string]interface{}: return visitor.ActionUpdate, `"` + getMapValueString(node, "Value") + `"` } diff --git a/language/printer/printer_test.go b/language/printer/printer_test.go index 1c48426d..b6d7de7d 100644 --- a/language/printer/printer_test.go +++ b/language/printer/printer_test.go @@ -186,3 +186,17 @@ fragment frag on Follower { t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, results)) } } + +func TestPrinter_CorrectlyPrintsStringArgumentsWithProperQuoting(t *testing.T) { + queryAst := `query { foo(jsonStr: "{\"foo\": \"bar\"}") }` + expected := `{ + foo(jsonStr: "{\"foo\": \"bar\"}") +} +` + astDoc := parse(t, queryAst) + results := printer.Print(astDoc) + + if !reflect.DeepEqual(expected, results) { + t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, results)) + } +}