Subgraph definition in GraphQL - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When defining a subgraph in GraphQL, it is important to understand how the size of the data affects the work done.
We want to know how the time to process a subgraph grows as the data grows.
Analyze the time complexity of the following subgraph definition.
type Product @key(fields: "id") {
id: ID!
name: String
reviews: [Review]
}
type Review {
id: ID!
content: String
product: Product
}
This defines a subgraph with products and their reviews linked together.
Look for repeated data fetching or linking steps.
- Primary operation: Fetching each product and then fetching its list of reviews.
- How many times: For each product, the reviews are fetched, so the operation repeats for every product.
As the number of products and reviews grows, the total work grows too.
| Input Size (n products) | Approx. Operations |
|---|---|
| 10 | Fetch 10 products + their reviews |
| 100 | Fetch 100 products + their reviews |
| 1000 | Fetch 1000 products + their reviews |
Pattern observation: The work grows roughly in proportion to the number of products and their reviews.
Time Complexity: O(n + m)
This means the time grows linearly with the number of products (n) plus the number of reviews (m).
[X] Wrong: "Fetching a subgraph is always constant time because it's just one query."
[OK] Correct: Even one query can fetch many items, so the time depends on how many products and reviews are included.
Understanding how subgraph definitions affect data fetching helps you explain performance in real projects.
What if the reviews field was paginated? How would that change the time complexity?
Practice
subgraph in a GraphQL architecture?Solution
Step 1: Understand the concept of subgraphs
Subgraphs are used to divide a big graph into smaller parts for clarity and manageability.Step 2: Identify the main purpose
The main goal is to make data easier to manage and improve collaboration by splitting the graph.Final Answer:
To split a large graph into smaller, manageable parts -> Option AQuick Check:
Subgraphs = smaller parts [OK]
- Thinking subgraphs increase query count
- Confusing subgraphs with database merging
- Believing subgraphs replace schemas
@key directive?Solution
Step 1: Recall the syntax of the @key directive
The @key directive requires the fields argument as a string specifying the unique key fields.Step 2: Match the correct syntax
type Product @key(fields: "id") { id: ID! name: String } correctly uses @key(fields: "id") with quotes around the field name.Final Answer:
type Product @key(fields: "id") { id: ID! name: String } -> Option CQuick Check:
@key(fields: "id") = correct syntax [OK]
- Omitting quotes around field names
- Using @key without 'fields:' keyword
- Passing field name without string quotes
type User @key(fields: "userID") {
userID: ID!
name: String
email: String
}What will happen if you query for
{ user { userID name } } in a federated setup?Solution
Step 1: Understand the @key directive role
The @key directive marks userID as the unique identifier for User entities in the subgraph.Step 2: Analyze the query fields
The query requests userID and name, both defined in the User type, so it will succeed.Final Answer:
The query returns userID and name for the User entity correctly -> Option AQuick Check:
Query fields in schema = successful fetch [OK]
- Assuming all fields must be queried
- Confusing @key with required query fields
- Expecting error if some fields are omitted
type Product @key(fields: "sku") {
sku: ID!
name: String
price: Float
}Which of the following fixes the error in this subgraph definition?
Option A:
type Product @key(fields: "id") {
sku: ID!
name: String
price: Float
}Option B:
type Product @key(fields: sku) {
sku: ID!
name: String
price: Float
}Option C:
type Product @key(fields: "sku") {
sku: ID!
name: String
price: Float
}Option D:
type Product {
sku: ID!
name: String
price: Float
}Solution
Step 1: Check the original @key syntax
The original schema uses @key(fields: "sku") correctly with quotes around the field name.Step 2: Verify field existence
The field sku exists and matches the @key directive, so no error is present.Final Answer:
No change needed; the original schema is correct -> Option BQuick Check:
Correct @key syntax and matching field = no error [OK]
- Removing quotes around fields in @key
- Using a field name not present in the type
- Removing @key directive causing federation errors
orderID and a list of items. Which is the best way to define the subgraph schema to support federation and ensure uniqueness?Solution
Step 1: Identify the unique key for the Order entity
The unique identifier is orderID, so it should be used in the @key directive.Step 2: Check the correct @key usage
Using @key(fields: "orderID") correctly marks orderID as the unique key for federation.Final Answer:
type Order @key(fields: "orderID") { orderID: ID! items: [String] } -> Option DQuick Check:
Unique key = orderID in @key [OK]
- Omitting @key directive causing federation issues
- Using non-unique fields like items in @key
- Combining multiple fields incorrectly in @key
