1.0.2 • Published 3 years ago

apollo-datasource-grpc v1.0.2

Weekly downloads
79
License
ISC
Repository
github
Last release
3 years ago

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:

yarn 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

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

5 years ago

0.0.1

5 years ago