Skip to content

Latest commit

 

History

History
111 lines (89 loc) · 2.8 KB

File metadata and controls

111 lines (89 loc) · 2.8 KB
description GraphQL Mesh gRPC Handler allows loading gRPC definition files and using reflection, with custom metadata for authorization. Learn more now!

import { Callout } from '@theguild/components'

gRPC / Protobuf

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

{/* TODO: Implement the loader and the transport */}

npm i @omnigraph/grpc

Then you can use it in your Mesh configuration:

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'],

        // 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.

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.

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'
        }
      })
    }
  ]
})