0
0
GraphQLquery~20 mins

Subscription syntax in GraphQL - Practice Problems & Coding Challenges

Choose your learning style9 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.