forked from twentyhq/twenty
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Optimistic Effects (twentyhq#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
- Loading branch information
1 parent
ae072b6
commit a46210b
Showing
10 changed files
with
188 additions
and
1 deletion.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
front/src/modules/apollo/optimistic-effect/hooks/useOptimisticEffect.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { useApolloClient } from '@apollo/client'; | ||
import { useRecoilCallback } from 'recoil'; | ||
|
||
import { optimisticEffectState } from '../states/optimisticEffectState'; | ||
import { OptimisticEffect } from '../types/OptimisticEffect'; | ||
|
||
export function useOptimisticEffect() { | ||
const apolloClient = useApolloClient(); | ||
|
||
const registerOptimisticEffect = useRecoilCallback( | ||
({ snapshot, set }) => | ||
(optimisticEffect: OptimisticEffect<unknown, unknown>) => { | ||
const { key } = optimisticEffect; | ||
const optimisticEffects = snapshot | ||
.getLoadable(optimisticEffectState) | ||
.getValue(); | ||
|
||
set(optimisticEffectState, { | ||
...optimisticEffects, | ||
[key]: optimisticEffect, | ||
}); | ||
}, | ||
); | ||
|
||
const triggerOptimisticEffects = useRecoilCallback( | ||
({ snapshot }) => | ||
(typename: string, entities: any[]) => { | ||
const optimisticEffects = snapshot | ||
.getLoadable(optimisticEffectState) | ||
.getValue(); | ||
|
||
Object.values(optimisticEffects).forEach((optimisticEffect) => { | ||
if (optimisticEffect.typename === typename) { | ||
optimisticEffect.resolver({ | ||
cache: apolloClient.cache, | ||
entities, | ||
variables: optimisticEffect.variables, | ||
}); | ||
} | ||
}); | ||
}, | ||
); | ||
|
||
return { | ||
registerOptimisticEffect, | ||
triggerOptimisticEffects, | ||
}; | ||
} |
10 changes: 10 additions & 0 deletions
10
front/src/modules/apollo/optimistic-effect/states/optimisticEffectState.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { atom } from 'recoil'; | ||
|
||
import { OptimisticEffect } from '../types/OptimisticEffect'; | ||
|
||
export const optimisticEffectState = atom< | ||
Record<string, OptimisticEffect<unknown, unknown>> | ||
>({ | ||
key: 'optimisticEffectState', | ||
default: {}, | ||
}); |
18 changes: 18 additions & 0 deletions
18
front/src/modules/apollo/optimistic-effect/types/OptimisticEffect.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { ApolloCache } from '@apollo/client'; | ||
|
||
type OptimisticEffectResolver<T, QueryVariables> = ({ | ||
cache, | ||
entities, | ||
variables, | ||
}: { | ||
cache: ApolloCache<T>; | ||
entities: T[]; | ||
variables: QueryVariables; | ||
}) => void; | ||
|
||
export type OptimisticEffect<T, QueryVariables> = { | ||
key: string; | ||
typename: string; | ||
variables: QueryVariables; | ||
resolver: OptimisticEffectResolver<T, QueryVariables>; | ||
}; |
47 changes: 47 additions & 0 deletions
47
front/src/modules/companies/graphql/optimistic-effects/getCompaniesOptimisticEffect.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
front/src/modules/people/graphql/optimistic-effect-callback/getPeopleOptimisticEffect.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { ApolloCache } from '@apollo/client'; | ||
|
||
import { | ||
GetPeopleQuery, | ||
GetPeopleQueryVariables, | ||
Person, | ||
} from '~/generated/graphql'; | ||
|
||
import { GET_PEOPLE } from '../queries/getPeople'; | ||
|
||
function optimisticEffectResolver({ | ||
cache, | ||
entities, | ||
variables, | ||
}: { | ||
cache: ApolloCache<Person>; | ||
entities: Person[]; | ||
variables: GetPeopleQueryVariables; | ||
}) { | ||
const existingData = cache.readQuery<GetPeopleQuery>({ | ||
query: GET_PEOPLE, | ||
variables: { orderBy: variables.orderBy, where: variables.where }, | ||
}); | ||
|
||
if (!existingData) { | ||
return; | ||
} | ||
|
||
cache.writeQuery({ | ||
query: GET_PEOPLE, | ||
variables: { orderBy: variables.orderBy, where: variables.where }, | ||
data: { | ||
people: [...entities, ...existingData.people], | ||
}, | ||
}); | ||
} | ||
|
||
export function getPeopleOptimisticEffect(variables: GetPeopleQueryVariables) { | ||
return { | ||
key: 'generic-entity-table-data-person', | ||
variables: variables, | ||
typename: 'Person', | ||
resolver: optimisticEffectResolver, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters