Documentation
Integrations and Guides
Apollo Gateway

Apollo Gateway

Apollo-Gateway (opens in a new tab) is the JavaScript implementation of the Apollo Federation gateway runtime.

Hive integrates with Apollo Gateway to provide the Supergraph schema information required for running a federated graph gateway, once all schemas are published.

Installation

Terminal
yarn add @graphql-hive/client

We recommend installing Hive Client package as a direct dependency of your project, because it includes a runtime to send usage reports and schemas to Hive registry.

The @graphql-hive/client package exports a utility function called createSupergraphManager that fetches the Supergraph schema from the Hive's CDN, and also a Apollo-Server plugin (opens in a new tab) for reporting operations.

Supergraph SDL from the CDN

Once you have all services schemas pushed to Hive, and available in the CDN, you can create a CDN Access Token and gain access to the CDN endpoint.

With the endpoint and CDN access token available, you can integrate Hive with Apollo Gateway:

import { ApolloGateway } from '@apollo/gateway'
import { ApolloServer } from '@apollo/server'
import { startStandaloneServer } from '@apollo/server/standalone'
import { createSupergraphManager } from '@graphql-hive/client'
 
const gateway = new ApolloGateway({
  // Apollo Gateway will fetch Supergraph from Hive CDN
  supergraphSdl: createSupergraphManager({
    endpoint: 'HIVE_CDN_ENDPOINT',
    key: 'HIVE_CDN_KEY',
    pollIntervalInMs: 15_000
  })
})
 
const server = new ApolloServer({ gateway })
 
const { url } = await startStandaloneServer({ server })
console.log(`🚀 Server ready at ${url}`)

Usage Reporting

To enable Usage Reporting with your Apollo Gateway, you can use the Hive plugin for Apollo Server:

import { ApolloGateway } from '@apollo/gateway'
import { ApolloServer } from '@apollo/server'
import { startStandaloneServer } from '@apollo/server/standalone'
import { createSupergraphManager } from '@graphql-hive/client'
import { hiveApollo } from '@graphql-hive/client'
 
const gateway = new ApolloGateway({
  // Apollo Gateway will fetch Supergraph from Hive CDN
  supergraphSdl: createSupergraphManager({
    endpoint: process.env.HIVE_CDN_ENDPOINT,
    key: process.env.HIVE_CDN_KEY,
    pollIntervalInMs: 15_000
  })
})
 
const server = new ApolloServer({
  gateway,
  plugins: [
    hiveApollo({
      enabled: true,
      token: 'YOUR-TOKEN',
      reporting: false, // no need to report the schema
      usage: true // add this one to report usage and operations
    })
  ]
})
 
const { url } = await startStandaloneServer({ server })
console.log(`🚀 Server ready at ${url}`)
💡

The HIVE_CDN_ENDPOINT variable should not include any artifact suffix (for example, /supergraph), it should be in the following format: https://cdn.graphql-hive.com/artifacts/v1/TARGET_ID

Additional Resources