fix(analyzer): report an operation wrapped in a top-level fragment

PO
5h ago
+159-5

An operation wrapped in a top-level fragment was not counted

GraphQLQueryAnalyzer.operations kept only the FieldNode children of an operation's top-level selection set, so any other selection ended the walk.

mutation B { ...Hidden }
fragment Hidden on Mutation { TestPersonCreate(data: {...}) { ok } }

OperationDefinition (mutation B)
└── SelectionSet
    └── FragmentSpread "Hidden"   before ──╳  not a FieldNode, dropped
         ╎                        after  ──►  resolved against the document's fragments
         └── fragment Hidden on Mutation
             └── Field "TestPersonCreate"

                     before    after
  operations         []        [mutation TestPersonCreate]
  contains_mutation  False     True

... on Mutation { X } and the type-condition-less ... { X } were dropped the same way.

A caller of the SDK analyzer therefore read contains_mutation == False for a document whose selected operation is a mutation. Infrahub's permission pipeline does not read it: it runs on the backend subclass InfrahubGraphQLQueryAnalyzer, fixed and released in Infrahub 1.12.4 / 1.11.14 / 1.10.12 (GHSA-299f-9wgh-7jhv).

The walk now descends into inline fragments and fragment spreads

infrahub_sdk/analyzer.py gains _iter_operation_fields, which yields a selection set's field nodes and recurses into InlineFragmentNode and FragmentSpreadNode, resolving a spread against the document's fragment definitions. It replicates backend/infrahub/graphql/analyzer.py on opsmill/infrahub stable. contains_mutation is untouched and still reads operations.

One visited_fragments set per operation expands each fragment once:

  cycle                            diamond
  mutation { ...Cycle }            mutation { ...A ...B }
  fragment Cycle { ...Cycle }      fragment A { ...Shared }
                                   fragment B { ...Shared }
                                   fragment Shared { TestPersonCreate }

  Cycle ──► Cycle ──╳ seen         A ──► Shared ──► TestPersonCreate
            terminates             B ──► Shared ──╳ seen, counted once

Tests

Each case carries the whole list operations must report, in document order, and asserts contains_mutation is True. Pinning the list rather than testing for membership also fails a case that gains or loses an unrelated operation.

Mutation wrapped inmutation { … }query A { __typename } mutation B { … }
... on Mutation { }✓✓
... { }, no type condition✓✓
named fragment spread✓✓
nested fragment spread✓✓

The cases come from backend/tests/component/graphql/hidden_mutation_cases.py, plus the single-operation nested spread that list does not carry.

Three guards go with them:

  • A self-referential fragment terminates, leaving operations empty.
  • A diamond fragment contributes its field once.
  • An ordinary field-only query keeps contains_mutation False.

All 9 new assertions fail on stable without the fix.

Left as is

  • operation_names is defined on the backend subclass, not in the SDK. It reads self.operations, so the backend fix already made it correct, and this pull request adds no such property to the SDK.
  • extract_fields (infrahub_sdk/utils.py:275) handles FieldNode and InlineFragmentNode but not FragmentSpreadNode, so get_fields, calculate_depth and calculate_height return {}, 1 and 0 for a document whose fields sit in a named fragment. It feeds neither operations nor contains_mutation, and resolving a spread there means passing the document's fragment definitions into the function, which changes a public signature.
  • nbr_queries counts every definition, so a fragment definition inflates it: one operation plus one fragment reports 2.

Follow-up

Once the python_sdk submodule pointer moves, InfrahubGraphQLQueryAnalyzer.operations and _iter_operation_fields duplicate the base class and can be removed from opsmill/infrahub. That bump is a separate change.

🤖 Generated with Claude Code