graphql-code-generator icon indicating copy to clipboard operation
graphql-code-generator copied to clipboard

Allow to have collapse whitespaces in query strings when documentMode: string is set

Open aradalvand opened this issue 4 years ago • 6 comments

There might be a reason why this isn't already implemented, and if there is tell me, but: It seems that it would be better if the whitespaces inside the generated strings that contain operations and fragments were collapsed and normalized.

For example, currently: Given a .graphql file that contains the following query:

query getCurrentUserInfo {
  currentUser {
    fullName
    cartItemsCount
    purchasedCoursesCount
    specialRole
  }
}

This would be the string version that gets generated by GraphQL Code Generator:

export const GetCurrentUserInfoDocument = gql`
    query getCurrentUserInfo {
  currentUser {
    fullName
    cartItemsCount
    purchasedCoursesCount
    specialRole
  }
}
    `;

However, notice that all those double whitespaces, tabs, and of course line breaks are basically redundant. It could instead generate:

export const GetCurrentUserInfoDocument = gql`query getCurrentUserInfo { currentUser { fullName cartItemsCount purchasedCoursesCount specialRole } }`;

The 2 queries above have absolutely no functional difference, but if the line breaks, tabs, and double whitespaces are preserved, that adds to the bundle size of the app (if you're building a client-side web app) completely unnecessarily. Not only that, but it's also just extra unnecessary bits sent over the network to the server every time when you use these strings to send requests to your intended GraphQL endpoint.

So, why not simply do a .replace(/\s+/g, ' ').trim() on these strings to get rid of all the unnecessary whitespace characters?

aradalvand avatar Feb 25 '21 23:02 aradalvand

Would you create a PR for this?

I think documentMode: documentNode would be better option for client-side app because gql tag runs everytime app is loaded in the client.

ardatan avatar Feb 25 '21 23:02 ardatan

I think documentMode: documentNode would be better option for client-side app because gql tag runs everytime app is loaded in the client.

You're right, but 2 points come to mind:

  1. You could be using a GraphQL client that just uses plain strings instead of the AST that the gql tag generates, and you've set documentMode to string. For instance, graphql-request is an example of such a client.
  2. Even if for some reason you've decided to go with documentMode: graphQLTag, which many people actually do, you again don't need any of the unnecessary characters in the strings that get generated. So, there's really no reason not to do get rid of them.

Would you create a PR for this?

I'll give it a shot.

Thanks for your quick response, by the way.

aradalvand avatar Feb 25 '21 23:02 aradalvand

I'm all it for that, feel free to send a PR! :)

dotansimha avatar Mar 07 '21 15:03 dotansimha

Hi there. Don't mean to hurry anyone, but any updates on this one? The tabs and line-breaks and stuff are totally redundant and should not be in the output. I think this is fairly straightforward to implement for somebody who's familiar with the inner workings of graphql-code-generator, @dotansimha and @ardatan would love to know if you have a couple hours to spend on this. Thank you.

ponderingexistence avatar Jan 19 '22 07:01 ponderingexistence

@dotansimha @ardatan Could you guys implement this? The extra whitespaces in the resulting strings are totally useless.

AaronFlynn1989 avatar Aug 27 '22 21:08 AaronFlynn1989