I have this example link:
const consoleLink = new ApolloLink((operation, forward): Observable<
FetchResult
> | null => {
console.log(`starting request for ${operation.operationName}`);
return forward(operation).map((data: FetchResult) => {
console.log(`ending request for ${operation.operationName}`);
return data;
});
});
I get a typescript compile error object is possibly undefined on the forward parameter.
The ApolloLink constructor accepts a RequestHandler which is defined like (operation: Operation, forward?: NextLink) => Observable<FetchResult> | null. By this definition, forward might be undefined.
When I develop my own custom link, I need to handle the case that forward can be undefined. My question is how I handle this. What should I return in this case?
Seems returning null works.
const authMiddleware = new ApolloLink((operation, forward) => {
operation.setContext({
headers: new HttpHeaders()
.set('Authorization', sessionService.getUserToken())
})
if (forward)
return forward(operation)
else
return null
})
Since this issue is really outdated I am closing it but if you are still concerned about this feel free to reopen and I'll get back to you asap.
Most helpful comment
Seems returning
nullworks.