Intended outcome:
I am attempting to add an afterware to a BatchHttpLink in order to see if a header was sent in the response. If I use just an HttpLink I am able to access the expected response header. All the code stays the same, just changing between BatchHttpLink and HttpLink. Here is the code I am using (the batch portions commented out right below the regular http portions). This is on Nuxt also in case it matters for debug:
import { ApolloLink } from 'apollo-link'
import { HttpLink } from 'apollo-link-http'
// import { BatchHttpLink } from 'apollo-link-batch-http'
import { InMemoryCache } from 'apollo-cache-inmemory'
export default ctx => {
const httpLink = new HttpLink({ uri: process.env.GRAPHQL_URL })
// const httpLink = new BatchHttpLink({ uri: process.env.GRAPHQL_URL })
// auth token
let token = 'asdf'
// middleware
const middlewareLink = new ApolloLink((operation, forward) => {
if (token) {
operation.setContext({
headers: { authorization: `Bearer ${token}` },
})
}
return forward(operation)
})
// refresh token afterware
const afterwareLink = new ApolloLink((operation, forward) => {
return forward(operation).map(response => {
const context = operation.getContext()
const { response: { headers } } = context
if (headers) {
const refreshToken = headers.get('Refresh-Token')
if (refreshToken) {
console.log('refresh token found: ', refreshToken)
}
}
return response
})
})
const link = ApolloLink.from([middlewareLink, afterwareLink, httpLink])
return {
link,
cache: new InMemoryCache(),
}
}
Actual outcome:
response object is not present at all, therefore unable to read headers property of undefined:
Error: Network error: Cannot read property 'headers' of undefined
at new ApolloError (ApolloError.js:34)
I'm experiencing the same thing when switching from HttpLink to BatchHttpLink
TypeError: Cannot set property 'headers' of undefined
node_modules/apollo-link-batch-http/lib/batchHttpLink.js:27
24 | __extends(BatchHttpLink, _super);
25 | function BatchHttpLink(fetchParams) {
26 | var _this = _super.call(this) || this;
> 27 | _this.headers = {};
28 | _this.batchInterval = (fetchParams && fetchParams.batchInterval) || 10;
29 | _this.batchMax = (fetchParams && fetchParams.batchMax) || 10;
30 | _this.apolloFetch =
Keep in mind apollo-link-batch-http still uses apollo-fetch, and is slated for a rewrite to match the newer apollo-link-http API.
@HeyHugo, try:
new BatchHttpLink
_this is an instance variable
Ah yes missed that, thanks.
Next obstacle:
Unhandled (in react-apollo) Error: Network error: Error writing result to store for query:
...
Cannot read property 'user' of undefined
no queries or anything changed from when using HttpLink (which works fine)
Edit
When looking at the response in chrome network tab, it looks fine I can see the responses from two queries, so it seems like something goes wrong when writing to the store/cache
If you pause on CAUGHT exceptions you will have random errors. Apollo throws errors internally but catches / handles them
I'm doing some research on this but I don't believe this issue is fixed yet. I think the issue below my original post got confused for the main issue. But what I'm experiencing is that BatchHttpLinks do not have response headers in the context when you attach an afterware to the link chain. I will try to research more why this is, but I believe it is because there is nothing setting the context from the response like it does in a normal HttpLink:
Unless I should be setting up afterware differently than I showed in the original post? Or maybe the response headers lie somewhere else but I can't find it.
Most helpful comment
Keep in mind
apollo-link-batch-httpstill usesapollo-fetch, and is slated for a rewrite to match the newerapollo-link-httpAPI.