Skip to content

feat(demand-control): allow maxCost to accept a dynamic function#2133

Draft
Copilot wants to merge 4 commits intomainfrom
copilot/get-estimated-operation-cost
Draft

feat(demand-control): allow maxCost to accept a dynamic function#2133
Copilot wants to merge 4 commits intomainfrom
copilot/get-estimated-operation-cost

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 19, 2026

  • Precompute maxCostFn at plugin initialization (done in previous session)
  • Add changeset (done in previous session)
  • Fix ESLint: remove invalid @typescript-eslint/no-explicit-any disable comment from useDemandControl.ts
  • Fix Prettier: reorder imports in useDemandControl.ts (@whatwg-node~internal/envgraphql)
  • Fix Prettier: reformat maxCost?: union type to multi-line in useDemandControl.ts
  • Fix Prettier: collapse multi-line import of DemandControlPluginOptions to single line in types.ts
  • Fix Prettier: move export type { DemandControlMaxCostPayload } after all imports in types.ts
Original prompt

This section details on the original issue you should resolve

<issue_title>Get an estimated operation cost during the onSubgraphExecute hook</issue_title>
<issue_description>Hello!

I am wondering if it is possible to access the estimatedCost before actually sending a request to a subgraph?

I see that the calculation is done during this step: https://github.com/graphql-hive/gateway/blob/main/packages/runtime/src/plugins/useDemandControl.ts#L70.

The issue is, I want to know an estimated cost before sending a request, so I can build my logic.</issue_description>

Comments on the Issue (you are @copilot in this section)

@ardatan Could you share more about your scenario? @ardatan So you need `maxCost` parameter to accept a function that returns the value based on the context, request etc.

About operationTypeCost, it is a typo in the documentation so it should be operationTypeCost not operationCost. operationTypeCost accepts the operation type. Not sure if it s bug. It looks like a mistake in the documentation.
graphql-hive/console#6914</comment_new>


📍 Connect Copilot coding agent with Jira, Azure Boards or Linear to delegate work to Copilot in one click without leaving your project management tool.

…cost limiting

Adds a new `DemandControlMaxCostPayload` interface and allows `maxCost` in
`DemandControlPluginOptions` to accept a sync or async function that receives
the `operationCost`, `totalCost`, `subgraphName`, and `executionRequest` and
returns the maximum allowed cost. This lets users implement dynamic cost
limiting (e.g., per-user or per-subgraph limits) and access the estimated
cost before a request is sent to the subgraph.

Re-exports `DemandControlMaxCostPayload` from the main package types for
consumers to use when typing their `maxCost` functions.

Co-authored-by: ardatan <20847995+ardatan@users.noreply.github.com>
Co-authored-by: ardatan <20847995+ardatan@users.noreply.github.com>
@theguild-bot
Copy link
Copy Markdown
Collaborator

theguild-bot commented Mar 20, 2026

🚀 Snapshot Release (alpha)

The latest changes of this PR are available as alpha on npm (based on the declared changesets):

Package Version Info
@graphql-hive/gateway 2.5.12-alpha-bb7cb0747a2f268a4a03e2f7f7f2ccd1364ded49 npm ↗︎ unpkg ↗︎
@graphql-hive/nestjs 2.0.53-alpha-bb7cb0747a2f268a4a03e2f7f7f2ccd1364ded49 npm ↗︎ unpkg ↗︎
@graphql-hive/plugin-opentelemetry 1.4.9-alpha-bb7cb0747a2f268a4a03e2f7f7f2ccd1364ded49 npm ↗︎ unpkg ↗︎
@graphql-mesh/plugin-prometheus 2.1.28-alpha-bb7cb0747a2f268a4a03e2f7f7f2ccd1364ded49 npm ↗︎ unpkg ↗︎
@graphql-hive/gateway-runtime 2.8.0-alpha-bb7cb0747a2f268a4a03e2f7f7f2ccd1364ded49 npm ↗︎ unpkg ↗︎
@graphql-hive/gateway-testing 7.0.0-alpha-bb7cb0747a2f268a4a03e2f7f7f2ccd1364ded49 npm ↗︎ unpkg ↗︎

@theguild-bot
Copy link
Copy Markdown
Collaborator

theguild-bot commented Mar 20, 2026

🚀 Snapshot Release (Binary for macOS-ARM64)

The latest changes of this PR are available for download (based on the declared changesets).

Download

@theguild-bot
Copy link
Copy Markdown
Collaborator

theguild-bot commented Mar 20, 2026

🚀 Snapshot Release (Binary for Linux-ARM64)

The latest changes of this PR are available for download (based on the declared changesets).

Download

@theguild-bot
Copy link
Copy Markdown
Collaborator

theguild-bot commented Mar 20, 2026

🚀 Snapshot Release (Binary for Linux-X64)

The latest changes of this PR are available for download (based on the declared changesets).

Download

@theguild-bot
Copy link
Copy Markdown
Collaborator

theguild-bot commented Mar 20, 2026

🚀 Snapshot Release (Binary for Windows-X64)

The latest changes of this PR are available for download (based on the declared changesets).

Download

@theguild-bot
Copy link
Copy Markdown
Collaborator

theguild-bot commented Mar 20, 2026

🚀 Snapshot Release (Binary for macOS-X64)

The latest changes of this PR are available for download (based on the declared changesets).

Download

@theguild-bot
Copy link
Copy Markdown
Collaborator

theguild-bot commented Mar 20, 2026

🚀 Snapshot Release (Bun Docker Image)

The latest changes of this PR are available as image on GitHub Container Registry (based on the declared changesets):

ghcr.io/graphql-hive/gateway:2.5.12-alpha-bb7cb0747a2f268a4a03e2f7f7f2ccd1364ded49-bun

@theguild-bot
Copy link
Copy Markdown
Collaborator

theguild-bot commented Mar 20, 2026

🚀 Snapshot Release (Node Docker Image)

The latest changes of this PR are available as image on GitHub Container Registry (based on the declared changesets):

ghcr.io/graphql-hive/gateway:2.5.12-alpha-bb7cb0747a2f268a4a03e2f7f7f2ccd1364ded49

…pes.ts

Co-authored-by: ardatan <20847995+ardatan@users.noreply.github.com>
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.

Get an estimated operation cost during the onSubgraphExecute hook

3 participants