Bird
Raised Fist0
GraphQLquery~20 mins

Subscription filtering in GraphQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Subscription Filtering Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
Filter subscription events by user ID

Given a GraphQL subscription that listens for new messages, which option correctly filters messages to only those sent by user with ID 42?

GraphQL
subscription OnNewMessage($userId: ID!) {
  newMessage(filter: { senderId: $userId }) {
    id
    content
    senderId
  }
}
A
subscription OnNewMessage {
  newMessage(filter: { senderId: 42 }) {
    id
    content
    senderId
  }
}
B
subscription OnNewMessage($userId: ID!) {
  newMessage(filter: { id: $userId }) {
    id
    content
    senderId
  }
}
C
subscription OnNewMessage($userId: ID!) {
  newMessage(filter: { senderId: $userId }) {
    id
    content
    senderId
  }
}
D
subscription OnNewMessage($userId: ID!) {
  newMessage(filter: { sender: $userId }) {
    id
    content
    senderId
  }
}
Attempts:
2 left
💡 Hint

Check the filter field name matches the message sender's ID field.

🧠 Conceptual
intermediate
1:30remaining
Understanding subscription filter behavior

What happens if a subscription filter does not match any events?

AThe subscription immediately closes with an error.
BThe subscription stays open but no events are sent until a matching event occurs.
CThe subscription sends all events regardless of the filter.
DThe subscription sends a single empty event and then closes.
Attempts:
2 left
💡 Hint

Think about how subscriptions work over time.

📝 Syntax
advanced
1:30remaining
Identify the syntax error in subscription filter

Which option contains a syntax error in the subscription filter?

GraphQL
subscription OnOrderUpdate($status: String!) {
  orderUpdated(filter: { status: $status }) {
    id
    status
  }
}
A
ubscription OnOrderUpdate($status: String!) {
  orderUpdated(filter: { status: $status }) {
    id
    status
  }
}
B
subscription OnOrderUpdate($status: String!) {
  orderUpdated(filter: { status: $status }) {
    id
    status
  }
}
C
}
}  
sutats    
di    
{ )} sutats$ :sutats { :retlif(detadpUredro  
{ )!gnirtS :sutats$(etadpUredrOnO noitpircsbus
D
subscription OnOrderUpdate($status: String!) {
  orderUpdated(filter: { status $status }) {
    id
    status
  }
}
Attempts:
2 left
💡 Hint

Look carefully at the colon usage in the filter object.

❓ optimization
advanced
2:30remaining
Optimize subscription filter for multiple conditions

You want to subscribe to new comments that are either from user ID 10 or have the tag 'urgent'. Which filter is the most efficient and correct?

Afilter: { OR: [{ authorId: 10 }, { tags_contains: "urgent" }] }
Bfilter: { authorId: 10, tags_contains: "urgent" }
Cfilter: { AND: [{ authorId: 10 }, { tags_contains: "urgent" }] }
Dfilter: { authorId: 10 OR tags_contains: "urgent" }
Attempts:
2 left
💡 Hint

Think about how to combine conditions to match either one or the other.

🔧 Debug
expert
3:00remaining
Debug why subscription filter does not receive expected events

A subscription is set up to receive updates for products with price greater than 100. The filter is written as filter: { price_gt: 100 }. However, no events are received even when products with price 150 are updated. What is the most likely cause?

AThe subscription server does not support the 'price_gt' filter operator.
BThe filter should use 'price > 100' syntax instead of 'price_gt'.
CThe subscription query is missing the variable declaration for price.
DThe filter field name 'price_gt' is incorrect; it should be 'priceGreaterThan'.
Attempts:
2 left
💡 Hint

Check the filter operators supported by the GraphQL server.

Practice

(1/5)
1. What is the main purpose of subscription filtering in GraphQL subscriptions?
easy
A. To batch multiple queries into one request
B. To store data permanently in the database
C. To receive only relevant real-time updates based on specified conditions
D. To convert queries into mutations automatically

Solution

  1. Step 1: Understand subscription filtering concept

    Subscription filtering allows clients to specify conditions so they only get updates they care about.
  2. Step 2: Compare options with concept

    Only To receive only relevant real-time updates based on specified conditions describes receiving relevant real-time updates, which matches subscription filtering.
  3. Final Answer:

    To receive only relevant real-time updates based on specified conditions -> Option C
  4. Quick Check:

    Subscription filtering = relevant updates [OK]
Hint: Filtering means getting only updates you want [OK]
Common Mistakes:
  • Confusing filtering with data storage
  • Thinking filtering batches queries
  • Assuming filtering changes query types
2. Which of the following is the correct syntax to add a filter argument to a GraphQL subscription named newMessages that filters by userId?
easy
A. subscription { newMessages(userId: "123") { id content } }
B. subscription { newMessages(filter: { userId: "123" }) { id content } }
C. subscription { newMessages(where: userId = "123") { id content } }
D. subscription { newMessages(user_id: "123") { id content } }

Solution

  1. Step 1: Identify correct filter argument syntax

    GraphQL subscriptions commonly use a filter argument with an object specifying conditions.
  2. Step 2: Match options to syntax

    subscription { newMessages(filter: { userId: "123" }) { id content } } uses filter: { userId: "123" }, which is the correct way to pass filter conditions.
  3. Final Answer:

    subscription { newMessages(filter: { userId: "123" }) { id content } } -> Option B
  4. Quick Check:

    Filter argument uses object syntax = subscription { newMessages(filter: { userId: "123" }) { id content } } [OK]
Hint: Use filter: { field: value } for subscription filters [OK]
Common Mistakes:
  • Using direct argument instead of filter object
  • Incorrect filter keyword like where
  • Wrong field name casing or underscores
3. Given this subscription query:
subscription { newOrders(filter: { status: "SHIPPED" }) { id status } }

Which orders will the client receive updates for?
medium
A. All orders regardless of status
B. No orders, because filter syntax is invalid
C. Only orders with status "PENDING"
D. Only orders with status "SHIPPED"

Solution

  1. Step 1: Analyze the filter condition

    The filter specifies status: "SHIPPED", so only orders with this status qualify.
  2. Step 2: Determine which updates are sent

    The subscription sends updates only for orders matching the filter, so only "SHIPPED" orders.
  3. Final Answer:

    Only orders with status "SHIPPED" -> Option D
  4. Quick Check:

    Filter status = "SHIPPED" means only shipped orders [OK]
Hint: Filter limits updates to matching status only [OK]
Common Mistakes:
  • Ignoring filter and expecting all updates
  • Confusing status values
  • Assuming filter syntax is wrong without checking
4. Consider this subscription query:
subscription { newComments(filter: { postId: 42 }) { id content } }

It returns no updates even when new comments are added to post 42. What is the likely issue?
medium
A. The filter argument is missing the correct type or schema support
B. Subscription queries cannot have filters
C. The filter field name should be post_id instead of postId
D. The subscription name newComments is invalid

Solution

  1. Step 1: Check filter usage in subscription

    Filters require the schema to support filtering on the specified field and type.
  2. Step 2: Identify why no updates appear

    If the schema does not support filtering by postId, the filter silently blocks updates.
  3. Final Answer:

    The filter argument is missing the correct type or schema support -> Option A
  4. Quick Check:

    Schema must support filter fields for updates [OK]
Hint: Check schema supports filter fields used [OK]
Common Mistakes:
  • Assuming field name casing is always wrong
  • Believing filters are not allowed in subscriptions
  • Thinking subscription name causes no updates
5. You want to subscribe to real-time updates of tasks but only for tasks assigned to user ID 7 and with priority "HIGH". Which subscription filter is correct?
hard
A. subscription { tasks(filter: { assignedUserId: 7, priority: "HIGH" }) { id title } }
B. subscription { tasks(filter: { assignedUserId: "7", priority: HIGH }) { id title } }
C. subscription { tasks(filter: { assignedUser: 7, priority: "HIGH" }) { id title } }
D. subscription { tasks(filter: { userId: 7, priority: "HIGH" }) { id title } }

Solution

  1. Step 1: Match filter fields to requirements

    The filter must use assignedUserId for user ID 7 and priority as "HIGH" string.
  2. Step 2: Check option correctness

    subscription { tasks(filter: { assignedUserId: 7, priority: "HIGH" }) { id title } } correctly uses numeric 7 and string "HIGH" with proper field names.
  3. Final Answer:

    subscription { tasks(filter: { assignedUserId: 7, priority: "HIGH" }) { id title } } -> Option A
  4. Quick Check:

    Correct fields and types in filter = subscription { tasks(filter: { assignedUserId: 7, priority: "HIGH" }) { id title } } [OK]
Hint: Use exact field names and correct value types in filter [OK]
Common Mistakes:
  • Using wrong field names like userId instead of assignedUserId
  • Mixing string and number types incorrectly
  • Omitting quotes around string values
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard