Skip to Content
Mesh
v1Source HandlersgRPC / Protobuf

gRPC / Protobuf

image

This Handler allows you to load gRPC definition files (.proto).

npm i @omnigraph/grpc

Then you can use it in your Mesh configuration:

mesh.config.ts
import loadGrpcSubgraph from '@omnigraph/grpc' import { defineConfig } from '@graphql-mesh/compose-cli' export const composeConfig = defineConfig({ subgraphs: [ { sourceHandler: loadGrpcSubgraph('MyGrpcApi', { // gRPC Endpoint endpoint: 'localhost:50051', // Path to the proto file source: 'grpc/proto/Example.proto', // or source: { file: 'grpc/proto/Example.proto', load: { defaults: true, includeDirs: ['grpc/proto'] } } // Request timeout in milliseconds requestTimeout: 200_000, // Use HTTPS instead HTTP for gRPC connection useHTTPS: false, // Use SSL credentials for gRPC connection credentialsSsl: { rootCA: 'path/to/rootCA.pem', certChain: 'path/to/certChain.pem', }, // Prefix to collect Query method default: list, get prefixQueryMethod: ['list', 'get'], // Select certain fields as Query or Mutation // This overrides `prefixQueryMethod` selectQueryOrMutationField: [ { // You can use a pattern matching with * fieldName: '*RetrieveMovies', type: 'Query', }, // Or you can use a specific field name // This will make the field GetMovie available as a Mutation // Because it would be Query because of `prefixQueryMethod` { fieldName: 'GetMovie', type: 'Mutation' } ] // Headers for the protobuf if URL is provided schemaHeaders: { 'x-api-key': 'my-api-key' } }) } ] })

Use Reflection instead of proto files

If you have configured reflection in your gRPC server, you don’t need to provide source.

mesh.config.ts
import { defineConfig } from '@graphql-mesh/compose-cli' import loadGrpcSubgraph from '@omnigraph/grpc' export const composeConfig = defineConfig({ subgraphs: [ { sourceHandler: loadGrpcSubgraph('MyGrpcApi', { endpoint: 'localhost:50051' }) } ] })

Custom Metadata for Authorization

Here you can use metaData field to pass some custom metadata from the context.

mesh.config.ts
import { defineConfig } from '@graphql-mesh/compose-cli' import loadGrpcSubgraph from '@omnigraph/grpc' export const composeConfig = defineConfig({ subgraphs: [ { sourceHandler: loadGrpcSubgraph('MyGrpcApi', { endpoint: 'localhost:50051', metaData: { authorization: "Bearer {context.headers['x-my-token']}", someStaticValue: 'MyStaticValue' } }) } ] })
Last updated on