---
description:
  Integrate your GraphQL Mesh app with GraphQL Hive - the schema registry, monitoring and analytics
  tool for your GraphQL API. Track your gateway, publish and consume service schemas.
---

import { Callout, Steps } from '@theguild/components'

# GraphQL Hive Integration

[GraphQL Hive](https://the-guild.dev/graphql/hive) is a schema registry, monitoring and analytics
tool for your GraphQL API, and it is possible to integrate your GraphQL Mesh application to GraphQL
Hive by using the dedicated plugins and transforms.

## Consume Supergraph SDL from Hive CDN

```sh npm2yarn
npm i @graphql-mesh/supergraph
```

If you have a supergraph schema published to GraphQL Hive registry, you can consume it in your
GraphQL Mesh gateway through Hive CDN;

```yaml filename=".meshrc.yaml"
sources:
  - name: Supergraph
    handler:
      supergraph:
        source: https://cdn.graphql-hive.com/SOME_UUID/sdl?ext=.graphql
        schemaHeaders:
          X-Hive-CDN-Key: SOME_TOKEN
```

## Polling the Hive CDN

```yaml filename=".meshrc.yaml"
sources:
  - name: Supergraph
    handler:
      supergraph:
        source: https://cdn.graphql-hive.com/SOME_UUID/sdl?ext=.graphql
        schemaHeaders:
          X-Hive-CDN-Key: SOME_TOKEN
pollingInterval: 60_000 # Poll for the supergraph updates per 1 hour
```

<Callout>
  When you use Hive CDN, you don't have to rebuild Mesh artifacts and/or redeploy it. You can just
  restart it if you don't configure `pollingInterval`. If you configured it already, you don't even
  need to restart the server. GraphQL Mesh will pull the recent changes and update it automatically.
</Callout>

## Track your Gateway

You can connect your GraphQL Mesh gateway to GraphQL Hive registry to track changes on the unified
schema and monitor the operations done by the clients.

```sh npm2yarn
npm i @graphql-mesh/plugin-hive
```

```yaml filename=".meshrc.yaml"
plugins:
  - hive:
      token: '{env.HIVE_TOKEN}'
```

<Callout>You have to create a project on GraphQL Hive and get an access token.</Callout>

## Publish and check the gateway schema

Everytime you build GraphQL Mesh gateway, you can find human-readable SDL version of your schema
under `.mesh/schema.graphql`. You can configure your CI to publish and check your schema
automatically by using Hive CLI.

[Learn more about managing the schema using GraphQL Hive CLI](https://docs.graphql-hive.com/features/publish-schema#using-hive-cli).

## Publish & Consume Service Schemas

GraphQL Mesh can consume GraphQL schemas from Hive CDN, and it is also possible to publish the non
GraphQL sources to Hive CDN and consume them in the gateway.

We have recipes for each case below.

### Consume the GraphQL source schema ([Federation Subgraph](https://the-guild.dev/graphql/hive/federation) or a standalone GraphQL API) from Hive CDN

If your service is originally GraphQL and published to GraphQL Hive registry, you can consume it in
your GraphQL Mesh gateway through Hive CDN;

```yaml filename=".meshrc.yaml"
sources:
  - name: FooService
    handler:
      graphql:
        # URL needs to have `.graphql` extension to tell the handler it is a GraphQL Schema
        source: https://cdn.graphql-hive.com/SOME_UUID/sdl?ext=.graphql
        schemaHeaders:
          X-Hive-CDN-Key: SOME_BASE64
        # The actual endpoint of the service
        endpoint: https://foo-service.com/graphql
```

### Publish the non-GraphQL source schema (OpenAPI, SOAP etc) seperately then consume it in the gateway

Let's say you have different repos on each service and you want to publish the schema on these
repos, and keep the configuration for these services there. And the gateway will consume these
schemas and merge them together.

<Steps>
### Create a project for the service

Create a project for the service, and get an active token for this gateway.

[Learn more about getting tokens from Hive](https://docs.graphql-hive.com/features/tokens)

### Setup Mesh CLI on the non-GraphQL service repo

Let's say you have an OpenAPI service on that repo then install the following packages in this case;

```sh npm2yarn
npm i @graphql-mesh/cli @graphql-mesh/openapi graphql
```

Then configure the source in `.meshrc.yml` file;

```yaml filename=".meshrc.yml"
sources:
  - name: FooService
    handler:
      openapi:
        source: ./openapi.yml
```

### Publish the non-GraphQL service schema

You need to install GraphQL Hive CLI and configure it with the token you already created before.

[See how to install Hive CLI](https://docs.graphql-hive.com/features/publish-schema#using-hive-cli)

Then you need to run `mesh build` like you do on the gateway to generate the schema, so GraphQL Hive
CLI can publish it.

Finally, you can publish the schema `hive schema:publish .mesh/sources/FooService/schema.graphql`

### Create Hive CDN URL and token for the non-GraphQL service

Go to the service project and click `Connect` on the right top of the page. Copy the URL and header
then paste it to the `.meshrc.yml` file.

```yaml filename=".meshrc.yml"
sources:
  - name: FooService
    handler:
      openapi:
        # URL needs to have `.graphql` extension to tell the handler it is a GraphQL Schema
        source: https://cdn.graphql-hive.com/SOME_UUID/sdl?ext=.graphql
        schemaHeaders:
          X-Hive-CDN-Key: SOME_BASE64
```

</Steps>

<Callout>
  If `source` gets a GraphQL Schema for non GraphQL handlers, it doesn't generate the schema again.
  It just reuses it, and adds execution logic only.
</Callout>

### Track your non-GraphQL sources

GraphQL Mesh creates GraphQL APIs from non GraphQL APIs, and it is still possible to register these
to GraphQL Hive as a regular GraphQL API. Add GraphQL Hive transform hooks into the delegation phase
to track operations done to the upstream APIs.

```sh npm2yarn
npm i @graphql-mesh/transform-hive
```

```yaml filename=".meshrc.yaml"
sources:
  - name: FooService
    handler:
      openapi:
        source: ./openapi.json
    transforms:
      - hive:
          token: '{env.MYOAS_HIVE_TOKEN}'
```

## Config API Reference

import API from '../../../generated-markdown/HivePlugin.generated.md'

<API />
