Home > database >  Why browser cancels HTTP POST requests when its not a duplicate request?
Why browser cancels HTTP POST requests when its not a duplicate request?

Time:10-17

I am working with Angular10. Calling GraphQL queries. When I call the same GraphQL query multiple times with different POST data all the API calls gets canceled except the last one.

But I want all the APIs needs to be called.

Having this Config in app.module.ts provider

     {
        provide: APOLLO_OPTIONS,
        useFactory: (httpLink: HttpLink) => {
            const http = httpLink.create({uri: '/graphql'});
            const errors = one rror(({graphQLErrors, networkError}) => { return graphQLErrors; });
            const link = errors.concat(http);
            return {
                link,
                cache: new InMemoryCache(),
            };
        },
        deps: [HttpLink],
    }

app.service.ts

  public getEmployeesData(
    input: {projectId: number}
): Observable<ApolloQueryResult<{employee_query: any}>> {
    return this.apollo.watchQuery<{employee_query: any}>({
        query:gql` query employee_query(
    $projectId: INT!
) {
    employee_query(
        projectId: $projectId
    ) {
        firstname
        lastname
        empId
        disignation
        
    }
},
        variables: input,
    }).valueChanges;
}

My Effect.ts

@Effect()
    public getEmployee(): Observable<IAction<any | HttpErrorResponse>> {
        return this._actions$.pipe(
            ofType(EmployeeAction.GET_EMPLOYEE_DATA),
            switchMap((action: IAction<any>) => {
                return this.getEmployeesData(action.payload).pipe(map(
                        (
                            response: ApolloQueryResult<{
                                employee_query?: any;
                            }>,
                        ) => {
                            return EmployeeAction.getEmployeeResponse(
                                response.data.employee_query
                            );
                        },
                    ),
                );
            }),
        );
    }

CodePudding user response:

The issue here happens because the use of switchMap. It is specifically designed to behave like you described:

When a new inner Observable is emitted, switchMap stops emitting items from the earlier-emitted inner Observable and begins emitting items from the new one. It continues to behave like this for subsequent inner Observables.

In your case, mergeMap would be better suited:

Returns an Observable that emits items based on applying a function that you supply to each item emitted by the source Observable, where that function returns an Observable, and then merging those resulting Observables and emitting the results of this merger.

  • Related