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

<div className="bg-[#244c5a]/90 rounded-md dark:bg-transparent inline-block">
  ![image](https://user-images.githubusercontent.com/20847995/79218793-b27dcf00-7e59-11ea-8f0f-df97503f5494.png)
</div>

This Handler allows you to load [gRPC](https://grpc.io) definition files (`.proto`).

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

```sh npm2yarn
npm i @omnigraph/grpc
```

Then you can use it in your Mesh configuration:

```ts filename="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`.

```ts filename="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.

```ts filename="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'
        }
      })
    }
  ]
})
```
