Skip to content

Extend DS tutorial to cover state-mutating requests#8962

Draft
mcmire wants to merge 1 commit into
initialize-data-services-docsfrom
add-state-mutating-requests-docs
Draft

Extend DS tutorial to cover state-mutating requests#8962
mcmire wants to merge 1 commit into
initialize-data-services-docsfrom
add-state-mutating-requests-docs

Conversation

@mcmire
Copy link
Copy Markdown
Contributor

@mcmire mcmire commented Jun 1, 2026

Explanation

Requests that change state on the server needs special consideration as we do not want to cache these types of requests. This part of the tutorial walks through how to do this and how to test that caching does not occur.

To read the docs:

  • View on GitHub (note: some things are missing, such as full code examples at the bottom of parts)
  • View locally by running yarn workspace @metamask/wallet-framework-docs run start-dev

Screenshots

Screenshot 2026-06-01 at 2 48 20 PM

References

https://consensyssoftware.atlassian.net/browse/WPC-1003

Checklist

  • I've updated the test suite for new or updated code as appropriate
  • I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate
  • I've communicated my changes to consumers by updating changelogs for packages I've changed
  • I've introduced breaking changes in this PR and have prepared draft pull requests for clients and consumer packages to resolve them

Requests that change state on the server needs special consideration as
we do not want to cache these types of requests. This part of the
tutorial walks through how to do this and how to test that caching does
not occur.

As usual you can view this locally by running:

    yarn workspace @metamask/wallet-framework-docs run start-dev
@mcmire mcmire force-pushed the add-state-mutating-requests-docs branch from 1f59cdc to 31ba7a2 Compare June 1, 2026 20:54
import ordersServiceMethodActionTypes3 from '!!raw-loader!./orders-service-method-action-types.ts';
import ordersServiceTest3 from '!!raw-loader!./orders-service.test.ts';

# Writing a Data Service, Part 3: Making State-Mutating Requests
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided to call these "state-mutating requests" because it sounded better than "non-GET requests". But it's kind of a mouthful. Is there something else we should use?

Now we'll add a new method. <!-- Note that whereas we used `fetchQuery` to make requests previously, now we'll want to use `createMutation`. --> Note the following:

- We pass `method` and `body` to the `fetch` function.
- We pass a `staleTime` of 0 to `fetchQuery`. This instructs TanStack Query not to cache these kinds of requests.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Besides passing a different method to fetch, passing staleTime: 0 to fetchQuery is basically the only difference between "read-only" requests and "state-mutating" requests. The rest is the same thing.

This prompts two questions:

  1. Should we introduce a createMutation method to BaseDataService so that engineers don't have to pass staleTime: 0?
  2. Should we merge parts 2 and 3 together? (I figured not because part 2 was getting long, but happy to reconsider.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant