0
0
GraphQLquery~20 mins

Field-level cost analysis in GraphQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Field-level Cost Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Calculate total cost for selected fields

Given a GraphQL query requesting name and price fields of products, and a cost model where name costs 1 unit and price costs 3 units per item, what is the total cost for 5 products?

GraphQL
query {
  products {
    name
    price
  }
}
A20
B15
C10
D25
Attempts:
2 left
💡 Hint

Multiply the cost per field by the number of products and sum.

🧠 Conceptual
intermediate
2:00remaining
Understanding cost impact of nested fields

In a GraphQL query, a user has a nested posts field. If user fields cost 2 units each and posts fields cost 1 unit each, which factor most increases total cost?

AIncreasing number of users queried
BIncreasing number of <code>user</code> fields requested
CIncreasing number of posts per user
DIncreasing number of <code>posts</code> fields requested
Attempts:
2 left
💡 Hint

Think about how nested lists multiply cost.

📝 Syntax
advanced
2:00remaining
Identify syntax error in cost directive usage

Which option contains a syntax error in applying a cost directive to a GraphQL field?

GraphQL
type Query {
  products: [Product] @cost(multipliers: ["first"])
}

type Product {
  name: String @cost(value: 1)
  price: Float @cost(value: 2)
}
AUsing @cost(value=1) on <code>price</code> field
BUsing @cost(multipliers: ["first"]) on <code>products</code> field
CUsing @cost(value: 1) on <code>name</code> field
DUsing @cost(value: 2) on <code>price</code> field
Attempts:
2 left
💡 Hint

Check the syntax for directive arguments in GraphQL.

optimization
advanced
2:00remaining
Optimize query cost by reducing field requests

You have a query requesting id, name, description, and price fields for 10 products. Costs per field are: id = 1, name = 2, description = 5, price = 3. Which option reduces total cost the most while keeping id and price?

ARemove <code>name</code> only
BRemove <code>name</code> and <code>description</code>
CRemove <code>price</code> only
DRemove <code>description</code> only
Attempts:
2 left
💡 Hint

Calculate cost savings for each removal.

🔧 Debug
expert
3:00remaining
Diagnose unexpected high cost in nested query

A query requests users with nested posts and comments. Each user has 3 posts, each post has 4 comments. Costs: user fields = 2 units each, post fields = 1 unit each, comment fields = 0.5 units each. The query requests 2 fields per user, 3 fields per post, and 2 fields per comment. Why is the total cost unexpectedly high?

AThe query requests too many user fields
BThe number of users queried is higher than expected
CThe cost per comment field is set too low
DThe cost calculation multiplies fields by number of comments incorrectly
Attempts:
2 left
💡 Hint

Consider how nested lists multiply total cost.