I've recently started migrating from ngrx to ngxs and had a design question of where I should be placing some of my calls.
In NGRX, I would create 3 actions for each interaction with an api. Something like:
GetEntities - to indicate that the initial api call was made GetEntitiesSuccess - to indicate a successful return of the data GetEntitiesFail - to indicate a unsuccessful return of the data
I would create an effect to watch for the GetEntities Action that actually called the API and handled the response by either calling the Success/Fail actions with the resultant payload.
In NGXS, do I make the api call from the store itself when the action occurs or is there some other NGXS object that I am supposed to use to handle those API calls and then handle the actions the same way I did in ngrx (by creating multiple actions per call)?
Application programming interfaces (APIs) are a way for one program to interact with another. API calls are the medium by which they interact. An API call, or API request, is a message sent to a server asking an API to provide a service or information.
The Actions API provides endpoints to help build, manage, and test your Action. While you can use the Actions API RESTful service by making direct HTTP requests to the server, we provide a client library that makes it easier to access the endpoints from Node.js.
That’s where API calls come in. What is an API Call? An API call is the process of a client application submitting a request to an API and that API retrieving the requested data from the external server or program and delivering it back to the client. Let’s say your app uses Facebook APIs to extract data and functionality from the platform.
Without getting into too much detail, Request Methods characterize what action we are going to take by referring to the API. In total, there are four main types of actions: GET: requests data from a server. This is the most common type of request. Using it we can get the data we are interested in from those that the API is ready to share.
API Example. An API specification can take many forms, but often includes specifications for routines, data structures, object classes, variables, or remote calls. POSIX, Windows API and ASPI are examples of different forms of APIs. – Wikipedia.
Most of the examples I have seen, and how I have used it is to make the API call from the action handler in the state, then when the API returns patch the state immediately.
Then after the patch call, you can dispatch an action to indicate success/failure if you need to. Something like this:
@Action(GetSomeData)
loadData({ patchState, dispatch}: StateContext<MyDataModel>, {payload}: GetSomeData) {
return this.myDataService.get(payload.id)
.pipe(
tap((data) => {
patchState({ data: data});
// optionally dispatch here
dispatch(new GetDataSuccess());
})
);
}
This q/a might also be useful Ngxs - Actions/state to load data from backend
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With