graphql-go / graphql

An implementation of GraphQL for Go / Golang
MIT License
9.87k stars 839 forks source link

Incorrect quotation when printing StringValue node #586

Closed pkosiec closed 3 years ago

pkosiec commented 3 years ago

Hi,

I noticed an issue with printing StringValue value (./language/printer/printer.go#L372). The quotation of the string value is incorrect. If the input StringValue node contains escaped double quote characters (\"), then the output string from printer drops proper character escaping.

Steps to reproduce

If we get a following query and parse it to *ast.Document, and then print it to a string:

queryAst := `query { foo(jsonStr: "{\"foo\": \"bar\"}") }`
astDoc, _ := parser.Parse(parser.ParseParams{
        Source: query,
        Options: parser.ParseOptions{
            NoLocation: true,
        },
    })
result := printer.Print(astDoc)

Expected result

We should expect the result string equal to:

{
  foo(jsonStr: "{\"foo\": \"bar\"}")
}

Actual result

However, what we get instead is:

{
  foo(jsonStr: "{"foo": "bar"}")
}

Which makes it incorrect query.

I will provide a pull request with a unit test case and fix for that issue.

Cheers!

pkosiec commented 3 years ago

Hi @yhc44 @chris-ramon, could you please take a look at this issue and my pull request which fixes it? Thanks a lot!