Removing whitespaces and newline characters from GraphQL based on condition

2.9k views Asked by At

I am trying to remove extra whitespaces and newline characters from my GraphQL query but the data between 2 double quotes in filter argument should remain intact.

Here's how the query is received on our fastly's CDN

# input
{"query":"query OpName    {\n   itemCollection         (filter: { text: "aa aa     aa", text2: "aa             aa"}){\n    group         {      slug\n\n\n\n            text text2  } }   }"}

# expected output
{"query":"query OpName { itemCollection (filter: { text: "aa aa     aa", text2: "aa             aa"}){ group { slug text text2 } } }"}

The objective is to

  • Remove extra whitespaces from the query
  • The whitespaces between 2 double quotes, should remain intact inside the graphql query (since the filter argument's value will be used to match records in our database)

We have tried the following:

  • \s+(?=(?:['|%22](?:\\['|%22]|[^'|%22])+['|%22]|[^'|%22])+$) given at fastly docs
  • \s+(?=([^"]*"[^"]*")*[^"]*$)

But it doesn't seem to work.

1

There are 1 answers

5
virolino On

I suggest doing it in 3 steps:

  • save the filter temporarily;
  • make the replacements (delete the unwanted spaces);
  • add back the filter.

The required regexes become significantly simpler (and I guess faster, as you do not need look-aheads or other tricks).


Alternative:

Split the string in 3 parts:

  • before filter;
  • filter;
  • after filter.

Make the replacements in the "before" and "after" strings. Join the parts at the end.


The regex for splitting might look like:

(.*?)(filter: {[^}]*})(.*)

Rebuild the final string with something as:

removeSpaces(group1) + group2 + removeSpaces(group3)

removeSpaces() actually replaces \s+ with . If you want to keep new lines (\n), then replace + with .

I am not very familiar with your programming language, so I cannot provide the exact code, but you should be able to get the idea.