Bird
Raised Fist0
GraphQLquery~20 mins

Subscription syntax in GraphQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Subscription Syntax Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
What is the output of this GraphQL subscription?

Consider this GraphQL subscription that listens for new messages:

subscription {
  newMessage {
    id
    content
    sender
  }
}

If a new message with id: 101, content: "Hello!", and sender: "Alice" is sent, what will the subscription receive?

GraphQL
subscription {
  newMessage {
    id
    content
    sender
  }
}
A{"data": {"newMessage": {"id": 101, "content": "Hello!", "sender": "Alice"}}}
B{"data": {"newMessage": {"id": "101", "content": "Hello!", "sender": "Alice"}}}
C{"data": {"message": {"id": 101, "content": "Hello!", "sender": "Alice"}}}
D{"error": "Subscription not found"}
Attempts:
2 left
💡 Hint

Check the exact field names in the subscription response.

📝 Syntax
intermediate
2:00remaining
Which subscription syntax is valid in GraphQL?

Identify the valid GraphQL subscription syntax from the options below:

A
subscription {
  newMessage {
    id
    content
  }
  extraField
}
B
subscription NewMessages {
  newMessage {
    id
    content
  }
}
C
subscription newMessage {
  newMessage {
    id
    content
  }
}
D
subscription {
  newMessage(id: 5) {
    id
    content
  }
}
Attempts:
2 left
💡 Hint

Subscriptions can have operation names and must have a single root field.

❓ optimization
advanced
2:00remaining
How to optimize a subscription to receive only necessary data?

You want to subscribe to user status updates but only need the userId and status fields. Which subscription query is optimized?

A
subscription {
  userStatus {
    userId
  }
}
B
subscription {
  userStatus {
    userId
    status
    lastLogin
    email
  }
}
C
subscription {
  userStatus {
    userId
    status
  }
}
D
subscription {
  userStatus {
    *
  }
}
Attempts:
2 left
💡 Hint

Request only the fields you need to reduce data transfer.

🔧 Debug
advanced
2:00remaining
Why does this subscription cause an error?

Given this subscription:

subscription {
  newMessage {
    id
    content
    sender
  }
  userStatus {
    userId
    status
  }
}

Why does it cause an error?

AGraphQL subscriptions must have exactly one root field; multiple root fields cause an error.
BThe subscription is missing an operation name.
CThe fields inside newMessage are invalid.
DSubscriptions cannot request nested fields.
Attempts:
2 left
💡 Hint

Check the number of root fields in the subscription.

🧠 Conceptual
expert
2:00remaining
What is the main difference between a GraphQL subscription and a query?

Choose the best explanation of how subscriptions differ from queries in GraphQL.

AQueries are used for mutations, subscriptions are used for fetching data.
BSubscriptions can only fetch data once, queries keep the connection open for updates.
CSubscriptions and queries are identical except subscriptions require an operation name.
DSubscriptions maintain a continuous connection to receive real-time data updates, while queries fetch data once.
Attempts:
2 left
💡 Hint

Think about how data is delivered over time.

Practice

(1/5)
1. What is the main purpose of a subscription in GraphQL?
easy
A. To delete data from the database
B. To receive real-time updates automatically when data changes
C. To fetch data only once from the server
D. To update data on the server

Solution

  1. Step 1: Understand the role of subscription

    Subscriptions are designed to keep the client updated automatically when data changes, without asking repeatedly.
  2. Step 2: Compare with other operations

    Queries fetch data once, mutations change data, but subscriptions listen for live updates.
  3. Final Answer:

    To receive real-time updates automatically when data changes -> Option B
  4. Quick Check:

    Subscription = live updates [OK]
Hint: Subscriptions push updates; queries pull once [OK]
Common Mistakes:
  • Confusing subscription with query or mutation
  • Thinking subscription fetches data only once
  • Assuming subscription modifies data
2. Which of the following is the correct syntax to start a GraphQL subscription named newMessage requesting id and content fields?
easy
A. query { newMessage { id content } }
B. mutation { newMessage { id content } }
C. subscription { newMessage { id content } }
D. subscribe { newMessage { id content } }

Solution

  1. Step 1: Identify the correct keyword for subscription

    The keyword to start a subscription is subscription, not query, mutation, or subscribe.
  2. Step 2: Check the structure

    The subscription block should contain the event name and requested fields inside curly braces.
  3. Final Answer:

    subscription { newMessage { id content } } -> Option C
  4. Quick Check:

    Keyword subscription + fields = subscription { newMessage { id content } } [OK]
Hint: Use 'subscription' keyword, not 'query' or 'mutation' [OK]
Common Mistakes:
  • Using 'query' or 'mutation' instead of 'subscription'
  • Using 'subscribe' keyword which is invalid
  • Missing curly braces around fields
3. Given the subscription below, what fields will the client receive when a new comment is added?
subscription { newComment { id text author { name } } }
medium
A. id, text, and author name of the new comment
B. Only id and text of the new comment
C. Only author name of the new comment
D. All comments in the database

Solution

  1. Step 1: Analyze the subscription fields

    The subscription requests id, text, and nested author { name } fields for the newComment event.
  2. Step 2: Understand what is sent on event

    When a new comment is added, the client receives exactly these fields for that comment, not all comments.
  3. Final Answer:

    id, text, and author name of the new comment -> Option A
  4. Quick Check:

    Requested fields = id, text, author name [OK]
Hint: Nested fields inside subscription are included in output [OK]
Common Mistakes:
  • Assuming all comments are sent, not just new ones
  • Ignoring nested author name field
  • Thinking only scalar fields are sent
4. Identify the error in this subscription syntax:
subscription newMessage { id content }
medium
A. Missing curly braces around the subscription fields
B. Subscription name cannot be 'newMessage'
C. Subscription keyword should be 'query'
D. Fields 'id' and 'content' should be inside the event name braces

Solution

  1. Step 1: Check subscription structure

    The subscription must specify the event name followed by its fields inside braces, e.g., subscription { newMessage { id content } }.
  2. Step 2: Identify the error in given syntax

    The given syntax places fields directly after subscription name without nesting inside event braces.
  3. Final Answer:

    Fields 'id' and 'content' should be inside the event name braces -> Option D
  4. Quick Check:

    Fields must be nested inside event braces [OK]
Hint: Fields must be inside event braces, not directly after subscription name [OK]
Common Mistakes:
  • Placing fields outside event braces
  • Using 'query' instead of 'subscription'
  • Omitting braces entirely
5. You want to subscribe to live updates of stock prices but only for stocks with price above 100. Which subscription syntax correctly applies this filter?
hard
A. subscription { stockPrice(price_gt: 100) { symbol price } }
B. subscription { stockPrice { symbol price where price > 100 } }
C. subscription { stockPrice { symbol price } filter: { price: { gt: 100 } } }
D. subscription { stockPrice { symbol price } if price > 100 }

Solution

  1. Step 1: Understand argument filtering in subscriptions

    GraphQL subscriptions accept arguments like queries, so filters are passed as arguments inside parentheses after the event name.
  2. Step 2: Identify correct filter syntax

    subscription { stockPrice(price_gt: 100) { symbol price } } uses price_gt: 100 as an argument, which is the standard way to filter for price greater than 100.
  3. Final Answer:

    subscription { stockPrice(price_gt: 100) { symbol price } } -> Option A
  4. Quick Check:

    Filters go as arguments inside parentheses [OK]
Hint: Use arguments in parentheses for filtering subscriptions [OK]
Common Mistakes:
  • Trying to filter inside field braces
  • Using invalid keywords like 'where' or 'if'
  • Placing filters outside subscription block
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