question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

link-http: cannot set headers

See original GitHub issue

This 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:closed
  • Created 5 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
ireadecommented, Jul 26, 2018

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:

constructor(apollo: Apollo, httpLink: HttpLink) {
    const http = httpLink.create({ uri: environment.graphqlUrl });
    const middleware = new ApolloLink((operation, forward) => {

      // Check for token
      const token = localStorage.getItem('token');
      if (!token) return forward(operation);

      operation.setContext({
        headers: new HttpHeaders().set(
          'Authorization',
          token,
        ),
      });
      return forward(operation);
    });
    const link = middleware.concat(http);

    apollo.create({
      link: link,
      cache: new InMemoryCache()
    });
  }

1reaction
kamilkisielacommented, Oct 26, 2018

@herkulano Maybe you could do it? I can help

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found