-
Notifications
You must be signed in to change notification settings - Fork 177
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(workspaces): mutate on join workspace #2833
Changes from all commits
b4a6002
c3b7679
762e835
1ef64a4
d975599
d3d9e72
d42b9b4
6830a78
27b66c6
10a8049
5c5ae60
40a9523
594fab4
05c421c
67bcfff
7e93f9f
ee89f9d
604647d
f7192cc
1c8c980
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,11 @@ import { | |
DashboardJoinWorkspaceDocument, | ||
type WorkspaceInviteDiscoverableWorkspaceBanner_DiscoverableWorkspaceFragment | ||
} from '~/lib/common/generated/gql/graphql' | ||
import { getCacheId, getFirstErrorMessage } from '~/lib/common/helpers/graphql' | ||
import { | ||
getCacheId, | ||
getFirstErrorMessage, | ||
modifyObjectField | ||
} from '~/lib/common/helpers/graphql' | ||
|
||
graphql(` | ||
fragment WorkspaceInviteDiscoverableWorkspaceBanner_DiscoverableWorkspace on DiscoverableWorkspace { | ||
|
@@ -43,6 +47,7 @@ const props = defineProps<{ | |
}>() | ||
|
||
const { client: apollo } = useApolloClient() | ||
const { activeUser } = useActiveUser() | ||
const { triggerNotification } = useGlobalToast() | ||
const router = useRouter() | ||
|
||
|
@@ -60,13 +65,34 @@ const processJoin = async (accept: boolean) => { | |
return | ||
} | ||
|
||
const userId = activeUser.value?.id | ||
if (!userId) return | ||
|
||
const result = await apollo | ||
.mutate({ | ||
mutation: DashboardJoinWorkspaceDocument, | ||
variables: { | ||
input: { | ||
workspaceId: props.workspace.id | ||
} | ||
}, | ||
update(cache, { data }) { | ||
const workspaceId = data?.workspaceMutations.join.id | ||
if (!workspaceId) return | ||
|
||
modifyObjectField( | ||
cache, | ||
getCacheId('User', userId), | ||
'workspaces', | ||
({ variables, helpers: { evict, createUpdatedValue, ref } }) => { | ||
if (variables.filter?.search?.length) return evict() | ||
|
||
return createUpdatedValue(({ update }) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cdriesler User.workspaces has variables as well, you usually want to just evict all filtered (e.g. search specified) cache values cause there's no way to accurately tell if the new workspace fits that filter or not. then only modify the lists in non-filtered cache values There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Evict means - wherever that evicted value was referenced, e.g. in some kind of query living in a UI component, the query will understand that its now missing a part of its data and refetch it |
||
update('totalCount', (totalCount) => totalCount + 1) | ||
update('items', (items) => [...items, ref('Workspace', workspaceId)]) | ||
}) | ||
} | ||
) | ||
} | ||
}) | ||
.catch(convertThrowIntoFetchResult) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should always use the 2nd param of
update
-data
- to get the actual data from the response and validate that the mutation did in fact go through successfully before you make any cache updates