How do I write effects for actions in ngrx/data?

Viewed 479

I work in a project with ngrx, also using other libraries & packages from ngrx like ngrx/entity or ngrx/data.

Sometimes I need to create effects in certains actions belonging (in terms of being dispatched) to ngrx/data.

Where I find some difficulties is dispatching other actions with strong dependencies of some other actions dispatched by ngrx/data in an automatic way or even by myself (custom) like below

...

    @Injectable()
    export class FooEffects {

        fooSelected$ = createEffect(() =>
            this.actions$
                .pipe(
                    ofType(FooActions.fooSelected),
                    map((action) => this.entityActionFactory.create<Foo>('Foo', EntityOp.QUERY_LOAD),
                  ))
);
       ...

        constructor(private actions$: Actions, private entityActionFactory: EntityActionFactory) { }

    }

...

So, how do I listen Actions in ngrx/data like QUERY_LOAD, the common get/all or any other else to achieve the creation of new effects? Does it exist something like

fooNgrxDataEffectForQueryLoad$ = createEffect(() =>
            this.actions$
                .pipe(
                    ofType(('Foo', EntityOp.QUERY_LOAD)),

Or asked in another way, how do I write effects for "NATIVE" actions in ngrx/data?

I know ngrx/data was created to get rid off from of Actions, Selectors, Effects, Reducers as much as it is possible, having therefore less boilerplate. However, there should be a way to create effects from ngrx/data.

Many thanks in advance.

1 Answers

What you have in your example is one of possible ways.

You should listen on actions of ngrx/data type and cause stuff you need. ngrx/data is quite raw and has a lot of limitations.

What they recommend is to extend base classes and to change behavior there adding new actions / effects / reducers etc.

Would be nice if you could provide an example of how you want to change the flow.

Related