6

In the same vein as Regex to Validate JSON, for reasons of pure interest and novelty, I am looking for a regex to validate that a string containing a GraphQL-query is syntactically valid.

I know there are loads of libraries that will allow me to validate GraphQL in different languages, but I am specifically looking for a solution using Regular Expressions.

Have anyone cracked the case of a regular expression that can determine whether a string has valid GraphQL query syntax - as per the latest spec from spec.graphql.org?

Tobias Roland
  • 1,182
  • 1
  • 13
  • 35

1 Answers1

-1

This is not possible. With the arbitrary nesting, GraphQL query syntax is not a regular language and cannot be matched with regular expressions.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Mm, yes, you are strictly speaking correct, although I think that recursive regex parsing is possible in quite a few languages - JSON is not a regular language either, but there [are ways to overcome the limitations when the regex parser allows recursive regular expressions](https://stackoverflow.com/a/3845829/2366976) – Tobias Roland Feb 28 '22 at 09:59
  • If you are working with a non-regular regex engine, please state so in the question. Also, if you take that JSON regex as a starting point, what are you stuck on trying this yourself? – Bergi Feb 28 '22 at 10:06
  • @TobiasRoland that json regex validator doesn't seem safe to me: https://3v4l.org/DpiAd – hanshenrik Mar 11 '22 at 22:02