Packages

Elixir library and Mix task that scans GraphQL query text in a CSV for selection depth and field count, with limits you set.

Current section

Files

Jump to
Raw

README.md

# graphql_depth_check

Elixir library and Mix task. Give it a CSV of GraphQL query text (columns query_id, query; the text may span lines inside quotes) and it prints, for each query, the operation kind, the depth, the field count and flags against two limits you set.

This is a static text scan. It is 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, so braces inside them are ignored.
- 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 never count as fields (a field that has one of those names is therefore not counted), and neither does an operation or fragment name.
- The operation kind is the first word (query, mutation, subscription or 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 row `invalid: unbalanced braces`; an unclosed or stray round bracket gives `invalid: unbalanced round brackets` and an unterminated string gives `invalid: unterminated string`. An invalid row is counted and shown, it does not stop the run, and it is left out of the depth and field checks.

Flags: `too deep` when the depth is above `--max-depth` and `too wide` when the field count is above `--max-fields`. `--max-depth` (a whole number of 1 or more, default 6) and `--max-fields` (a whole number of 1 or more, default 50) are limits set by you. Queries are listed in file order; the deepest query is the one with the highest depth, ties broken by the lower query_id. Header names are matched trimmed and case-insensitive, extra columns are ignored, and a UTF-8 BOM and CRLF line endings are accepted.

What it does not do: it counts braces and names in text you give it. It does not know any service's real limits, validate a query or call anything.

Exit code 0 means the report was printed (flagged and invalid queries appear in it), 2 means the input could not be used: a missing file, a missing column, no data rows, an empty query or query_id (the message names the file and the data row) or a bad option.

Part of the [OneGTMLab](https://www.onegtmlab.dev) free GTM tools. Background on the idea: [QuickMail](https://www.onegtmlab.dev/tool-directory/sending/quickmail).

## Install

```
{:graphql_depth_check, "~> 1.0"}   # in mix.exs deps
```

## Use

CSV columns: query_id, query. The sample has eight invented queries: nested, multi-line, a fragment, a comment and a string holding braces, one too deep, one too wide, one with unbalanced braces and one mutation with an alias and spreads.

```
$ mix graphql_depth_check sample.csv --max-depth 3 --max-fields 6
query_id  kind      depth  fields  flags
q1        query     3      4
q2        query     3      4
q3        fragment  1      1
q4        query     2      2
q5        query     5      5       too deep
q6        query     1      7       too wide
q7        -         -      -       invalid: unbalanced braces
q8        mutation  3      3

limits: max depth 3, max fields 6
queries: 8
invalid: 1
too deep: 1
too wide: 1
deepest: q5 (depth 5)
```

## Test

```
mix test
```

Offline, no network calls, no dependencies beyond the language runtime. MIT licensed.