Registry / api / apollo-link

apollo-link

JSON →
library1.2.14jsnpmunverified

Apollo Link is the standard interface for modifying control flow of GraphQL requests and fetching GraphQL results, designed to provide a simple GraphQL client that is capable of extensions. Version 1.2.14 is stable with no active development; consider using @apollo/client (v3+) which includes links natively. Differentiators: composable chain of middleware, Observable-based, supports subscriptions.

npm install apollo-link
INSTALL
IMPORT
SIG · APOLLO-LINK
A
apollo-link
apijavascriptv1.2.14
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

ApolloLink
import { ApolloLink } from 'apollo-link'
const ApolloLink = require('apollo-link').default
Default export in CJS, named export in ESM; ApolloLink class must be extended.
Observable
import { Observable } from 'apollo-link'
import Observable from 'apollo-link'
Named export, not default. Used for creating custom links.
execute
import { execute } from 'apollo-link'
import { execute } from 'apollo-client'
execute function is part of apollo-link, not apollo-client.

Creates a custom Apollo Link that sends GraphQL requests over HTTP using fetch, demonstrating Observable usage.

import { ApolloLink, Observable } from 'apollo-link'; import { print } from 'graphql/language/printer'; class MyLink extends ApolloLink { request(operation, forward) { if (forward) return forward(operation); return new Observable(observer => { fetch('/graphql', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ query: print(operation.query), variables: operation.variables }) }) .then(res => res.json()) .then(result => { observer.next(result); observer.complete(); }) .catch(err => observer.error(err)); }); } } const link = new MyLink(); link.request({ query: '{ hello }', variables: {} }).subscribe({ next: console.log, error: console.error });
Debug
Known issues
deprecatedapollo-link is deprecated. Use @apollo/client which includes links natively.
fix
Migrate to @apollo/client and use ApolloClient with ApolloLink from that package.
affects: >=1.0
breakingBreaking change: Inline fragments inside @export directives fail in Apollo Link as of v1.2.4
fix
Upgrade to @apollo/client which handles this correctly.
affects: >=1.2.4 <1.3.0
gotchaCommon mistake: using `require('apollo-link')` returns default export as object with ApolloLink, Observable, etc. Named imports require destructuring.
fix
Use import syntax: import { ApolloLink } from 'apollo-link'
affects: >=1.0
gotchaCommon mistake: `forward` argument may be undefined if link is terminal. Must check before calling forward(operation).
fix
Check if forward exists: if (forward) return forward(operation); else return new Observable(...)
affects: >=1.0
Errors
Common errors & fixes
Uncaught TypeError: forward is not a function
Calling forward(operation) without checking if forward exists in a terminal link.
fix
Check if forward is defined before calling: if (forward) return forward(operation);
Module not found: Can't resolve 'apollo-link'
Package not installed or not in node_modules.
fix
Run: npm install apollo-link graphql
TypeError: Observable is not a constructor
Importing Observable as default instead of named export.
fix
Use: import { Observable } from 'apollo-link'
import { ApolloLink } from 'apollo-link' returns undefined
Using CommonJS require without destructuring in CJS environment.
fix
Use: const { ApolloLink } = require('apollo-link');
Upgrade
Version history
1.2.14latest on npm
Audit
Dependencies
graphqlrequiredpeer dependency for GraphQL types and execution
Agent activity
73 hits · last 30 days
node
62
OpenAI (training)
1
Resources
apollo-link — npm install apollo-link · libregistry