v0GuidesIntegration with Hive
⚠️
This is the documentation for the old GraphQL Mesh version v0. We recommend upgrading to the latest GraphQL Mesh version v1.

Migrate to GraphQL Mesh v1

GraphQL Hive Integration

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

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;

.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

.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
đź’ˇ

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.

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.

npm i @graphql-mesh/plugin-hive
.meshrc.yaml
plugins:
  - hive:
      token: '{env.HIVE_TOKEN}'
đź’ˇ
You have to create a project on GraphQL Hive and get an access token.

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.

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

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

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

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;

npm i @graphql-mesh/cli @graphql-mesh/openapi graphql

Then configure the source in .meshrc.yml file;

.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

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.

.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
đź’ˇ

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.

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.

npm i @graphql-mesh/transform-hive
.meshrc.yaml
sources:
  - name: FooService
    handler:
      openapi:
        source: ./openapi.json
    transforms:
      - hive:
          token: '{env.MYOAS_HIVE_TOKEN}'

Config API Reference

  • enabled - - If this expression is truthy, mocking would be enabled You can use environment variables expression, for example: process.env.MOCKING_ENABLED != null One of:
    • Boolean
    • String
  • token (type: String) - Access Token for Usage Reporting
  • agent (type: Object) - Agent Options:
    • name (type: String)
    • logger (type: Any)
    • timeout (type: Int) - 30s by default
    • maxRetries (type: Int) - 5 by default
    • minTimeout (type: Int) - 200 by default
    • sendInterval (type: Int) - Send reports in interval (defaults to 10_000ms)
    • maxSize (type: Int) - Max number of traces to send at once (defaults to 25)
  • usage (type: Object) - Collects schema usage based on operations:
    • clientInfo (type: Object) - Extract client info from GraphQL Context:
      • name (type: String) - Extract client name Example: {context.headers['x-client-name']}
      • version (type: String) - Extract client version Example: {context.headers['x-client-version']}
    • max (type: Int) - Hive uses LRU cache to store info about operations. This option represents the maximum size of the cache. Default: 1000
    • ttl (type: Int) - Hive uses LRU cache to store info about operations. This option represents the maximum age of an unused operation in the cache. Default: no ttl
    • exclude (type: Array of String) - A list of operations (by name) to be ignored by Hive.
    • sampleRate (type: Float) - Sample rate to determine sampling. 0.0 = 0% chance of being sent 1.0 = 100% chance of being sent Default: 1.0
    • processVariables (type: Boolean) - (Experimental) Enables collecting Input fields usage based on the variables passed to the operation. Default: false
    • sampler (type: Any)
  • reporting (type: Object) - Schema reporting:
    • author (type: String, required) - Author of current version of the schema
    • commit (type: String, required) - Commit SHA hash (or any identifier) related to the schema version
    • serviceName (type: String)
    • serviceUrl (type: String)
  • selfHosting (type: Object) - Options for self-hosting See more:
    • graphqlEndpoint (type: String, required) - Point to your own instance of GraphQL Hive API

Used by schema reporting and token info.

  • applicationUrl (type: String, required) - Address of your own GraphQL Hive application

Used by token info to generate a link to the organization, project and target.

  • usageEndpoint (type: String) - Point to your own instance of GraphQL Hive Usage API

Used by usage reporting.

  • experimental__persistedDocuments (type: Object) - Experimental persisted documents configuration See more:
    • cdn (type: Object, required) - Point to your own instance of GraphQL Hive API

Used by schema reporting and token info.: * endpoint (type: String, required) - CDN endpoint * accessToken (type: String, required) - Access Token for Persisted Documents CDN

  • allowArbitraryDocuments (type: Boolean) - Whether arbitrary documents should be allowed along-side persisted documents. false by default
  • cache (type: Int) - Maximum amount of operations that shall be kept in memory after being loaded from the CDN. 10 seconds by default