gqlparser icon indicating copy to clipboard operation
gqlparser copied to clipboard

Add `minified` option to allow removing the newline and any extra whitespace

Open git-hulk opened this issue 10 months ago • 3 comments

For more information, could refer to https://github.com/vektah/gqlparser/pull/362.

git-hulk avatar Apr 08 '25 03:04 git-hulk

For this issue, I would like to hear suggestions from you.

In summary, the solution to resolve this issue is to:

  • Set emitComments = false
  • Set omitDescription = true
  • Set ident = ""
  • Set compacted = true
  • Use white space(" ") instead of \n in WriteNewline

Does that sound good to you? It needs to control by the compacted option if it wants to remove more white space.

cc @StevenACoffman

git-hulk avatar Apr 10 '25 13:04 git-hulk

I think so, but would like to see what standard you are testing your modification against. Apollo? @giautm What are your thoughts?

StevenACoffman avatar Apr 10 '25 17:04 StevenACoffman

To be honest, I don't use Apollo and don't know exactly how it normalizes the schema. However, from the workaround in issue #337, I guess it should be basically aligned with Apollo in query except for the need to remove the trailing newline.

formatter.FormatQueryDocument(doc)
normalizedQuery := buf.String()
normalizedQuery = strings.ReplaceAll(normalizedQuery, " (", "(")     // Remove space before parentheses
normalizedQuery = strings.ReplaceAll(normalizedQuery, "... ", "...") // Remove space before parentheses
normalizedQuery = strings.TrimSpace(normalizedQuery)                 // Remove trailing newlines and whitespace

And I haven't found any places that declare how to normalize GraphQL. So, from my perspective, I preferred to hold this feature unless any users explicitly request it. What do you think? @StevenACoffman

git-hulk avatar Apr 11 '25 03:04 git-hulk