Static text scan of GraphQL query text: selection depth and field count.
This is a text scan, not a GraphQL parser, and it uses no schema. The rules:
#comments run to the end of the line and are dropped.- String literals (
"..."and block strings"""...""") are skipped. - Text inside round brackets (variable definitions and arguments, with any nested brackets and braces) is skipped.
@directivenames are skipped.- depth is the deepest nesting of selection-set braces; the outermost braces are depth 1.
- fields are the identifiers directly inside a selection set. An alias
a: bcounts once, asb. A fragment spread...Namecounts once.... on Type {opens a selection set without counting a field. The wordsquery,mutation,subscription,fragmentandon, and the operation or fragment name, never count. - The operation kind is the first word (
query,mutation,subscription,fragment), orqueryfor a bare{. - With several operations in one text the depth is the maximum and the fields are summed.
- Unbalanced braces make the query invalid (counted, not fatal).
The CSV has the columns query_id and query. A UTF-8 BOM, CRLF line endings and
quoted cells that span lines are accepted; header names are matched trimmed and
case-insensitive; extra columns are ignored.
Summary
Functions
Scan every query and apply the limits. opts are :max_depth (default 6) and
:max_fields (default 50), both integers of 1 or more. Returns {:ok, report}.
Parse the CSV text into {query_id, query} pairs. label is the file name used in
error messages; rows are numbered from 1 after the header, blank lines skipped.
Parse CSV text into a list of records (lists of strings). Handles a UTF-8 BOM, CRLF, double-quoted fields with commas, doubled quotes and line breaks. Blank lines are dropped.
Render the report as text.
Scan one query text. Returns {:ok, %{kind:, depth:, fields:}} or {:error, reason}
where reason is "unbalanced braces", "unbalanced round brackets" or "unterminated string".
Types
@type result() :: %{ id: String.t(), kind: String.t(), depth: non_neg_integer(), fields: non_neg_integer(), invalid: nil | String.t() }
Functions
Scan every query and apply the limits. opts are :max_depth (default 6) and
:max_fields (default 50), both integers of 1 or more. Returns {:ok, report}.
Parse the CSV text into {query_id, query} pairs. label is the file name used in
error messages; rows are numbered from 1 after the header, blank lines skipped.
Parse CSV text into a list of records (lists of strings). Handles a UTF-8 BOM, CRLF, double-quoted fields with commas, doubled quotes and line breaks. Blank lines are dropped.
Render the report as text.
@spec scan(String.t()) :: {:ok, %{kind: String.t(), depth: non_neg_integer(), fields: non_neg_integer()}} | {:error, String.t()}
Scan one query text. Returns {:ok, %{kind:, depth:, fields:}} or {:error, reason}
where reason is "unbalanced braces", "unbalanced round brackets" or "unterminated string".