0
0
GraphQLquery~10 mins

WebSocket transport in GraphQL - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to start a WebSocket connection for GraphQL subscriptions.

GraphQL
const wsClient = new WebSocket('[1]');
Drag options to blanks, or click blank then click option'
Aftp://localhost:4000/graphql
Bhttp://localhost:4000/graphql
Cfile://localhost:4000/graphql
Dws://localhost:4000/graphql
Attempts:
3 left
💡 Hint
Common Mistakes
Using http:// instead of ws://
Using unsupported protocols like ftp:// or file://
2fill in blank
medium

Complete the code to send a GraphQL subscription start message over WebSocket.

GraphQL
wsClient.send(JSON.stringify({ type: '[1]', id: '1', payload: { query: SUBSCRIPTION_QUERY } }));
Drag options to blanks, or click blank then click option'
Astart
Bquery
Cstop
Dconnection_init
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'query' instead of 'start' for subscription messages
Using 'connection_init' which is for connection setup, not starting subscriptions
3fill in blank
hard

Fix the error in the WebSocket message handler to correctly parse incoming data.

GraphQL
wsClient.onmessage = event => { const data = JSON.parse(event.[1]); console.log(data); };
Drag options to blanks, or click blank then click option'
Amessage
Bdata
Ctext
Dpayload
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to parse event.message or event.payload which do not exist
Not parsing the string before using it
4fill in blank
hard

Fill both blanks to correctly handle a GraphQL subscription stop message and close the WebSocket.

GraphQL
if (message.type === '[1]') { wsClient.[2](); }
Drag options to blanks, or click blank then click option'
Astop
Bstart
Cclose
DcloseConnection
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'start' instead of 'stop' to end subscription
Calling a non-existent method like 'closeConnection()'
5fill in blank
hard

Fill all three blanks to initialize a WebSocket connection with connection_init message and send it.

GraphQL
const initMessage = { type: '[1]', payload: { headers: { Authorization: '[2]' } } }; wsClient.[3](JSON.stringify(initMessage));
Drag options to blanks, or click blank then click option'
Aconnection_init
BBearer token123
Csend
Dstart
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'start' instead of 'connection_init' for initialization
Forgetting to use 'send' method to send the message