Skip to content

gRPC implementation of Apollo Server's Datasources. Makes it possible to use Partial Query Caching

Notifications You must be signed in to change notification settings

Inherenc/apollo-datasource-grpc

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

apollo-datasource-grpc

gRPC implementation of Apollo Server's Datasources. Makes it possible to use Partial Query Caching

Usage

To get started, install the apollo-datasource-grpc package:

npm i apollo-datasource-grpc

To define a data source, extend the GRPCDataSource class

import GRPCDataSource from 'apollo-datasource-grpc';
import * as grpc from 'grpc';
import * as protoLoader from '@grpc/proto-loader';

const packageDefinition: any = protoLoader.loadSync(__dirname + '/movies.proto');
const proto: any = grpc.loadPackageDefinition(packageDefinition).Movies;
const client = new proto.Assets(process.env.MOVIES_DATASOURCE_URL, 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' });
  }

  async getMostViewedMovies() {
    const meta = new grpc.Metadata();
    meta.add('userId', this.context.currentUser.id);

    return (await this.callRPC(0, { args: {}, meta, rpcName: 'GetMovies' }) as any).movies;
  }
}

export default MoviesAPI;

It's also possible to user DataLoader for batch requests

About

gRPC implementation of Apollo Server's Datasources. Makes it possible to use Partial Query Caching

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 100.0%