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 { 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
const apolloRef = useRef<ApolloFactory<NormalizedCacheObject> | null>(null);
const [isDebugMode] = useRecoilState(isDebugModeState);
@ -50,6 +50,8 @@ export const useApolloFactory = () => {
},
extraLinks: [],
isDebugMode,
// Override options
...options,
});
return apolloRef.current.getClient();

View File

@ -1,9 +1,4 @@
import { useMemo } from 'react';
import { ApolloClient, InMemoryCache } from '@apollo/client';
import { isNonEmptyString } from '@sniptt/guards';
import { useRecoilState } from 'recoil';
import { tokenPairState } from '@/auth/states/tokenPairState';
import { useApolloFactory } from '@/apollo/hooks/useApolloFactory';
import { REACT_APP_SERVER_BASE_URL } from '~/config';
import { ApolloMetadataClientContext } from '../context/ApolloClientMetadataContext';
@ -13,20 +8,9 @@ export const ApolloMetadataClientProvider = ({
}: {
children: React.ReactNode;
}) => {
const [tokenPair] = useRecoilState(tokenPairState);
const apolloMetadataClient = useMemo(() => {
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]);
const apolloMetadataClient = useApolloFactory({
uri: `${REACT_APP_SERVER_BASE_URL}/metadata`,
});
return (
<ApolloMetadataClientContext.Provider value={apolloMetadataClient}>