Documentation
Other Integrations
Schema Stitching

Schema-Stitching

Installation

npm i @graphql-hive/core

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/core package exports a utility function called createServicesFetcher that can be used to fetch services definition from Hive’s CDN. You can use it to create a GraphQL schema from the all services schemas published to Hive.

The createServicesFetcher function is a part of the @graphql-hive/core package, and it can be used with any GraphQL server runtime. You may use it with Apollo Server, GraphQL Yoga, or any other library to implement a custom GraphQL gateway.

Fetching Services Info from 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.

In this example, we are using GraphQL-Yoga to create the Gateway server.

import { createServer } from 'node:http'
import { buildSchema } from 'graphql'
import { createYoga } from 'graphql-yoga'
import { createServicesFetcher } from '@graphql-hive/core'
import { buildHTTPExecutor } from '@graphql-tools/executor-http'
import { stitchSchemas } from '@graphql-tools/stitch'
 
const fetchServices = createServicesFetcher({
  // The CDN endpoint you are using here should not end with any artifact name.
  // So drop the "/sdl" or "/services" from the URL get from Hive dashboard.
  endpoint: process.env.HIVE_CDN_ENDPOINT,
  key: process.env.HIVE_CDN_KEY
})
 
async function main() {
  const services = await fetchServices()
  const subschemas = services.map(service => {
    return {
      schema: buildSchema(service.sdl),
      executor: buildHTTPExecutor({ endpoint: service.url })
    }
  })
 
  const schema = stitchSchemas({ subschemas })
  const yoga = createYoga({ schema })
  const server = createServer(yoga)
 
  server.listen(4000, () => {
    console.info('Gateway is running on http://localhost:4000/graphql')
  })
}
 
main().catch(err => {
  console.error(err)
  process.exit(1)
})
💡

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

Usage Reporting

Based on the server runtime you choosed, you can enable the usage reporting activate the Hive plugin for the server you are running.

Additional Resources