0
0
GraphQLquery~20 mins

WebSocket transport in GraphQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
WebSocket Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
WebSocket subscription query output

Consider a GraphQL subscription over WebSocket that listens for new messages in a chat room. The subscription query is:

subscription { newMessage { id content sender } }

When a new message with id: 101, content: "Hello!", and sender: "Alice" arrives, what is the expected JSON output sent over the WebSocket?

A{"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{"data": {"newMessage": {"id": "101", "content": "Hello!", "sender": "Alice"}}}
Attempts:
2 left
💡 Hint

GraphQL responses wrap the result inside a data field matching the subscription field name.

📝 Syntax
intermediate
2:00remaining
Identify invalid WebSocket GraphQL subscription message

Which of the following WebSocket messages is not a valid GraphQL subscription start message according to the Apollo protocol?

A{"id": "1", "type": "start", "payload": {"query": "subscription { newMessage { id content } }", "variables": {}}}
B{"id": "1", "type": "start", "payload": {"query": "subscription { newMessage { id content } }"}}
C{"id": 1, "type": "start", "payload": {"query": "subscription { newMessage { id content } }"}}
D{"id": "1", "type": "start", "payload": {"query": "subscription { newMessage { id content } }", "operationName": "NewMsg"}}
Attempts:
2 left
💡 Hint

The id field must be a string, not a number.

optimization
advanced
2:00remaining
Optimizing WebSocket subscription message size

You want to reduce the size of WebSocket messages for a GraphQL subscription that sends frequent updates. Which approach will most effectively reduce message size without losing data?

ASend only changed fields in the subscription payload instead of the full object.
BCompress the WebSocket messages using gzip before sending.
CUse shorter field names in the GraphQL schema and queries.
DBatch multiple subscription updates into a single WebSocket message.
Attempts:
2 left
💡 Hint

Think about sending less data per message rather than just compressing or batching.

🔧 Debug
advanced
2:00remaining
Debugging WebSocket subscription connection failure

A client tries to start a GraphQL subscription over WebSocket but immediately receives a Connection error and the subscription does not start. The server logs show no errors. Which is the most likely cause?

AThe client did not send the <code>connection_init</code> message before <code>start</code>.
BThe server does not support subscriptions.
CThe WebSocket URL is missing the <code>ws://</code> or <code>wss://</code> scheme.
DThe subscription query has a syntax error.
Attempts:
2 left
💡 Hint

Check the required handshake messages in the WebSocket protocol.

🧠 Conceptual
expert
2:00remaining
Understanding WebSocket transport advantages for GraphQL

Why is WebSocket transport preferred over HTTP polling for real-time GraphQL subscriptions?

AWebSocket automatically retries failed queries without client intervention.
BWebSocket allows sending multiple queries in a single HTTP request.
CWebSocket encrypts data by default, unlike HTTP polling.
DWebSocket maintains a persistent connection, reducing overhead and latency for real-time updates.
Attempts:
2 left
💡 Hint

Consider connection persistence and efficiency for real-time data.