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

After createOneDbConnection mutation, update cache manually instead of using refetchQuery #5684

Merged
merged 2 commits into from
May 31, 2024
Merged
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
@@ -1,5 +1,4 @@
import { ApolloClient, useMutation } from '@apollo/client';
import { getOperationName } from '@apollo/client/utilities';

import { CREATE_ONE_DATABASE_CONNECTION } from '@/databases/graphql/mutations/createOneDatabaseConnection';
import { GET_MANY_DATABASE_CONNECTIONS } from '@/databases/graphql/queries/findManyDatabaseConnections';
Expand All @@ -9,6 +8,7 @@ import {
CreateServerMutation,
CreateServerMutationVariables,
} from '~/generated-metadata/graphql';
import { isDefined } from '~/utils/isDefined';

export const useCreateOneDatabaseConnection = () => {
const apolloMetadataClient = useApolloMetadataClient();
Expand All @@ -27,8 +27,28 @@ export const useCreateOneDatabaseConnection = () => {
variables: {
input,
},
awaitRefetchQueries: true,
refetchQueries: [getOperationName(GET_MANY_DATABASE_CONNECTIONS) ?? ''],
update: (cache, { data }) => {
const createdRemoteServer = data?.createOneRemoteServer;
if (!createdRemoteServer) return;

const getManyDatabaseConnectionsQuery = {
query: GET_MANY_DATABASE_CONNECTIONS,
variables: {
input: { foreignDataWrapperType: input.foreignDataWrapperType },
},
};

if (isDefined(cache.readQuery(getManyDatabaseConnectionsQuery))) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure cache.readQuery returns a defined result before updating the cache.

cache.updateQuery(getManyDatabaseConnectionsQuery, (cachedQuery) => ({
findManyRemoteServersByType: [
...cachedQuery.findManyRemoteServersByType,
createdRemoteServer,
],
}));

return;
}
},
});
};

Expand Down
Loading