Field-level cost analysis helps you understand how much work each part of a GraphQL query takes. This helps keep your server fast and avoid overload.
Field-level cost analysis in GraphQL
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
GraphQL
type Query {
fieldName: FieldType @cost(complexity: Int, multipliers: [String])
}The @cost directive is used to assign a cost to a field.
complexity is the base cost number for the field.
Examples
books field to 5.GraphQL
type Query {
books: [Book] @cost(complexity: 5)
}id argument, multiplying the base cost.GraphQL
type Query {
author(id: ID!): Author @cost(complexity: 2, multipliers: ["id"])
}Sample Program
This schema assigns costs to users and posts fields. The posts cost multiplies by the userId argument.
The sample query fetches users and posts for user 123.
GraphQL
type Query {
users: [User] @cost(complexity: 3)
posts(userId: ID!): [Post] @cost(complexity: 2, multipliers: ["userId"])
}
# Sample query
{
users {
id
name
}
posts(userId: "123") {
title
}
}Important Notes
Costs help prevent very expensive queries from slowing down your server.
Multipliers let you adjust cost based on input values, like list sizes.
Always test your cost settings to balance protection and usability.
Summary
Field-level cost analysis assigns a cost number to each GraphQL field.
This helps track and limit query resource use.
Use @cost directive with complexity and optional multipliers.
Practice
1. What is the main purpose of using the
@cost directive in GraphQL field-level cost analysis?easy
Solution
Step 1: Understand the purpose of field-level cost analysis
Field-level cost analysis helps monitor and limit resource use by assigning costs to fields.Step 2: Identify the role of the
The@costdirective@costdirective assigns a numeric complexity cost to each field to estimate query cost.Final Answer:
To assign a numeric cost to each field to track resource usage -> Option DQuick Check:
@costassigns cost = A [OK]
Hint: Remember:
@cost tracks resource use per field [OK]Common Mistakes:
- Confusing cost with data type definition
- Thinking
@costrenames fields - Assuming it sets default values
2. Which of the following is the correct syntax to add a cost directive with complexity 5 to a GraphQL field named
books?easy
Solution
Step 1: Recall the correct directive syntax
The@costdirective uses parentheses with named arguments, e.g.,@cost(complexity: 5).Step 2: Check each option's syntax
books: [Book] @cost(complexity: 5)uses correct syntax with named argument and colon. The other three options use incorrect syntax forms.Final Answer:
books: [Book] @cost(complexity: 5) -> Option AQuick Check:
Correct directive syntax = B [OK]
Hint: Use parentheses and colon for directive args: @cost(complexity: 5) [OK]
Common Mistakes:
- Using equal sign instead of colon
- Omitting parentheses
- Using braces instead of parentheses
3. Given the schema snippet:
And the query:
What is the total cost of this query assuming
type Query {
users: [User] @cost(complexity: 2, multipliers: ["first"])
}
input UserFilter {
first: Int
}
And the query:
{ users(first: 3) { id name } }What is the total cost of this query assuming
id and name fields have cost 1 each?medium
Solution
Step 1: Calculate base complexity and multipliers
Base complexity is 2. The multiplier is the argument "first" with value 3, so multiply 2 * 3 = 6.Step 2: Add cost of requested fields
Fieldsidandnameeach cost 1, total 2. Add to 6 gives 8.Final Answer:
8 -> Option CQuick Check:
2 * 3 + 1 + 1 = 8 [OK]
Hint: Multiply complexity by argument, then add field costs [OK]
Common Mistakes:
- Ignoring multipliers
- Not adding field costs
- Multiplying fields cost instead of adding
4. Consider this incorrect directive usage:
What is the main error here?
type Query {
posts: [Post] @cost(complexity: "high")
}What is the main error here?
medium
Solution
Step 1: Check the type of complexity argument
Thecomplexityargument expects an integer value, but "high" is a string.Step 2: Verify other parts of the directive usage
The field name and directive usage are valid; parentheses are present.Final Answer:
The complexity value must be an integer, not a string -> Option AQuick Check:
Complexity expects integer = D [OK]
Hint: Complexity must be a number, not text [OK]
Common Mistakes:
- Using string instead of integer for complexity
- Thinking directive can't be on lists
- Missing parentheses in directive
5. You have a GraphQL field
comments with @cost(complexity: 1, multipliers: ["limit"]). The query requests comments(limit: 4) with subfields text and author, each costing 2. What is the total cost of this query?hard
Solution
Step 1: Calculate base complexity with multiplier
Base complexity is 1. Multiplier is argument "limit" with value 4, so 1 * 4 = 4.Step 2: Add cost of subfields
Subfieldstextandauthoreach cost 2, total 4. Add to 4 gives 8.Step 3: Verify the total
Subfields are added flatly without further multiplication by list size: 4 (base) + 4 (subfields) = 8.Final Answer:
8 -> Option BQuick Check:
1*4 + (2+2) = 8 [OK]
Hint: Add base cost times multiplier plus subfields cost [OK]
Common Mistakes:
- Multiplying subfields cost by multiplier (e.g., getting 20)
- Adding costs without multiplier
- Confusing which costs to multiply
