Created
January 2, 2020 03:20
-
-
Save austin43/8d3837d8d6938b0dd72e7096d2f9aa39 to your computer and use it in GitHub Desktop.
Apollo Hasura Firebase
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
const authLink = setContext(async () => { | |
const idToken = await user.getIdToken() | |
const headers = { Authorization: `Bearer ${idToken}` } | |
return { | |
headers, | |
} | |
}) | |
const httpLink = new HttpLink({ | |
uri: process.env.REACT_APP_GRAPHQL_HTTP_URL || '', | |
}) | |
const wsLink = new WebSocketLink({ | |
uri: process.env.REACT_APP_GRAPHQL_WS_URL || '', | |
lazy: true, | |
options: { | |
reconnect: true, | |
connectionParams: async () => { | |
const idToken = await user.getIdToken() | |
const params = { headers: { Authorization: `Bearer ${idToken}` } } | |
return params | |
}, | |
}, | |
}) | |
const link = ApolloLink.from([ | |
split( | |
({ query }) => { | |
const { kind, operation }: any = getMainDefinition(query) | |
return kind === 'OperationDefinition' && operation === 'subscription' | |
}, | |
wsLink, | |
authLink.concat(httpLink) | |
), | |
]) | |
const apolloClient: any = new ApolloClient({ | |
link, | |
cache: new InMemoryCache(), | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment