-
-
Notifications
You must be signed in to change notification settings - Fork 288
Update BaseDataService to accommodate mutations, not just queries #9324
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
base: main
Are you sure you want to change the base?
Changes from all commits
603d673
e88e14a
ab2386d
748b6e9
5ce00ba
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 |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ import { | |
| InfiniteData, | ||
| InvalidateOptions, | ||
| InvalidateQueryFilters, | ||
| MutationOptions, | ||
| OmitKeyof, | ||
| QueryClient, | ||
| QueryClientConfig, | ||
|
|
@@ -251,6 +252,39 @@ export class BaseDataService< | |
| return result.pages[pageIndex]; | ||
| } | ||
|
|
||
| /** | ||
| * Execute a mutation (a request that is expected to change the state of a server). | ||
| * Unlike `fetchQuery`, the request will not be cached. | ||
| * | ||
| * @param options - The options defining the mutation. Keep in mind that `mutationKey` and `mutationFn` are required when using data services. | ||
| * Additionally `retry` and `retryDelay` are not available, retries can be customized using the `servicePolicyOptions`. | ||
| * @returns The mutation results. | ||
| */ | ||
| protected async executeMutation< | ||
| TData extends Json, | ||
| TError = unknown, | ||
| TVariables = void, | ||
| TContext = unknown, | ||
| >( | ||
| options: WithRequired< | ||
| OmitKeyof< | ||
| MutationOptions<TData, TError, TVariables, TContext>, | ||
| 'retry' | 'retryDelay' | ||
| >, | ||
| 'mutationKey' | 'mutationFn' | ||
| >, | ||
| ): Promise<TData> { | ||
| const mutationCache = this.#queryClient.getMutationCache(); | ||
|
Collaborator
Author
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.
|
||
| const mutation = mutationCache.build(this.#queryClient, { | ||
| ...options, | ||
| mutationFn: (context) => | ||
| this.#policy.circuitBreakerPolicy.execute(() => | ||
| options.mutationFn(context), | ||
| ), | ||
| }); | ||
| return await mutation.execute(); | ||
|
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. Mutation variables never forwardedMedium Severity
Reviewed by Cursor Bugbot for commit 603d673. Configure here.
Collaborator
Author
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. We are using an older version of TanStack Query where |
||
| } | ||
|
|
||
| /** | ||
| * Invalidate queries serviced by this data service. | ||
| * | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -44,6 +44,15 @@ export type GetActivityResponse = { | |||
| }; | ||||
| }; | ||||
|
|
||||
| export type AddFollowerResponse = { | ||||
| followed: { | ||||
| profileId: string; | ||||
| address: string; | ||||
| name: string; | ||||
| imageUrl?: string | null; | ||||
| }[]; | ||||
| }; | ||||
|
|
||||
| export type PageParam = | ||||
| | { | ||||
| before: string; | ||||
|
|
@@ -60,6 +69,8 @@ export class ExampleDataService extends BaseDataService< | |||
|
|
||||
| readonly #tokensBaseUrl = 'https://tokens.api.cx.metamask.io'; | ||||
|
|
||||
| readonly #socialBaseUrl = 'https://social.api.cx.metamask.io'; | ||||
|
|
||||
| constructor(messenger: ExampleMessenger) { | ||||
| super({ | ||||
| name: serviceName, | ||||
|
|
@@ -139,6 +150,29 @@ export class ExampleDataService extends BaseDataService< | |||
| ); | ||||
| } | ||||
|
|
||||
| async addFollower(followerId: string): Promise<AddFollowerResponse> { | ||||
|
Collaborator
Author
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. Adapted from SocialService:
|
||||
| return this.executeMutation<AddFollowerResponse>({ | ||||
| mutationKey: [`${this.name}:addFollower`, followerId], | ||||
| mutationFn: async () => { | ||||
| const url = new URL(`${this.#socialBaseUrl}/api/v1/users/me/follows`); | ||||
|
|
||||
| const response = await fetch(url, { | ||||
| method: 'PUT', | ||||
| headers: { 'Content-Type': 'application/json' }, | ||||
| body: JSON.stringify({ followerId }), | ||||
| }); | ||||
|
|
||||
| if (!response.ok) { | ||||
| throw new Error( | ||||
| `Mutation failed with status code: ${response.status}.`, | ||||
| ); | ||||
| } | ||||
|
|
||||
| return response.json(); | ||||
| }, | ||||
| }); | ||||
| } | ||||
|
|
||||
| destroy(): void { | ||||
| super.destroy(); | ||||
| } | ||||
|
|
||||


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.
The mutation cache is separate from the query cache, should we sync it with the UI as well?
E.g. https://github.com/MetaMask/core/blob/main/packages/base-data-service/src/BaseDataService.ts#L145-L152
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.
Ooh good point. Okay I'll make this change.