fix: enforce NonNegativeInt on GraphQL pagination arguments

ID
3d ago
+405-160

Summary

Adds a NonNegativeInt GraphQL scalar and applies it to the pagination arguments (limit, offset, log_limit, log_offset) of the specialized top-level queries: branch, event, task, search, relationship, account-token, resource-manager and diff-tree. Negative or non-integer pagination values on these queries are now rejected at the schema level with an error that names the offending argument and its location.

The scalar lives in backend/infrahub/graphql/scalars.py (next to FixedGenericScalar).

This supersedes #7610 (originally opened from a personal fork before I joined OpsMill), rebased onto current develop and reworked per the review there.

Context / scope

#7474 (late errors for negative offset/limit) was already fixed and released in v1.9.5 by #9203, which added a resolver-level validate_offset_and_limit() guard in default_paginated_list_resolver — the resolver used by the generic node list queries. That guard covers generic node pagination but not the specialized query resolvers.

This PR closes that gap by validating declaratively at the schema level on exactly those specialized queries. The generic node filters are intentionally left as Int — they are already validated by #9203, and switching them to NonNegativeInt would be a breaking change for every client (frontend, SDK, internal node-id fetch queries) that paginates a node with an Int variable.

Review feedback addressed (from #7610)

  • @ogenstad: raise ValidationError rather than GraphQLError so the response keeps the argument type name and source location. The scalar raises infrahub.exceptions.ValidationError, which graphql-core wraps as Expected value of type 'NonNegativeInt', found -1; Value must be a non-negative integer with locations populated.
  • Docstrings + type hints on the scalar; ValueError/TypeError handled.

Notable fixes beyond the original PR

  • parse_literal accepts the variables argument the executor passes, so a paginated query that also declares query variables is coerced correctly instead of failing argument validation (the original raised a spurious invalid value).
  • parse_value validates strictly: booleans, strings and fractional floats are rejected (whole-number floats accepted, matching the built-in Int), so variable inputs enforce the same integer contract as inline literals.

Changes

  • New NonNegativeInt in backend/infrahub/graphql/scalars.py, with unit tests in backend/tests/unit/graphql/types/test_scalars.py.
  • Int() → NonNegativeInt() on the pagination args of the eight specialized query modules.
  • Regenerated schema/schema.graphql (scalar + the eight fields) and the frontend generated GraphQL files; updated the frontend query documents for the specialized queries and mapped NonNegativeInt → number in gql.tada / graphql-codegen.
  • Component tests for the specialized queries; the branch resolver's now-redundant negative-offset check was removed (the scalar guarantees it).

Test plan

  • uv run invoke format / lint, mypy — clean.
  • Backend unit + affected component tests pass locally against a Neo4j testcontainer; CI green.
  • Frontend codegen + betterer/biome/knip all pass.

Closes #7610.