Allow to have collapse whitespaces in query strings when documentMode: string is set
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?
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.
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:
- You could be using a GraphQL client that just uses plain strings instead of the AST that the
gqltag generates, and you've setdocumentModetostring. For instance, graphql-request is an example of such a client. - 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.
I'm all it for that, feel free to send a PR! :)
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.
@dotansimha @ardatan Could you guys implement this? The extra whitespaces in the resulting strings are totally useless.