perf(graphql): skip peer hydration for id-only single relationships
Requested by Damien Garros · Slack thread
Big diff
The diff is big b/c I moved some relationship-read graphql tests out of the huge test_graphql_query.py and into their own test_graphql_read_rel_query.py. only the final test in test_graphql_read_rel_query.py is new, the rest are just moved.
Why
GraphQL queries frequently ask a cardinality-one relationship for nothing more than the peer's id (for example owner { node { id } }). Reports that walk many such relationships — cable-plan reports being the motivating case — spend a meaningful share of their time on this pattern.
Before: even when a single-relationship field requested only the peer's id, the resolver hydrated a full peer node through the node data loader (an extra database round-trip's worth of work) and then serialized just the id out of it. The information needed to answer the query was already sitting on the parent record.
After: when a cardinality-one relationship requests only the peer's id — no relationship properties, no metadata, no other node fields, no __typename — the resolver returns the peer id that is already loaded on the parent and skips node hydration entirely. Every other query shape (properties, metadata, hfid, display_label, additional fields, etc.) is untouched and still flows through the existing loader / query_peers paths. In local profiling of cable-plan reports this shortcut produced a ~20–25% speedup.
What changed
backend/infrahub/graphql/resolvers/single_relationship.py: added an early return inSingleRelationshipResolver.resolvethat fires only when the requested node fields are exactly{"id"}and there are no property or metadata fields. It reads the already-loaded peer id fromparent[node_rel.name][0]["node"]["id"], returns{"node": {"id": peer_id}, "properties": {}}, and registers the peer id inrelated_node_ids— mirroring exactly what the normal path does viato_graphql. If the peer id isn't present on the parent it returns the empty response, identical to the loader path's miss behavior.- The guard is deliberately conservative: any additional requested field makes the check fall through to the unchanged code path, so there is no behavioral change for non-
id-only queries.
How to review
The change is a single early-return block. The key correctness points:
set(node_fields) == {"id"}excludes__typename,hfid,display_label, and any other field (they would appear as extra keys), so those keep using the full path.to_graphqlalways adds the serialized node's id torelated_node_ids; the shortcut reproduces that sorelated_node_idsis populated identically.__typename, when requested, is resolved by graphql-core from the schema type rather than the returned data dict, and in any case its presence innode_fieldsmakes the guard fall through — so the minimal{"id": peer_id}return cannot drop a requested__typename.
How to test
A new component test was added:
backend/tests/component/graphql/test_graphql_query.py::test_single_relationship_id_only_uses_preloaded_peer_id. It monkeypatches NodeDataLoader.load to fail if called, runs a TestDog { ... owner { node { id } } } query, and asserts the data is correct and that related_node_ids contains both the dog and the owner ids — proving the peer id is served without hydrating a node.
Note on verification in this environment: the component test suite requires a Neo4j test container, and pulling the Neo4j image is blocked by the sandbox's egress policy here, so the DB-backed test was NOT executed in this environment. What was run:
$ uv run ruff format --check backend/infrahub/graphql/resolvers/single_relationship.py backend/tests/component/graphql/test_graphql_query.py
1 file already formatted # resolver
1 file reformatted # test file (formatting applied, now clean)
$ uv run ruff check backend/infrahub/graphql/resolvers/single_relationship.py backend/tests/component/graphql/test_graphql_query.py
All checks passed!
$ uv run mypy backend/infrahub/graphql/resolvers/single_relationship.py
Success: no issues found in 1 source file
$ uv run pytest tests/component/graphql/test_graphql_query.py --collect-only -q
# collected successfully, including test_single_relationship_id_only_uses_preloaded_peer_id
The new test needs to be run in an environment with Neo4j available (uv run pytest backend/tests/component/graphql/test_graphql_query.py::test_single_relationship_id_only_uses_preloaded_peer_id) before merge.
Impact & rollout
- Backward compatibility: No API or schema changes. Only affects the internal resolution of
id-only cardinality-one relationship selections; all other query shapes are unchanged. - Performance: ~20–25% faster cable-plan report generation in local profiling; avoids one peer-node hydration per
id-only single-relationship field. - Config/env changes: None.
- Deployment notes: Safe to deploy.
Checklist
- Tests added/updated
- Changelog entry added (
changelog/+single-relationship-id-only-shortcut.changed.md) - External docs updated (not user-facing)
- Internal .md docs updated (not needed)
- I have reviewed AI generated content