Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multiple Apollo clients issue #319

Merged
merged 2 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,13 @@ export class MemberDataApiImpl
}

async getMemberByIdWithCommunity(memberId: string): Promise<MemberData> {
const result = await this.model.findById(memberId).populate('community').exec();
return result;
return await this.model.findById(memberId).populate('community').exec();
}
async getMemberByIdWithCommunityAccountRole(memberId: string): Promise<MemberData> {
const result = await this.model.findById(memberId).populate('community').populate('accounts.user').populate('role').exec();
return result;
return await this.model.findById(memberId).populate('community').populate('accounts.user').populate('role').exec();
}
async getMemberById(memberId: string): Promise<MemberData> {
let result = await (await this.findOneById(memberId)).populate('role');
return result;
return await this.model.findById(memberId).populate('role');
}

async getMembersByUserExternalId(userExternalId: string): Promise<MemberData[]> {
Expand Down
61 changes: 33 additions & 28 deletions ui-community/src/components/shared/apollo-connection.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FC } from 'react';
import { FC, useEffect } from 'react';

import { ApolloClient, ApolloLink, ApolloProvider, HttpLink, InMemoryCache, from } from '@apollo/client';

Expand All @@ -7,11 +7,39 @@ import { setContext } from '@apollo/client/link/context';
import { useAuth } from 'react-oidc-context';
import { useParams } from 'react-router-dom';

const countryLink = new HttpLink({
uri: 'https://countries.trevorblades.com/'
});


const httpLink = new BatchHttpLink({
uri: `${import.meta.env.VITE_FUNCTION_ENDPOINT}`,
batchMax: 15, // No more than 15 operations per batch
batchInterval: 50 // Wait no more than 50ms after first batched operation
});

const client = new ApolloClient({
cache: new InMemoryCache(),
connectToDevTools: import.meta.env.NODE_ENV !== 'production'
});

const ApolloConnection: FC<any> = (props) => {
mgupta83 marked this conversation as resolved.
Show resolved Hide resolved
const auth = useAuth();
const params = useParams(); // useParams.memberId won't work here because ApolloConnection wraps the Routes, not inside a Route

const withToken = setContext(async (_, { headers }) => {
mgupta83 marked this conversation as resolved.
Show resolved Hide resolved
return getAuthHeaders(headers);

});

const getAuthHeaders = async (headers: any) => {
if(auth.isAuthenticated !== true) {
return {
headers: {
...headers
}
};
}
const access_token = auth.user?.access_token;
console.log('auth-token', access_token);
const returnHeaders = { ...headers };
Expand All @@ -31,37 +59,14 @@ const ApolloConnection: FC<any> = (props) => {
return { headers: returnHeaders };
};

const withToken = setContext(async (_, { headers }) => {
if (auth.isAuthenticated) {
return getAuthHeaders(headers);
} else {
return {
headers: {
...headers
}
};
}
});

const httpLink = new BatchHttpLink({
uri: `${import.meta.env.VITE_FUNCTION_ENDPOINT}`,
batchMax: 15, // No more than 15 operations per batch
batchInterval: 50 // Wait no more than 50ms after first batched operation
});

const countryLink = new HttpLink({
uri: 'https://countries.trevorblades.com/'
});

const client = new ApolloClient({
link: ApolloLink.split(
useEffect(() => {
mgupta83 marked this conversation as resolved.
Show resolved Hide resolved
client.setLink(ApolloLink.split(
(operation) => operation.getContext().clientName === 'country',
countryLink,
from([withToken, httpLink])
),
cache: new InMemoryCache(),
connectToDevTools: import.meta.env.NODE_ENV !== 'production'
});
));
},[auth]);

return <ApolloProvider client={client}>{props.children}</ApolloProvider>;
};
Expand Down