Registry / api / apollo-datasource-graphql

apollo-datasource-graphql

JSON →
library1.3.2jsnpmunverified

A library for creating Apollo DataSources that connect to an existing GraphQL API. Current version is 1.3.2. It provides a simple way to integrate external GraphQL services into Apollo Server, similar to RESTDataSource but for GraphQL. Key differentiator: allows using Apollo Server's data source layer with GraphQL APIs, enabling caching, deduplication, and error handling. It is designed for Apollo Server 2.0 and uses GraphQL queries/mutations to fetch data. Note that this project is not actively maintained and has limited test coverage.

npm install apollo-datasource-graphql
INSTALL
IMPORT
SIG · APOLLO-DATASOURCE-
A
apollo-datasource-graphql
apijavascriptv1.3.2
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.

GraphQLDataSource
import { GraphQLDataSource } from 'apollo-datasource-graphql'
const { GraphQLDataSource } = require('apollo-datasource-graphql')
ESM-only; CommonJS require will not work because package type is module
default export
import ApolloGraphQLDataSource from 'apollo-datasource-graphql'
import { default } from 'apollo-datasource-graphql'
There is no default export; only named export GraphQLDataSource
type imports
import type { GraphQLDataSource } from 'apollo-datasource-graphql'
import { GraphQLDataSource } from 'apollo-datasource-graphql'
If only using for type checking, prefer type import to avoid bundling

Defines a GraphQLDataSource subclass that connects to an external GraphQL API, sets auth headers, and runs a query.

import { GraphQLDataSource } from 'apollo-datasource-graphql'; import { gql } from 'apollo-server-express'; const GET_USER = gql` query GetUser($id: ID!) { user(id: $id) { id name } } `; class UserAPI extends GraphQLDataSource { baseURL = 'https://api.example.com/graphql'; async getUser(id: string) { const response = await this.query(GET_USER, { variables: { id }, }); return response.data.user; } willSendRequest(request: any) { request.headers = request.headers || {}; request.headers.authorization = this.context.token; } } export { UserAPI };
Debug
Known issues
gotchaPackage is not actively maintained; last release was in 2019. No test suite exists. Use with caution.
fix
Consider forking the repository or using RESTDataSource if possible.
affects: all
gotchaCommonJS require() will not work. The package is ESM-only (type: module in package.json).
fix
Use import syntax or ensure your project is also ESM.
affects: >=1.3.0
gotchabaseURL must not include a trailing slash. The package appends '/' internally.
fix
Set baseURL without trailing slash, e.g., 'https://api.example.com/graphql'.
affects: all
gotchawillSendRequest must set request.headers property if it does not exist, otherwise it may throw.
fix
Add guard: if (!request.headers) request.headers = {};
affects: <1.2.0
Errors
Common errors & fixes
Cannot find module 'apollo-datasource-graphql'
Package not installed or typo in import path
fix
Run 'npm install apollo-datasource-graphql' and verify import path
TypeError: Cannot read property 'authorization' of undefined
Accessing this.context without checking if it exists or before willSendRequest runs
fix
Ensure willSendRequest checks for context: if (this.context && this.context.token) { request.headers.authorization = this.context.token; }
GraphQLError: Must provide Source. Received: undefined
Calling this.query() without a valid GraphQL document (gql result)
fix
Ensure the first argument is a parsed GraphQL document: const query = gql`...`; then this.query(query)
Upgrade
Version history
1.3.2latest on npm
Audit
Dependencies
graphqlrequiredRequired for parsing GraphQL documents and executing queries
apollo-datasourcerequiredBase class for Apollo DataSource
Agent activity
51 hits · last 30 days
node
42
OpenAI (training)
1
Resources
apollo-datasource-graphql — npm install apollo-datasource-graphql · libregistry