question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature request: strip whitespace from GraphQL queries / fragments / mutations / subscriptions

See original GitHub issue

Currently, 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:closed
  • Created 5 years ago
  • Reactions:18
  • Comments:13 (8 by maintainers)

github_iconTop GitHub Comments

7reactions
IvanGoncharovcommented, Sep 14, 2018

Maybe I am asking this question in the wrong repository though. If so, where should I ask it instead?

@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.

5reactions
IvanGoncharovcommented, Feb 26, 2019

I consider it to be a trivial and obvious feature request, even though the implementation is not.

I totally agree with @rybon it’s not the responsibility of graphql-js to 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found