Feature request: strip whitespace from GraphQL queries / fragments / mutations / subscriptions
See original GitHub issueCurrently, when issuing requests to a GraphQL API, the GraphQL string describing the query contains a lot of whitespace. While this makes readability in development mode easier, it is wasteful in production.
Would it be possible to add a function to this library that strips this whitespace away? Here’s some example code that does this:
let graphQLQuery = `...` // some GraphQL literal
graphQLQuery = graphQLQuery
.replace(/#.*\n/g, '')
.replace(/[\s|,]*\n+[\s|,]*/g, ' ')
.replace(/:\s/g, ':')
.replace(/,\s/g, ',')
.replace(/\)\s\{/g, '){')
.replace(/\}\s/g, '}')
.replace(/\{\s/g, '{')
.replace(/\s\}/g, '}')
.replace(/\s\{/g, '{')
.replace(/\)\s/g, ')')
.replace(/\(\s/g, '(')
.replace(/\s\)/g, ')')
.replace(/\s\(/g, '(')
.replace(/=\s/g, '=')
.replace(/\s=/g, '=')
.replace(/@\s/g, '@')
.replace(/\s@/g, '@')
.replace(/\s\$/g, '$')
.replace(/\s\./g, '.')
.trim()
This code turns a GraphQL query such as this one:
query SomeQuery($foo: String!, $bar: String) {
someField(foo: $foo, bar: $bar) {
a
b {
c
d
}
}
}
into:
query SomeQuery($foo:String!$bar:String){someField(foo:$foo bar:$bar){a b{c d}}}
Preferably this would happen at compile time for a production build, so the GraphQL literals in the source code would be minified in the output bundle and thus sent in minified form to the GraphQL API.
Proposed name for this function: condense.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:18
- Comments:13 (8 by maintainers)
Top Results From Across the Web
HTTP Link - Apollo GraphQL Docs
HttpLink supports both POST and GET requests, and you can configure HTTP ... can be used with stripIgnoredCharacters to remove whitespace from queries....
Read more >GraphQL specification
A document may contain operations (queries, mutations, and subscriptions) as well as fragments, a common unit of composition allowing for query reuse. A...
Read more >Designing a URL-based query syntax for GraphQL
See how to design a single-line URL-based query syntax for GraphQL servers that is simple to read and write.
Read more >GraphQL Content API - Contentful
The query should be sent as a property in a JSON payload in the body of the POST request with the property name...
Read more >GraphQL API - GitLab Docs
Parts of the schema marked for removal from the GitLab GraphQL API are first ... --request POST --data-binary '{"query": "mutation {createSnippet(input: ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@rybon Personally, I think it’s the right place to implement such functionality since minification is tightly coupled with language grammar so it would make sense to keep it together with lexer and parser.
Plus it would be super simple to implement it on top of Lexer and actual implementation should be pretty small.
I totally agree with @rybon it’s not the responsibility of
graphql-jsto dictate patterns of the client to server communication instead it should provide common functionality as composable building blocks.Moreover, this functionality is useful in a number of other cases like storing introspection dumps as minified SDL or having a hash function for graphql documents that doesn’t affected by formatting changes.
Please keep this issue on the topic of striping ignored characters and stop discussing best practices for client-server communication.
P.S. Update: I’m working on it ATM with good progress and expect to have it to be ready for review in the next couple days.