Refactor client config (#529)

* Refactor client config

* Fix server tests

* Fix lint
This commit is contained in:
Charles Bochet
2023-07-07 11:10:42 -07:00
committed by GitHub
parent 11d18cc269
commit 26b033abc9
38 changed files with 386 additions and 180 deletions

View File

@ -8,6 +8,7 @@ import { useRecoilState } from 'recoil';
import { isMockModeState } from '@/auth/states/isMockModeState';
import { tokenPairState } from '@/auth/states/tokenPairState';
import { isDebugModeState } from '@/client-config/states/isDebugModeState';
import { CommentThreadTarget } from '~/generated/graphql';
import { mockedCompaniesData } from '~/testing/mock-data/companies';
import { mockedUsersData } from '~/testing/mock-data/users';
@ -16,6 +17,7 @@ import { ApolloFactory } from '../services/apollo.factory';
export function useApolloFactory() {
const apolloRef = useRef<ApolloFactory<NormalizedCacheObject> | null>(null);
const [isDebugMode] = useRecoilState(isDebugModeState);
const [tokenPair, setTokenPair] = useRecoilState(tokenPairState);
const [isMockMode] = useRecoilState(isMockModeState);
@ -64,10 +66,11 @@ export function useApolloFactory() {
setTokenPair(null);
},
extraLinks: isMockMode ? [mockLink] : [],
isDebugMode,
});
return apolloRef.current.getClient();
}, [isMockMode, setTokenPair]);
}, [isMockMode, setTokenPair, isDebugMode]);
useEffect(() => {
if (apolloRef.current) {

View File

@ -29,6 +29,7 @@ export interface Options<TCacheShape> extends ApolloClientOptions<TCacheShape> {
onTokenPairChange?: (tokenPair: AuthTokenPair) => void;
onUnauthenticatedError?: () => void;
extraLinks?: ApolloLink[];
isDebugMode?: boolean;
}
export class ApolloFactory<TCacheShape> implements ApolloManager<TCacheShape> {
@ -43,6 +44,7 @@ export class ApolloFactory<TCacheShape> implements ApolloManager<TCacheShape> {
onTokenPairChange,
onUnauthenticatedError,
extraLinks,
isDebugMode,
...options
} = opts;
@ -98,7 +100,7 @@ export class ApolloFactory<TCacheShape> implements ApolloManager<TCacheShape> {
return forward(operation);
}
default:
if (process.env.NODE_ENV === 'development') {
if (isDebugMode) {
console.warn(
`[GraphQL error]: Message: ${
graphQLError.message
@ -114,7 +116,7 @@ export class ApolloFactory<TCacheShape> implements ApolloManager<TCacheShape> {
}
if (networkError) {
if (process.env.NODE_ENV === 'development') {
if (isDebugMode) {
console.warn(`[Network error]: ${networkError}`);
}
onNetworkError?.(networkError);
@ -127,8 +129,7 @@ export class ApolloFactory<TCacheShape> implements ApolloManager<TCacheShape> {
errorLink,
authLink,
...(extraLinks ? extraLinks : []),
// Only show logger in dev mode
process.env.NODE_ENV !== 'production' ? logger : null,
isDebugMode ? logger : null,
retryLink,
httpLink,
].filter(assertNotNull),