Bird
Raised Fist0
GraphQLquery~10 mins

Field-level cost analysis in GraphQL - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Field-level cost analysis
Start Query
Parse Query Fields
Assign Cost to Each Field
Sum Field Costs
Compare Total Cost to Limit
Yes No
Reject
Return Result
The query is parsed to identify each field, each field is assigned a cost, costs are summed, and the total is checked against a limit to decide execution.
Execution Sample
GraphQL
query {
  user {
    id
    posts {
      title
      comments {
        text
      }
    }
  }
}
This query requests user id, their posts' titles, and comments' text, each field having a cost contributing to total query cost.
Execution Table
StepFieldAssigned CostRunning Total CostAction
1user11Add cost for 'user' field
2user.id12Add cost for 'id' field inside 'user'
3user.posts24Add cost for 'posts' field inside 'user'
4user.posts.title15Add cost for 'title' field inside 'posts'
5user.posts.comments38Add cost for 'comments' field inside 'posts'
6user.posts.comments.text19Add cost for 'text' field inside 'comments'
7Total Cost Check-9Compare total cost 9 to limit 10
8Decision--Cost under limit, query allowed
💡 Total cost 9 is less than limit 10, so query execution proceeds.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6Final
total_cost01245899
Key Moments - 2 Insights
Why does 'user.posts.comments' have a higher cost than 'user.posts.title'?
'user.posts.comments' has cost 3 because nested fields often represent more data or complexity, as shown in execution_table step 5, while 'title' is simpler with cost 1 at step 4.
What happens if total cost exceeds the limit?
As per execution_table step 7 and 8, if total cost was above limit (e.g., >10), the query would be rejected to protect server resources.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the running total cost after processing 'user.posts' field?
A2
B4
C5
D8
💡 Hint
Check the 'Running Total Cost' column at step 3 in execution_table.
At which step does the total cost first exceed 5?
AStep 6
BStep 4
CStep 5
DStep 7
💡 Hint
Look at the 'Running Total Cost' column in execution_table rows for costs above 5.
If the cost of 'user.posts.comments' was reduced from 3 to 1, what would be the final total cost?
A7
B8
C9
D10
💡 Hint
Subtract 2 from the final total cost 9 shown in variable_tracker.
Concept Snapshot
Field-level cost analysis in GraphQL:
- Assign a cost to each field in the query
- Sum all field costs to get total query cost
- Compare total cost to a predefined limit
- Reject query if cost exceeds limit
- Allows controlling resource use per query
Full Transcript
Field-level cost analysis in GraphQL works by parsing the query to identify each requested field. Each field is assigned a cost based on its complexity or data size. These costs are summed to get the total query cost. This total is then compared to a set limit to decide if the query should be executed or rejected. For example, a query requesting user id, posts, and comments accumulates costs step by step. If the total cost is under the limit, the query runs; otherwise, it is rejected to protect server resources.

Practice

(1/5)
1. What is the main purpose of using the @cost directive in GraphQL field-level cost analysis?
easy
A. To rename a field in the schema
B. To define the data type of a field
C. To specify the default value of a field
D. To assign a numeric cost to each field to track resource usage

Solution

  1. 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.
  2. Step 2: Identify the role of the @cost directive

    The @cost directive assigns a numeric complexity cost to each field to estimate query cost.
  3. Final Answer:

    To assign a numeric cost to each field to track resource usage -> Option D
  4. Quick Check:

    @cost assigns cost = A [OK]
Hint: Remember: @cost tracks resource use per field [OK]
Common Mistakes:
  • Confusing cost with data type definition
  • Thinking @cost renames 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
A. books: [Book] @cost(complexity: 5)
B. books: [Book] @cost(5)
C. books: [Book] @cost(complexity=5)
D. books: [Book] @cost { complexity: 5 }

Solution

  1. Step 1: Recall the correct directive syntax

    The @cost directive uses parentheses with named arguments, e.g., @cost(complexity: 5).
  2. 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.
  3. Final Answer:

    books: [Book] @cost(complexity: 5) -> Option A
  4. Quick 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:
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
A. 3
B. 6
C. 8
D. 5

Solution

  1. 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.
  2. Step 2: Add cost of requested fields

    Fields id and name each cost 1, total 2. Add to 6 gives 8.
  3. Final Answer:

    8 -> Option C
  4. Quick 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:
type Query {
  posts: [Post] @cost(complexity: "high")
}

What is the main error here?
medium
A. The complexity value must be an integer, not a string
B. The field name posts is invalid
C. The directive @cost cannot be used on lists
D. The directive syntax is missing parentheses

Solution

  1. Step 1: Check the type of complexity argument

    The complexity argument expects an integer value, but "high" is a string.
  2. Step 2: Verify other parts of the directive usage

    The field name and directive usage are valid; parentheses are present.
  3. Final Answer:

    The complexity value must be an integer, not a string -> Option A
  4. Quick 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
A. 12
B. 8
C. 10
D. 6

Solution

  1. Step 1: Calculate base complexity with multiplier

    Base complexity is 1. Multiplier is argument "limit" with value 4, so 1 * 4 = 4.
  2. Step 2: Add cost of subfields

    Subfields text and author each cost 2, total 4. Add to 4 gives 8.
  3. Step 3: Verify the total

    Subfields are added flatly without further multiplication by list size: 4 (base) + 4 (subfields) = 8.
  4. Final Answer:

    8 -> Option B
  5. Quick 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