-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: move apollo links into separate files
- Loading branch information
alexander.pomazan
committed
Feb 11, 2019
1 parent
e9d041f
commit c59f1e3
Showing
7 changed files
with
110 additions
and
76 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { ApolloLink } from 'apollo-link' | ||
|
||
import { storeTokensInCookie } from '../manage-token' | ||
|
||
export const createAfterwareLink = () => | ||
new ApolloLink((operation, forward) => { | ||
return forward(operation).map((response) => { | ||
const { headers, cache } = operation.getContext() | ||
|
||
// check if tokens were refreshed | ||
if (headers && headers['x-token'] && headers['x-refresh-token']) { | ||
const token = headers['x-token'] | ||
const refreshToken = headers['x-refresh-token'] | ||
|
||
const data = { | ||
authState: { | ||
__typename: 'authState', | ||
token, | ||
refreshToken | ||
} | ||
} | ||
|
||
cache.writeData({ data }) | ||
if (typeof window !== 'undefined') { | ||
// make sure to only do this on client as this | ||
// will try to access window.document | ||
storeTokensInCookie(token, refreshToken) | ||
} | ||
} | ||
|
||
return response | ||
}) | ||
}) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { setContext } from 'apollo-link-context' | ||
|
||
import { getTokens } from '../localState' | ||
|
||
export const createAuthLink = () => | ||
setContext((_, { headers, cache }) => { | ||
const { | ||
authState: { token, refreshToken } | ||
} = cache.readQuery({ | ||
query: getTokens | ||
}) | ||
|
||
return { | ||
headers: { | ||
...headers, | ||
['x-token']: token || '', | ||
['x-refresh-token']: refreshToken || '' | ||
} | ||
} | ||
}) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { withClientState } from 'apollo-link-state' | ||
|
||
import { localStateResolvers } from '../localState' | ||
|
||
export const createStateLink = (cache, token, refreshToken) => | ||
withClientState({ | ||
cache, | ||
defaults: { | ||
authState: { | ||
__typename: 'authState', | ||
token, | ||
refreshToken | ||
} | ||
}, | ||
resolvers: localStateResolvers | ||
}) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import getConfig from 'next/config' | ||
import { WebSocketLink } from 'apollo-link-ws' | ||
|
||
import { extractTokens } from '../manage-token' | ||
|
||
const { | ||
publicRuntimeConfig: { WS_ENDPOINT } | ||
} = getConfig() | ||
|
||
export const createWsLink = () => | ||
process.browser | ||
? new WebSocketLink({ | ||
uri: WS_ENDPOINT, | ||
options: { | ||
reconnect: true, | ||
connectionParams: () => { | ||
const { token, refreshToken } = extractTokens() | ||
return { token, refreshToken } | ||
} | ||
} | ||
}) | ||
: null |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export { createAfterwareLink } from './create-afterware-link' | ||
export { createAuthLink } from './create-auth-link' | ||
export { createWsLink } from './create-ws-link' | ||
export { createStateLink } from './create-state-link' |
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