Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support option to remove padding for FormatQueryDocument #337

Open
hienqn opened this issue Dec 13, 2024 · 1 comment
Open

Support option to remove padding for FormatQueryDocument #337

hienqn opened this issue Dec 13, 2024 · 1 comment

Comments

@hienqn
Copy link

hienqn commented Dec 13, 2024

What happened?

The query name and the query variables are padded, causing a inconsistent hashing with Apollo Router's feature called Safelisting

For example, for this query,

mutation CreatePost($userId: ID!, $postInput: PostInput!) {
      createPost(userId: $userId, input: $postInput) { 
             ...PostFields 
            author { 
                 ...UserFields 
             } 
       } 
}

fragment UserFields on User { id name email age }
fragment PostFields on Post { id title content createdAt }

or equivalent

fragment UserFields on User { id name email age } fragment PostFields on Post { id title content createdAt } query GetUserWithPosts($userId: ID!) { user(id: $userId) { ...UserFields posts { ...PostFields comments { id text } } } } query GetAllUsers { users { ...UserFields totalPosts } } mutation CreateUserPost($userId: ID!, $postInput: PostInput!) { createPost(userId: $userId, input: $postInput) { ...PostFields author { ...UserFields } } }

calling FormatQueryDocument would result in

mutation CreateUserPost ($userId: ID!, $postInput: PostInput!) {\n createPost(userId: $userId, input: $postInput) {\n ... PostFields\n author {\n ... UserFields\n }\n }\n}\nquery GetAllUsers {\n users {\n ... UserFields\n totalPosts\n }\n}\nquery GetUserWithPosts ($userId: ID!) {\n user(id: $userId) {\n ... UserFields\n posts {\n ... PostFields\n comments {\n id\n text\n }\n }\n }\n}\nfragment PostFields on Post {\n id\n title\n content\n createdAt\n}\nfragment UserFields on User {\n id\n name\n email\n age\n}\n

Notice there's a space padded between CreateUserPost and ($userId: ID!, $postInput: PostInput!), and also ... and UserFields

Unfortunately, this doesn't match Apollo's normalization rule so the formatted string is slightly different.

What did you expect?

I expect to see no space between operation name and the arguments. I think the problem is in FormatOperationDefinition where it called WriteWord, the padding configure is set to true so padding was added.

Minimal graphql.schema and models to reproduce

An example is above, and this is the current set up to produce the the same normalized string with Apollo. Without this, our ID will be inconsistent.

var buf strings.Builder
// Create a formatter configured for Apollo's style
formatter :=  formatter.NewFormatter(buf,
		formatter.WithIndent("  "), // Use two spaces for indentation
)

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

versions

  • go list -m github.com/vektah/gqlparser/v2?
  • go version?
github.com/vektah/gqlparser/v2 v2.5.16
go version go1.23.3 darwin/arm64
@StevenACoffman
Copy link
Collaborator

StevenACoffman commented Dec 14, 2024

Hey, thanks for filing this issue. I would welcome a pull request for this, as I think the community would benefit from it.
Are you able to provide one?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants