Skip to content

Instantly share code, notes, and snippets.

@austin43
Created January 2, 2020 03:20
Show Gist options
  • Save austin43/8d3837d8d6938b0dd72e7096d2f9aa39 to your computer and use it in GitHub Desktop.
Save austin43/8d3837d8d6938b0dd72e7096d2f9aa39 to your computer and use it in GitHub Desktop.
Apollo Hasura Firebase
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