website/docs/guides/document-comparison.mdx
import DocsRating from '@site/src/core/DocsRating';
:::note This feature is experimental and the API may change in future versions. :::
The Relay compiler includes an experimental feature for comparing the intermediate representations (IR) of two GraphQL documents. This feature determines whether one document is a subset of another and reports any missing selections.
This feature is particularly useful for comparing documents generated by LLMs where exact match of LLM generated documents and static expected documents is neither attainable or desirable, due to the nature of LLMs. In those cases, you could:
--left) an expected document with minimally required selections, and determine if the LLM generated document (via --right) contains all required selections, via the subset test that the command performs.--right and LLM generated document via --left to determine whether LLM generated documents contains extra selections than expected, aka. overfetching.> relay experimental-compare-document-ir --help
EXPERIMENTAL! Compare intermediate representations (IR) of documents passed via left and right, outputs selections that exists in left but not in right.
Usage: relay experimental-compare-document-ir [OPTIONS] --left <LEFT> --right <RIGHT>
Options:
--left <LEFT> Document in string, must contain exactly 1 operation
--right <RIGHT> Document in string, must contain exactly 1 operation
--schemaPaths <SCHEMA_PATHS>... Path(s) to the full schema file(s) to convert documents to intermediate representation (IR) for comparison
-h, --help Print help
Detailed information about missing selection(s) from the right document, including line number and path.
The 2 documents (left and right) are first transformed to their Intermediate Representation (IR), then to Normalized Tree form, for comparison. Each node of the tree corresponds to a selection in the original document, and is checked for its existence in the other tree.
Each IR is transformed into a normalized tree structure that:
my_id: id → id)The normalized tree mirrors the JSON response structure of the query.
Consists of
... on A { id }, the type condition of selection id is A if A is a concrete type, or a set of types that are A if A is an abstract type.A selection exists in a tree if:
user(id: $id) is a superset of user(id: 100) because variable $id can be set to arbitrary values, including 100, at runtime.Left document:
query A {
viewer {
timezone_estimate {
display_name
gmt_offset
}
}
}
Right document:
query A {
viewer {
timezone_estimate {
display_name
}
}
}
Output
[INFO] Found 1 missing selection(s):
4 │ display_name
5 │ gmt_offset
│ ^^^^^^^^^^
6 │ }
* query -> viewer -> timezone_estimate -> gmt_offset
Explore tests for more examples.