Skip to content

Commit

Permalink
Invalidate myProjects and myDraftProjects qyeries
Browse files Browse the repository at this point in the history
This invalidates the *queries* when createProject is called (picking the
appropriate one depending on whether a project or draft project was
created), rather than invalidating a cache entry that didn't exist for
that ID anyway.
  • Loading branch information
rmunn committed Jul 25, 2024
1 parent 5ac3f95 commit a1a5390
Showing 1 changed file with 7 additions and 13 deletions.
20 changes: 7 additions & 13 deletions frontend/src/lib/gql/gql-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import {
type ChangeOrgMemberRoleMutationVariables,
type AddOrgMemberMutationVariables,
type CreateProjectMutationVariables,
type CreateProjectMutation,
CreateProjectResult,
} from './types';
import type {Readable, Unsubscriber} from 'svelte/store';
import {derived} from 'svelte/store';
Expand All @@ -58,22 +60,14 @@ function createGqlClient(_gqlEndpoint?: string): Client {
},
updates: {
Mutation: {
createProject: (result, args: CreateProjectMutationVariables, cache, _info) => {
createProject: (result: CreateProjectMutation, args: CreateProjectMutationVariables, cache, _info) => {
if (args.input.orgId) {
cache.invalidate({__typename: 'OrgById', id: args.input.orgId}, 'projects');
}
if (args.input.id) {
cache.invalidate({__typename: 'DraftProject', id: args.input.id});
// Urql cache also stores values for myProjects and myDraftProjects query so we need to invalidate them too
// Note singular MyProject name for the myProjects query cache, ditto for draft
cache.invalidate({__typename: 'MyProject', id: args.input.id});
cache.invalidate({__typename: 'MyDraftProject', id: args.input.id});
}
if (result?.createProject?.createProjectResponse?.id) {
cache.invalidate({__typename: 'DraftProject', id: result?.createProject?.createProjectResponse?.id});
cache.invalidate({__typename: 'MyProject', id: result?.createProject?.createProjectResponse?.id});
cache.invalidate({__typename: 'MyDraftProject', id: result?.createProject?.createProjectResponse?.id});
}
const invalidateField = result.createProject.createProjectResponse?.result === CreateProjectResult.Created ? 'myProjects' : 'myDraftProjects';
cache.inspectFields('Query')
.filter(field => field.fieldName === invalidateField)
.forEach(field => cache.invalidate('Query', field.fieldKey));
},
softDeleteProject: (result, args: SoftDeleteProjectMutationVariables, cache, _info) => {
cache.invalidate({__typename: 'Project', id: args.input.projectId});
Expand Down

0 comments on commit a1a5390

Please sign in to comment.