GraphqlDepthCheck (graphql_depth_check v1.0.0)

Copy Markdown View Source

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.
  • @directive names 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: b counts once, as b. A fragment spread ...Name counts once. ... on Type { opens a selection set without counting a field. The words query, mutation, subscription, fragment and on, and the operation or fragment name, never count.
  • The operation kind is the first word (query, mutation, subscription, fragment), or query for 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

result()

@type result() :: %{
  id: String.t(),
  kind: String.t(),
  depth: non_neg_integer(),
  fields: non_neg_integer(),
  invalid: nil | String.t()
}

Functions

analyze(queries, opts \\ [])

@spec analyze([{String.t(), String.t()}], keyword()) ::
  {:ok, map()} | {:error, String.t()}

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(text, label \\ "input")

@spec parse(String.t(), String.t()) ::
  {:ok, [{String.t(), String.t()}]} | {:error, String.t()}

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)

@spec parse_csv(String.t()) :: {:ok, [[String.t()]]} | {:error, String.t()}

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(r)

@spec render(map()) :: String.t()

Render the report as text.

scan(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".