Created
July 21, 2022 12:24
-
-
Save arjan/6c639be99bba0f3aeec8f24ac646ddf3 to your computer and use it in GitHub Desktop.
Replacement for @absinthe/socket-apollo-link package
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
// Use at your own risk! No support for mutations currently. | |
const createSocketLink = (socket: Socket) => { | |
const channel = socket.channel('__absinthe__:control') | |
channel.join().receive('error', () => console.error('Failed to join Absinthe channel!')) | |
return new ApolloLink(operation => { | |
return new Observable(observer => { | |
const query = print(operation.query) | |
const { variables } = operation | |
channel | |
.push('doc', { query, variables }) | |
.receive('ok', data => { | |
if (!observer.closed) { | |
observer.next(data) | |
observer.complete() | |
} | |
}) | |
.receive('error', response => { | |
const error = new Error('AbsintheError') as any | |
if (Array.isArray(response.errors)) { | |
error.message = response.errors.map((e: any) => e.message).join('; ') | |
} | |
error.response = response | |
observer.error(error) | |
}) | |
}) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment