Skip to content

Instantly share code, notes, and snippets.

@rybon
Last active August 25, 2022 19:48
Show Gist options
  • Save rybon/7cafd12a635fd3a2bf720cefe9292f6c to your computer and use it in GitHub Desktop.
Save rybon/7cafd12a635fd3a2bf720cefe9292f6c to your computer and use it in GitHub Desktop.
Remove unnecessary whitespace from a GraphQL query / mutation / subscription
const compressGraphqlDocument = graphqlDocument =>
graphqlDocument
.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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment