Implement Optimistic Effects (#1415)
* Fix person deletion not reflected on Opportunities POC * Fix companies, user deletion * Implement optimistic effects * Implement optimistic effects * Implement optimistic effects * Fix accoding to PR
This commit is contained in:
@ -0,0 +1,47 @@
|
||||
import { ApolloCache } from '@apollo/client';
|
||||
|
||||
import {
|
||||
Company,
|
||||
GetCompaniesQuery,
|
||||
GetCompaniesQueryVariables,
|
||||
} from '~/generated/graphql';
|
||||
|
||||
import { GET_COMPANIES } from '../queries/getCompanies';
|
||||
|
||||
function optimisticEffectResolver({
|
||||
cache,
|
||||
entities,
|
||||
variables,
|
||||
}: {
|
||||
cache: ApolloCache<Company>;
|
||||
entities: Company[];
|
||||
variables: GetCompaniesQueryVariables;
|
||||
}) {
|
||||
const existingData = cache.readQuery<GetCompaniesQuery>({
|
||||
query: GET_COMPANIES,
|
||||
variables: { orderBy: variables.orderBy, where: variables.where },
|
||||
});
|
||||
|
||||
if (!existingData) {
|
||||
return;
|
||||
}
|
||||
|
||||
cache.writeQuery({
|
||||
query: GET_COMPANIES,
|
||||
variables: { orderBy: variables.orderBy, where: variables.where },
|
||||
data: {
|
||||
companies: [...entities, ...existingData.companies],
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function getCompaniesOptimisticEffect(
|
||||
variables: GetCompaniesQueryVariables,
|
||||
) {
|
||||
return {
|
||||
key: 'generic-entity-table-data-companies',
|
||||
variables: variables,
|
||||
typename: 'Company',
|
||||
resolver: optimisticEffectResolver,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user