fix(utils): extract the fields of an inline fragment nested in another
Stacked on #1557.
A nested inline fragment made extract_fields raise
extract_fields (infrahub_sdk/utils.py) read .name on every child of an inline fragment. An inline fragment written inside another inline fragment has no name, so the call raised AttributeError: 'InlineFragmentNode' object has no attribute 'name'. GraphQLQueryAnalyzer.get_fields, calculate_depth and calculate_height raised the same error.
Infrahub calls calculate_depth for its metrics after it runs an operation. On a live stack that ran opsmill/infrahub#11815:
query { ... on Query { ... { BuiltinTag { count } } } }returned HTTP 500, also without credentials.- A mutation written the same way and sent by the admin created its node, and the response was still HTTP 500.
infrahub_sdk/utils.py is identical at daf2fa02, the commit Infrahub stable pins.
An inline fragment now contributes what extract_fields returns for its selection set
- The fields of a nested inline fragment are reported at the level of the enclosing fragment.
- A fragment spread inside an inline fragment is skipped, as it already was outside one. Before, it was reported as a field named after the fragment:
... on TestPerson { id ...Names }gave{"id": None, "Names": None}.
A field selected twice lost sub-fields
Reported by cubic on this pull request. The behavior is older than this pull request and does not need a fragment.
cars { edges { node { id } } } cars { edges { node { name { value } } } }reported onlynameundernode. Both are now reported.cars cars { id }reportedcarswithout sub-fields. It now reportsid.
The merge used dict.update; it now uses deep_merge_dict. The depth and height of such a query can be higher than before. Fixed in its own commit, with 5 test cases in test_extract_fields_merges_a_field_selected_twice, 4 of which fail without it.
Tests
Written first. For the nested inline fragment, all 6 fail without the fix.
test_extract_fields_nested_inline_fragment: 4 cases (at the operation root, below a field, three levels, merged with the same field of the enclosing fragment).test_extract_fields_ignores_fragment_spread_in_inline_fragment.test_depth_and_height_of_nested_inline_fragments: a query has the same fields, depth and height written plainly or inside nested inline fragments.
On the same live stack with python_sdk moved to this branch (60bed4a4):
- The anonymous nested query returns HTTP 200 with its data.
- The admin's nested mutation returns HTTP 200 and creates its node.
- The anonymous nested mutation is still refused with HTTP 401 and creates nothing.
Not run against Infrahub's CI yet.
🤖 Generated with Claude Code