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:

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 free GTM tools. Background on the idea: 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.