Registry / databases / apollo-datasource-grpc

apollo-datasource-grpc

JSON →
library1.0.2jsnpmunverified

gRPC data source for Apollo Server, enabling partial query caching and integration with gRPC microservices. Version 1.0.2 is the latest stable release. Released as a single major version with no active development. Key differentiators: bridges Apollo's data source pattern with gRPC, supports DataLoader for batching, and works with Apollo's caching layer. Ships TypeScript types. Not to be confused with Apollo REST or SQL data sources.

npm install apollo-datasource-grpc
INSTALL
IMPORT
SIG · APOLLO-DATASOURCE-
A
apollo-datasource-grpc
databasesjavascriptv1.0.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.

GRPCDataSource
import GRPCDataSource from 'apollo-datasource-grpc'
const { GRPCDataSource } = require('apollo-datasource-grpc')
Default export only. Named export pattern will fail.
MoviesAPI
import MoviesAPI from './MoviesAPI'
User-defined class extending GRPCDataSource

Shows how to define a gRPC data source by extending GRPCDataSource, setting up gRPC client with proto loading, and implementing a method using callRPC.

import GRPCDataSource from 'apollo-datasource-grpc'; import * as grpc from 'grpc'; import * as protoLoader from '@grpc/proto-loader'; const packageDefinition = protoLoader.loadSync(__dirname + '/movies.proto'); const proto = grpc.loadPackageDefinition(packageDefinition).Movies; const client = new proto.Assets(process.env.MOVIES_URL ?? 'localhost:50051', grpc.credentials.createInsecure()); class MoviesAPI extends GRPCDataSource { constructor() { super(); this.client = client; } async getMovie(id: string) { const meta = new grpc.Metadata(); meta.add('userId', this.context.currentUser.id); return this.callRPC(0, { args: { id }, meta, rpcName: 'GetMovie' }); } } export default MoviesAPI;
Debug
Known issues
gotchaDefault export only: must use `import GRPCDataSource from 'apollo-datasource-grpc'` not named import.
fix
Use default import syntax.
affects: >=1.0.0
gotchaRequires setting `this.client` before using `callRPC`. Failure results in undefined client errors.
fix
Assign `this.client = client` in constructor.
affects: >=1.0.0
breakingThe constructor takes no arguments. Passing options to super() may cause unexpected behavior.
fix
Do not pass arguments to super(). Configure via properties.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: Cannot read property 'callRPC' of undefined
GRPCDataSource not properly imported (using named import)
fix
Use `import GRPCDataSource from 'apollo-datasource-grpc'` (default import)
Error: No client defined
Forgot to assign `this.client` in the constructor before calling methods
fix
Add `this.client = client;` inside the constructor after super()
Upgrade
Version history
1.0.2latest on npm
Audit
Dependencies
apollo-datasourcerequiredextends Apollo's data source base class
dataloaderoptionaloptional batching support
grpcrequiredgRPC client dependency
@grpc/proto-loaderrequiredloading .proto files
Agent activity
27 hits · last 30 days
node
22
OpenAI (training)
1
Resources
apollo-datasource-grpc — npm install apollo-datasource-grpc · libregistry