Created
November 6, 2018 16:54
-
-
Save enriquebrgn/b3d6557f86472c48ae62f82f85e0dc8e to your computer and use it in GitHub Desktop.
Secure subscription stitching
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
import { from } from 'apollo-link' | |
import { HttpLink } from 'apollo-link-http'; | |
import { setContext } from 'apollo-link-context'; | |
import { split } from 'apollo-client-preset' | |
import { hasSubscription } from "@jumpn/utils-graphql"; | |
import SocketLink from './socketLink'; | |
import fetch from 'node-fetch'; | |
import { introspectSchema, makeRemoteExecutableSchema } from 'graphql-tools'; | |
import { GraphQLSchema } from "graphql"; | |
export const getRemoteSchema = async (uri, subUri: string): Promise<GraphQLSchema> => { | |
const httpLink = new HttpLink({ uri, fetch }); | |
const wsLink = new SocketLink(subUri) | |
const contextLink = setContext((_, previousContext) => ({ | |
headers: { | |
authorization: previousContext.graphqlContext.request.headers.authorization | |
} | |
})); | |
const wsContextLink = setContext((_, previousContext) => { | |
wsLink.watchChanges(previousContext.graphqlContext.connection.context.Authorization) | |
}); | |
const link = split( | |
operation => hasSubscription(operation.query), | |
from([wsContextLink, wsLink]), | |
from([contextLink, httpLink]), | |
); | |
const schema = await introspectSchema(httpLink); | |
const executableSchema = makeRemoteExecutableSchema({ | |
schema, | |
link, | |
}); | |
return executableSchema; | |
} |
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
import { ApolloLink, NextLink, Operation } from "apollo-link"; | |
import * as AbsintheSocket from '@absinthe/socket'; | |
import { createAbsintheSocketLink, AbsintheSocketLink } from '@absinthe/socket-apollo-link'; | |
import { Socket as PhoenixSocket } from 'phoenix'; | |
import * as ws from 'ws'; | |
function createPhoenixSocket(uri: string, authToken: string | null): PhoenixSocket { | |
return new PhoenixSocket(uri, { | |
transport: ws, | |
params: { | |
Authorization: authToken, | |
}, | |
}) | |
} | |
function createInnerSocketLink(phoenixSocket: PhoenixSocket): any { | |
const absintheSocket = AbsintheSocket.create(phoenixSocket) | |
return createAbsintheSocketLink(absintheSocket) | |
} | |
class SocketLink extends ApolloLink { | |
socket: PhoenixSocket | |
link: AbsintheSocketLink | |
uri: string | |
token: string | |
constructor(uri: string) { | |
super() | |
this.uri = uri | |
this.token = null | |
this.socket = createPhoenixSocket(this.uri, this.token) | |
this.link = createInnerSocketLink(this.socket) | |
} | |
request(operation: Operation, forward?: NextLink | undefined) { | |
return this.link.request(operation, forward) | |
} | |
watchChanges = (newToken) => { | |
if (newToken !== this.token) { | |
this.token = newToken | |
this.socket.disconnect() | |
this.socket = createPhoenixSocket(this.uri, this.token) | |
this.link = createInnerSocketLink(this.socket) | |
} | |
} | |
} | |
export default SocketLink; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment