Skip to content

feat: support costs on arguments of directives#1465

Merged
ysmolski merged 11 commits intomasterfrom
yury/eng-8700-router-support-cost-on-the-arguments-of-directives
Apr 27, 2026
Merged

feat: support costs on arguments of directives#1465
ysmolski merged 11 commits intomasterfrom
yury/eng-8700-router-support-cost-on-the-arguments-of-directives

Conversation

@ysmolski
Copy link
Copy Markdown
Contributor

@ysmolski ysmolski commented Apr 8, 2026

I have opted for the simple base layer without recursion.
Directives could have input objects passed to arguments,
where fields could have costs.

We do not handle such recursion here.
I settled on the arguments level of directives.

I have opted for the simple base layer without recursion.
Directives could have input objects passed to arguments,
where fields could have costs. We do not handle such recursion here.
I settled on the arguments level of directives.
Copy link
Copy Markdown

@claude claude Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This repository is configured for manual code reviews. Comment @claude review to trigger a review and subscribe this PR to future pushes, or @claude review once for a one-time review.

Tip: disable this comment in your organization's Code Review settings.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 8, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 6ee7154e-3e0d-48df-a667-52bc61670e7a

📥 Commits

Reviewing files that changed from the base of the PR and between 1fef69b and e988eed.

📒 Files selected for processing (1)
  • v2/pkg/engine/plan/cost.go

📝 Walkthrough

Walkthrough

Renames FieldWeight→FieldCost, adds per-directive-argument weights, propagates FieldCost through cost plumbing, computes directive-argument weights (including max-union across implementations when parent returns abstract types), and updates tests and introspection/schema fixtures to exercise directive-argument cost behavior. (50 words)

Changes

Cohort / File(s) Summary
Cost core
v2/pkg/engine/plan/cost.go
Renamed FieldWeightFieldCost, added DirectiveArgumentWeights map[string]int, switched DataSourceCostConfig.Weights and related APIs to *FieldCost, added max/union logic for implementing types' directive-argument weights, and included directive-argument weights in node cost computation (renamed directiveCostdirectivesCost).
Cost visitor / comments
v2/pkg/engine/plan/cost_visitor.go
Comment wording tweaks: clarify CostTreeNode is "partially filled" and update "Attach to parent" wording; no behavior change.
Execution tests (cost-focused)
execution/engine/execution_engine_cost_test.go
Expanded Star Wars test graph (added Query.search and Starship type) and added test cases validating directive-argument weight inclusion/exclusion, implementation-type directive-arg weight fallback, and null-argument handling; updated tests to use *plan.FieldCost.
Execution tests (other)
execution/engine/execution_engine_test.go
Replaced *plan.FieldWeight with *plan.FieldCost in test cost config literals; no logic change.
Schema / Introspection fixtures
v2/pkg/starwars/testdata/star_wars.graphql, execution/engine/testdata/full_introspection*.json
Added directive @approx(tolerance: Int = 1) on FIELD_DEFINITION; applied @approx (with default and explicit tolerance: null) to fields in Star Wars test schema; added corresponding approx entries to JSON introspection fixtures.
Test data expansion
execution/engine/execution_engine_cost_test.go
Expanded test graph: new root field Query.search and new type Starship { name, length } used by cost tests.

Sequence Diagram

sequenceDiagram
    participant Schema as Schema (Field Def)
    participant Visitor as Cost Visitor
    participant TreeNode as Cost TreeNode
    participant Calculator as Cost Calculator

    Schema->>Visitor: Encounter field with directives (e.g. `@approx`)
    Visitor->>Visitor: Resolve directive args (provided, default, or null)
    Visitor->>TreeNode: Create/attach node with FieldCost and directive-arg keys
    TreeNode->>Calculator: Request costs & multiplier for node
    Calculator->>Calculator: If parent returns abstract type → gather implementing fields
    Calculator->>Calculator: Compute max/union of DirectiveArgumentWeights across impls
    Calculator->>Calculator: Sum field cost + directive-argument costs + args cost → total
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately reflects the main feature being added: support for costs on directive arguments. It is concise and clearly describes the primary change.
Description check ✅ Passed The description is related to the changeset, explaining the design decision to implement a simple base-layer solution without recursion into nested input objects, which aligns with the cost computation changes shown in the diffs.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch yury/eng-8700-router-support-cost-on-the-arguments-of-directives

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
v2/pkg/engine/plan/cost.go (1)

138-142: Keep the default cost config constructor in sync with the new map field.

NewDataSourceCostConfig() still leaves DirectiveArguments nil. Reads are fine, but incremental builders now need a separate make before their first assignment. Initializing it in the constructor keeps this section consistent with the other map-backed config blocks.

♻️ Suggested change
 func NewDataSourceCostConfig() *DataSourceCostConfig {
 	return &DataSourceCostConfig{
-		Weights:   make(map[FieldCoordinate]*FieldWeight),
-		ListSizes: make(map[FieldCoordinate]*FieldListSize),
-		Types:     make(map[string]int),
+		Weights:            make(map[FieldCoordinate]*FieldWeight),
+		ListSizes:          make(map[FieldCoordinate]*FieldListSize),
+		Types:              make(map[string]int),
+		DirectiveArguments: make(map[DirectiveArgCoords]int),
 	}
 }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@v2/pkg/engine/plan/cost.go` around lines 138 - 142, The
NewDataSourceCostConfig constructor currently leaves the DirectiveArguments map
nil; update NewDataSourceCostConfig to initialize DirectiveArguments
(map[DirectiveArgCoords]int) to an empty map so callers can assign into it
without a separate make, keeping it consistent with other map-backed config
fields like FieldCosts and ArgumentCosts; ensure the map is created when the
DataSourceCostConfig struct is constructed.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@v2/pkg/engine/plan/cost_visitor.go`:
- Around line 69-70: The code only calls
extractFieldDirectives(fieldDefinitionRef) and thus misses directives present on
implementing fields; update the logic (around where directiveArgs :=
v.extractFieldDirectives(fieldDefinitionRef)) to also resolve and merge
directive args from all concrete implementing field definitions (e.g., iterate
the type/interface implementations for the abstract field and call
extractFieldDirectives on each implementing field), then apply the existing
upper-bound strategy used for abstract-field handling to compute and use the
max/merged directive values; apply the same change for the other occurrences
noted (the block covering the logic analogous to lines 270-319).

---

Nitpick comments:
In `@v2/pkg/engine/plan/cost.go`:
- Around line 138-142: The NewDataSourceCostConfig constructor currently leaves
the DirectiveArguments map nil; update NewDataSourceCostConfig to initialize
DirectiveArguments (map[DirectiveArgCoords]int) to an empty map so callers can
assign into it without a separate make, keeping it consistent with other
map-backed config fields like FieldCosts and ArgumentCosts; ensure the map is
created when the DataSourceCostConfig struct is constructed.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: eba32a71-439b-4c84-8b80-2fcab5a7e2b7

📥 Commits

Reviewing files that changed from the base of the PR and between 7c59ee0 and 13b86cc.

📒 Files selected for processing (4)
  • execution/engine/execution_engine_cost_test.go
  • v2/pkg/engine/plan/cost.go
  • v2/pkg/engine/plan/cost_visitor.go
  • v2/pkg/starwars/testdata/star_wars.graphql

Comment thread v2/pkg/engine/plan/cost_visitor.go Outdated
@ysmolski ysmolski marked this pull request as draft April 9, 2026 12:14
@ysmolski ysmolski marked this pull request as ready for review April 10, 2026 13:06
Copy link
Copy Markdown

@claude claude Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This repository is configured for manual code reviews. Comment @claude review to trigger a review and subscribe this PR to future pushes, or @claude review once for a one-time review.

Tip: disable this comment in your organization's Code Review settings.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@v2/pkg/engine/plan/cost.go`:
- Around line 500-510: The code currently adds all configured
DirectiveArgumentWeights regardless of whether a directive/argument was actually
present; update the data model and summation so only applied/coerced directive
arguments are charged: add a field on CostTreeNode (e.g., AppliedDirectiveArgs
or AppliedDirectives map keyed by directive+arg) that is populated during
parsing/coercion, then change the loops in cost.go that call
parent.maxDirectiveArgumentWeightsImplementingFields(...) and iterate
fieldWeight.DirectiveArgumentWeights to filter by node.AppliedDirectiveArgs (or
have maxDirectiveArgumentWeightsImplementingFields accept the node or its
applied set) so you only accumulate weights for directives/arguments present
after coercion/defaulting for node.fieldCoords.FieldName and fieldWeight.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: ec696c39-ee40-4e64-a042-8779c761bd63

📥 Commits

Reviewing files that changed from the base of the PR and between 13b86cc and 865f50c.

📒 Files selected for processing (8)
  • execution/engine/execution_engine_cost_test.go
  • execution/engine/execution_engine_test.go
  • execution/engine/testdata/full_introspection.json
  • execution/engine/testdata/full_introspection_with_deprecated.json
  • execution/engine/testdata/full_introspection_with_typenames.json
  • v2/pkg/engine/plan/cost.go
  • v2/pkg/engine/plan/cost_visitor.go
  • v2/pkg/starwars/testdata/star_wars.graphql
✅ Files skipped from review due to trivial changes (4)
  • execution/engine/testdata/full_introspection_with_typenames.json
  • execution/engine/testdata/full_introspection_with_deprecated.json
  • execution/engine/testdata/full_introspection.json
  • v2/pkg/engine/plan/cost_visitor.go
🚧 Files skipped from review as they are similar to previous changes (2)
  • v2/pkg/starwars/testdata/star_wars.graphql
  • execution/engine/execution_engine_cost_test.go

Comment thread v2/pkg/engine/plan/cost.go
@ysmolski ysmolski merged commit 2eca1ab into master Apr 27, 2026
13 of 18 checks passed
@ysmolski ysmolski deleted the yury/eng-8700-router-support-cost-on-the-arguments-of-directives branch April 27, 2026 09:33
ysmolski pushed a commit that referenced this pull request Apr 27, 2026
🤖 I have created a release *beep* *boop*
---


##
[2.0.0](v2.0.0-rc.270...v2.0.0)
(2026-04-27)


### Features

* support costs on arguments of directives
([#1465](#1465))
([2eca1ab](2eca1ab))


### Bug Fixes

* grpc datasource stack overflows on recursive input
([#1466](#1466))
([eba0f58](eba0f58))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).
ysmolski pushed a commit that referenced this pull request Apr 29, 2026
🤖 I have created a release *beep* *boop*
---


##
[1.12.0](execution/v1.11.0...execution/v1.12.0)
(2026-04-28)


### Features

* support costs on arguments of directives
([#1465](#1465))
([2eca1ab](2eca1ab))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants