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.
I suggest doing it in 3 steps:
filter
temporarily;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:
filter
;filter
;filter
.Make the replacements in the "before" and "after" strings. Join the parts at the end.
The regex for splitting might look like:
Rebuild the final string with something as:
removeSpaces()
actually replaces\s+
with\n
), then replace+
withI am not very familiar with your programming language, so I cannot provide the exact code, but you should be able to get the idea.