Learn how to implement resolvers in @wordpress/data to automate API data fetching. Discover how to sync your React state with the WordPress REST API seamlessly.
Previously in this course, we covered Registering a Custom Data Store in WordPress @wordpress/data and learned how to build Writing Selectors for Data Access: Memoization in WordPress. While selectors allow us to query the state, they are passive—they only return what is currently in memory.
In this lesson, we introduce resolvers, the mechanism that makes your data layer proactive. A resolver detects when requested data is missing from the store and automatically triggers an API request to fetch it.
In the @wordpress/data architecture, a resolver is a special function associated with a selector. When you call a selector from a component, the data registry checks if the requested data exists. If it doesn't, the registry executes the corresponding resolver.
This creates a "lazy loading" pattern for your plugin data. Your React components don't need to manually orchestrate useEffect hooks to fetch data on mount; they simply ask for the data they need, and the store handles the heavy lifting of fetching it if it’s absent.
To implement a resolver, we add a resolvers object to our createReduxStore configuration. Let's extend our Knowledge Base plugin to fetch articles automatically.
We'll create a resolver that targets a specific article by its ID. If the article isn't in our store, the resolver will call our service layer (which we built in Building the Knowledge Base Service Layer).
JAVASCRIPT// store/resolvers.js import { receiveArticles } from CE9178">'./actions'; import { apiFetchArticles } from CE9178">'../services/api'; export const resolvers = { getArticle(id) { return { type: CE9178">'RESOLVE_ARTICLE', id, }; }, }; // We handle the side effect in a generator function export const controls = { RESOLVE_ARTICLE: ({ id }) => apiFetchArticles(id), }; export const resolversGenerators = { *getArticle(id) { const article = yield { type: CE9178">'RESOLVE_ARTICLE', id }; yield receiveArticles([article]); } };
When you register your store, you pass the resolvers object alongside your actions and selectors. The registry maps the selector getArticle to the resolver getArticle.
JAVASCRIPT// store/index.js import { createReduxStore, register } from CE9178">'@wordpress/data'; import { reducer } from CE9178">'./reducer'; import * as selectors from CE9178">'./selectors'; import * as actions from CE9178">'./actions'; import { resolvers } from CE9178">'./resolvers'; const store = createReduxStore(CE9178">'my-plugin/knowledge-base', { reducer, selectors, actions, resolvers, }); register(store);
useSelect(select => select('my-plugin/knowledge-base').getArticle(123)).getArticle(123) is in the store.getArticle resolver.receiveArticles action to populate the store.Your task is to implement a resolver for the getArticles selector in your Knowledge Base dashboard.
resolvers.js to include a getArticles() function.receiveArticles) to update the store with the fetched response.wp.data.select('my-plugin/knowledge-base').getArticles()) that the data populates the store after the initial empty state.Resolvers transform your data layer from a static cache into a self-filling system. By defining them in your store, you abstract away the complexity of API management from your UI components, keeping your React code clean and declarative.
Up next: Optimizing Performance with Selectors, where we will look at how to ensure our components don't re-render unnecessarily when the store updates.
Learn how to scaffold your React admin dashboard by registering a WordPress menu, creating a root container, and mounting your application into the DOM.
Read moreStop scattering fetch calls across your React components. Learn to build a clean service layer to centralize API logic and simplify your WordPress plugin.
Implementing Resolvers for Data Fetching
Protecting Admin Screens
Production Build Pipeline
Debugging React in the WordPress Admin
Building Search and Filter Functionality
Internationalization in React
Managing File Uploads via REST API
Optimizing API Response Times
Working with Date and Time in React
Implementing Drag-and-Drop Sorting
Creating Custom Hooks for API Logic
Integrating with Gutenberg Blocks
Handling Conflict Resolution
Building a Modal Confirmation System
Implementing Activity Logging
Using Webpack Aliases
Unit Testing API Endpoints
Unit Testing React Components
Handling Large Datasets with GraphQL
Implementing Real-time Updates with Web