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(workspaces): mutate on join workspace #2833

Merged
merged 20 commits into from
Sep 5, 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
Expand Up @@ -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 {
Expand Down Expand Up @@ -43,6 +47,7 @@ const props = defineProps<{
}>()

const { client: apollo } = useApolloClient()
const { activeUser } = useActiveUser()
const { triggerNotification } = useGlobalToast()
const router = useRouter()

Expand All @@ -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(
Copy link
Contributor

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

cache,
getCacheId('User', userId),
'workspaces',
({ variables, helpers: { evict, createUpdatedValue, ref } }) => {
if (variables.filter?.search?.length) return evict()

return createUpdatedValue(({ update }) => {
Copy link
Contributor

@fabis94 fabis94 Aug 30, 2024

Choose a reason for hiding this comment

The 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

Copy link
Contributor

Choose a reason for hiding this comment

The 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)
Expand Down