# `GraphqlDepthCheck`
[🔗](https://github.com/jaizendra-netizen/graphql_depth_check/blob/main/lib/graphql_depth_check.ex#L1)

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.

# `result`

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

# `analyze`

```elixir
@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`

```elixir
@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`

```elixir
@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`

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

Render the report as text.

# `scan`

```elixir
@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"`.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
