Apollo Angular HTTP Link
apollo-angular-link-http is an Apollo Link implementation designed for Angular applications to facilitate sending GraphQL operations over standard HTTP requests. This package leverages Angular's `HttpClient` (`@angular/common/http`) for network communication. While its last stable version, `1.11.0`, was released over three years ago, its core functionality for creating an HTTP link has largely been integrated and superseded by the primary `apollo-angular` package. As such, `apollo-angular-link-http` is now considered abandoned. Modern Angular and Apollo setups typically configure `HttpLink` directly through `apollo-angular`'s `ApolloClient` creation process, often utilizing `provideApollo()` for dependency injection. This package previously served as a distinct module for HTTP-based GraphQL communication before the `apollo-angular` ecosystem evolved to incorporate this functionality directly.
Common errors
-
Module not found: Can't resolve 'apollo-angular-link-http'
cause The package `apollo-angular-link-http` is not installed or the import path is incorrect.fixEnsure the package is installed via `npm install apollo-angular-link-http` or `yarn add apollo-angular-link-http`, and verify the import statement. For modern Angular setups, consider migrating to `apollo-angular`'s built-in `HttpLink`. -
Error: NG0201: No provider for HttpLink!
cause The `HttpLinkModule` was not imported into your Angular `NgModule`, or you are using an older module-based setup with a newer Apollo Angular version that expects `provideApollo()`.fixIf using this package, ensure `HttpLinkModule` is in your `imports` array of the relevant `NgModule`. If using `apollo-angular` v10+, consider replacing `HttpLinkModule` with `HttpLink` from `apollo-angular/http` and configuring it via `provideApollo()`. -
Argument of type 'object' is not assignable to parameter of type 'HttpHeaders'.
cause When setting `headers` in `HttpLink.create()` or `context`, a plain JavaScript object was provided instead of an `HttpHeaders` instance.fixAlways construct `HttpHeaders` using `new HttpHeaders()` from `@angular/common/http`: `new HttpHeaders().set('Key', 'Value')`.
Warnings
- breaking The `apollo-angular-link-http` package is effectively abandoned and its functionality has been superseded. Its last publish was over three years ago (v1.11.0). Users should migrate to the main `apollo-angular` package, which now includes its own `HttpLink` implementation and provides modern setup options like `provideApollo()` for Angular applications.
- breaking This package's peer dependency for `@angular/core` is limited to `^6.0.0 || ... || ^10.0.0`. It is not compatible with Angular versions 11 and above, which are now widely used. Attempting to use it with newer Angular versions will lead to peer dependency resolution errors.
- breaking Major versions of `apollo-angular` (e.g., v10, v12, v13) have introduced significant breaking changes, including dropping `ApolloModule` in favor of `provideApollo()`, requiring `@apollo/client` v4 migration, and dropping support for older Angular versions. These changes make `apollo-angular-link-http` incompatible with modern `apollo-angular` setups.
- gotcha The `graphql` peer dependency is very specific (`>=0.11.0 <0.14.0 || ^14.0.0 || ^15.0.0`). Newer versions of `@apollo/client` might require `graphql@^16` or `^17`, leading to potential version conflicts if this package is retained.
Install
-
npm install apollo-angular-link-http -
yarn add apollo-angular-link-http -
pnpm add apollo-angular-link-http
Imports
- HttpLinkModule
import { HttpLinkModule } from 'apollo-angular';import { HttpLinkModule } from 'apollo-angular-link-http'; - HttpLink
const HttpLink = require('apollo-angular-link-http');import { HttpLink } from 'apollo-angular-link-http'; - HttpHeaders
import { HttpHeaders } from '@angular/common/http';
Quickstart
import { NgModule } from '@angular/core';
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/client/core'; // From @apollo/client
import { gql } from 'graphql-tag'; // For defining GraphQL queries
const MY_QUERY = gql`
query MyQuery($id: ID!) {
item(id: $id) {
name
description
}
}
`;
@NgModule({
imports: [HttpClientModule, ApolloModule, HttpLinkModule],
// ... other module configurations
})
class AppModule {
constructor(apollo: Apollo, httpLink: HttpLink) {
apollo.create({
link: httpLink.create({
uri: '/graphql',
includeExtensions: false,
headers: new HttpHeaders().set('Authorization', 'Bearer ' + (process.env.AUTH_TOKEN ?? '')),
}),
cache: new InMemoryCache(),
});
// Example of executing a query with context
apollo.query({
query: MY_QUERY,
variables: { id: '123' },
context: {
headers: new HttpHeaders().set('X-Custom-Header', 'custom-value'),
},
}).subscribe({
next: ({ data, loading }) => {
console.log('Query data:', data);
console.log('Loading:', loading);
},
error: (error) => {
console.error('Query error:', error);
}
});
}
}