feat: use ApolloFactory for metadata client (#4608)

Co-authored-by: Charles Bochet <charles@twenty.com>
This commit is contained in:
Jérémy M
2024-03-25 19:15:46 +01:00
committed by GitHub
parent d2b237ebf2
commit d1ab063000
2 changed files with 8 additions and 22 deletions

View File

@ -11,9 +11,9 @@ import { useIsMatchingLocation } from '~/hooks/useIsMatchingLocation';
import { useUpdateEffect } from '~/hooks/useUpdateEffect'; import { useUpdateEffect } from '~/hooks/useUpdateEffect';
import { isDefined } from '~/utils/isDefined'; import { isDefined } from '~/utils/isDefined';
import { ApolloFactory } from '../services/apollo.factory'; import { ApolloFactory, Options } from '../services/apollo.factory';
export const useApolloFactory = () => { export const useApolloFactory = (options: Partial<Options<any>> = {}) => {
// eslint-disable-next-line @nx/workspace-no-state-useref // eslint-disable-next-line @nx/workspace-no-state-useref
const apolloRef = useRef<ApolloFactory<NormalizedCacheObject> | null>(null); const apolloRef = useRef<ApolloFactory<NormalizedCacheObject> | null>(null);
const [isDebugMode] = useRecoilState(isDebugModeState); const [isDebugMode] = useRecoilState(isDebugModeState);
@ -50,6 +50,8 @@ export const useApolloFactory = () => {
}, },
extraLinks: [], extraLinks: [],
isDebugMode, isDebugMode,
// Override options
...options,
}); });
return apolloRef.current.getClient(); return apolloRef.current.getClient();

View File

@ -1,9 +1,4 @@
import { useMemo } from 'react'; import { useApolloFactory } from '@/apollo/hooks/useApolloFactory';
import { ApolloClient, InMemoryCache } from '@apollo/client';
import { isNonEmptyString } from '@sniptt/guards';
import { useRecoilState } from 'recoil';
import { tokenPairState } from '@/auth/states/tokenPairState';
import { REACT_APP_SERVER_BASE_URL } from '~/config'; import { REACT_APP_SERVER_BASE_URL } from '~/config';
import { ApolloMetadataClientContext } from '../context/ApolloClientMetadataContext'; import { ApolloMetadataClientContext } from '../context/ApolloClientMetadataContext';
@ -13,20 +8,9 @@ export const ApolloMetadataClientProvider = ({
}: { }: {
children: React.ReactNode; children: React.ReactNode;
}) => { }) => {
const [tokenPair] = useRecoilState(tokenPairState); const apolloMetadataClient = useApolloFactory({
const apolloMetadataClient = useMemo(() => { uri: `${REACT_APP_SERVER_BASE_URL}/metadata`,
if (isNonEmptyString(tokenPair?.accessToken.token)) { });
return new ApolloClient({
uri: `${REACT_APP_SERVER_BASE_URL}/metadata`,
cache: new InMemoryCache(),
headers: {
Authorization: `Bearer ${tokenPair.accessToken.token}`,
},
});
} else {
return null;
}
}, [tokenPair]);
return ( return (
<ApolloMetadataClientContext.Provider value={apolloMetadataClient}> <ApolloMetadataClientContext.Provider value={apolloMetadataClient}>