Last active
March 26, 2021 03:53
-
-
Save shaykav/d75c20e3e9b31e44189f110db8f7e7b3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (eventType === 'MESSAGE') { | |
const message = JSON.parse(event.body); | |
// Handshake with Apollo | |
if (message.type === 'connection_init') { | |
// Write connection to persistence | |
const connection = Object.assign(new Connection(), { | |
id: connectionId, | |
requestContext: event.requestContext, | |
payload: event.body.payload | |
}); | |
await mapper.put(connection); | |
// Acknowledges connection | |
await gateway | |
.postToConnection({ | |
ConnectionId: connectionId, | |
Data: JSON.stringify({ type: 'connection_ack', payload: {} }) | |
}) | |
.promise(); | |
return { | |
statusCode: 200, | |
body: '' | |
}; | |
} | |
// Subscription start event | |
if (message.type === 'subscribe') { | |
const connection = await mapper.get(Object.assign(new Connection(), { | |
id: connectionId | |
})); | |
// Write subscription to persistence | |
const sub = Object.assign(new Subscription(), { | |
id: `${connectionId}|${message.id}`, | |
connectionId, | |
topic: 'NEW_MESSAGE', | |
requestContext: event.requestContext, | |
connectionRef: connection, | |
subscription: { | |
...message.payload | |
} | |
}); | |
await mapper.put(sub); | |
return { | |
statusCode: 200, | |
body: '' | |
}; | |
} | |
// Subscription stop event | |
if (message.type === 'complete') { | |
// Remove subscription from persistence | |
const sub = assign(new Subscription(), { | |
id: `${connectionId}|${message.id}` | |
}); | |
await mapper.delete(sub); | |
return { | |
statusCode: 200, | |
body: '', | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment