link-http: cannot set headers
See original GitHub issueThis is my shared module
import { environment } from './../../environments/environment';
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { HttpClientModule, HttpHeaders } from '@angular/common/http';
import { ApolloModule, Apollo } from 'apollo-angular';
import { HttpLinkModule, HttpLink } from 'apollo-angular-link-http';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { ApolloLink } from 'apollo-link';
@NgModule({
imports: [CommonModule, HttpLinkModule],
exports: [
CommonModule,
HttpClientModule,
ApolloModule,
HttpLinkModule
]
})
export class SharedModule {
constructor(apollo: Apollo, httpLink: HttpLink) {
const http = httpLink.create({ uri: environment.graphqlUrl });
const middleware = new ApolloLink((operation, forward) => {
operation.setContext({
headers: new HttpHeaders().set(
'Authorization',
localStorage.getItem('token') || null,
),
});
return forward(operation);
});
const link = middleware.concat(http);
apollo.create({
link: link,
cache: new InMemoryCache()
});
}
}
When I request query
import { Apollo } from 'apollo-angular';
import gql from 'graphql-tag';
@Injectable()
export class MoviesService {
constructor(private apollo: Apollo) {}
}
const QUERY = gql`
query getMovies {
movies(orderBy:{column:"id" order:ASC}) { ... }
}
`;
this.apollo.query({ query: QUERY }).subscribe( ... )
I get this error
core.js:1448 ERROR Error: Uncaught (in promise): Error: Network error: Cannot read property 'length' of null
Error: Network error: Cannot read property 'length' of null
at new ApolloError (ApolloError.js:43)
at eval (QueryManager.js:325)
at eval (QueryManager.js:758)
at Array.forEach (<anonymous>)
at eval (QueryManager.js:757)
at Map.forEach (<anonymous>)
at QueryManager.broadcastQueries (QueryManager.js:752)
at eval (QueryManager.js:252)
at ZoneDelegate.invoke (zone.js:388)
at Object.onInvoke (core.js:4749)
at new ApolloError (ApolloError.js:43)
at eval (QueryManager.js:325)
at eval (QueryManager.js:758)
at Array.forEach (<anonymous>)
at eval (QueryManager.js:757)
at Map.forEach (<anonymous>)
at QueryManager.broadcastQueries (QueryManager.js:752)
at eval (QueryManager.js:252)
at ZoneDelegate.invoke (zone.js:388)
at Object.onInvoke (core.js:4749)
at resolvePromise (zone.js:814)
at eval (zone.js:877)
at ZoneDelegate.invokeTask (zone.js:421)
at Object.onInvokeTask (core.js:4740)
at ZoneDelegate.invokeTask (zone.js:420)
at Zone.runTask (zone.js:188)
at drainMicroTaskQueue (zone.js:595)
at ZoneTask.invokeTask (zone.js:500)
at ZoneTask.invoke (zone.js:485)
at timer (zone.js:2054)
But without middleware (only with http link) it works fine. Like that
apollo.create({
link: http,
cache: new InMemoryCache()
});
Any suggestions?
Versions
"apollo-angular": "^1.0.1",
"apollo-angular-link-http": "^1.0.3",
"apollo-cache-inmemory": "^1.1.12",
"apollo-client": "^2.2.8",
"apollo-link": "^1.2.2",
"@angular/cli": "~1.7.4",
Issue Analytics
- State:
- Created 5 years ago
- Comments:8 (6 by maintainers)
Top Results From Across the Web
link-http: cannot set headers · Issue #628 · kamilkisiela/apollo ...
This is my shared module import { environment } from './../../environments/environment'; import { NgModule } from '@angular/core'; ...
Read more >Error: Can't set headers after they are sent to the client
The error "Error: Can't set headers after they are sent." means that you're already in the Body or Finished state, but some function...
Read more >5.x API - Express.js
setHeaders. For this option, specify a function to set custom response headers. Alterations to the headers must occur synchronously. The signature of the ......
Read more >Link - HTTP - MDN Web Docs - Mozilla
The HTTP Link entity-header field provides a means for serializing one or more links in HTTP headers. It is semantically equivalent to the ......
Read more >Cannot set headers after they are sent to the client node JS
Today we shall learn how to resolve HTTP Headers Sent Error. In the previous video, we have created login and register rest A...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I was able to get around this by checking if a token exists first, and just forwarding the operation without attempting to apply any headers if it doesn’t exist:
@herkulano Maybe you could do it? I can help