Skip to main content

Cost decoration

A cost decoration weights one field or type of the GraphQL schema this service fronts.

In the console Cost decorationsBelongs to a serviceEngine resource graphql_cost_decoration

What a cost decoration is​

A GraphQL cost decoration weights one field or type of the GraphQL schema a service fronts. graphql-limit-count reads the weights on the routes of that service under its complexity and node_quantifier strategies, and the default depth strategy never does. The console writes a decoration as JSON under its service.

field_path names the position in the schema, for example Query.products. add_value is the field's own cost, and mul_arguments names the query arguments whose values multiply the cost of what is selected under it.

When to use it​

  • A product list whose cost grows with the page size a client asks for, the argument first of Query.products.
  • A type that is expensive to resolve wherever a query reaches it, weighted once on the type, for example Product.
  • Lists nested inside lists, priced by how many times the inner list is fetched under node_quantifier.
  • Partners on a GraphQL orders API whose budgets should follow what their queries cost the backend rather than how many they send.

Not for the budget a query's price is charged to — see graphql-limit-count (Limits GraphQL requests by their query cost).

Set it up​

Charge a product list by the page size it asks for​

Read by the graphql-limit-count plugin on the routes of this service under its complexity or node_quantifier strategy. Under complexity, first multiplies the cost of everything selected under products. Under node_quantifier it multiplies only what is nested under products, never the product list's own size.

graphql_cost_decoration · as the console saves it
{
"name": "product lists cost their page size",
"field_path": "Query.products",
"add_value": 1,
"mul_arguments": [
"first"
]
}

Things to know​

  • The engine assigns the id when the object is created; it is not the name, and it is what other objects refer to.
  • With Query.products at an own cost of 1 and multiplied by first, a query for 50 products under complexity costs 53. The answer carries the price in X-Graphql-Query-Cost.
  • The same query for 500 products is answered 403 on a route whose cost ceiling is 100.
  • Under node_quantifier, a page size multiplies only what is nested under the list, never the list's own size. 5000 products with nothing listed under them cost 2.
  • The plugin reads the schema from its Introspection endpoint, or, when that is unset, from the route's target.
  • A decoration without field_path is refused when you save.
  • A path with an empty step, Query..products, is refused when you save.
  • Once a decoration is deleted, queries are priced without it from the next request on.
  • AdviceWeight first the lists your backend pays most for, and keep the weights on the service whose routes share that schema.

Edited as Raw JSON​

A cost decoration weights one position of the GraphQL schema a service fronts, and only the graphql-limit-count plugin reads it; the console writes it as JSON under the service, and the engine checks it at save.

G8 dev · Apache APISIX® 3.19.0
Apache, Apache APISIX and APISIX are trademarks of The Apache Software Foundation.
Facts
In the console
Cost decorations
Belongs to
a service
Engine resource
graphql_cost_decoration