console/packages/web/docs/pages/features/registry-usage.mdx
2022-08-08 14:58:22 +02:00

162 lines
5.1 KiB
Text

# Using Registry
## CDN Access
With high-availability and multi-zone CDN service based on Cloudflare, Hive allows you to access the registry, through a secured external service, that's always up regardless of Hive services.
To connect your GraphQL Gateway or any other tool with GraphQL Hive you need to generate an access key to our CDN.
Go to the schema view of your target https://app.graphql-hive.com/:org/:project/:target and click "Connect".
![Connect button](../../public/schema-view-connect-button.png)
GraphQL Hive will now generate a unique key:
![Connect to CDN modal](../../public/connect-to-cdn-modal.png)
## Apollo Federation
There are gateways maintained by Apollo team, [Apollo Gateway](#apollo-gateway) and [Apollo Router](#apollo-router). GraphQL Hive supports both!
### Apollo Gateway
- `HIVE_CDN_ENDPOINT` - the endpoint Hive generated for you in the previous step
- `HIVE_CDN_KEY` - the access key
```typescript
import { createSupergraphManager } from '@graphql-hive/client'
import { ApolloGateway } from '@apollo/gateway'
import { ApolloServer } from 'apollo-server'
const gateway = new ApolloGateway({
// Apollo Gateway will fetch Supergraph from GraphQL Hive CDN
supergraphSdl: createSupergraphManager({
endpoint: process.env.HIVE_CDN_ENDPOINT,
key: process.env.HIVE_CDN_KEY,
pollIntervalInMs: 15_000
})
})
const server = new ApolloServer({
gateway
})
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`)
})
```
### Apollo Router
GraphQL Hive ships a custom version of [Apollo Router](https://www.apollographql.com/docs/router/). The reason is that in order to extend Apollo Router, we need to write [a native Rust plugin](https://www.apollographql.com/docs/router/customizations/native).
Download Apollo Router for Linux (x86_64), MacOS (x86_64) or Windows (x86_64):
```bash
curl -fsSL https://graphql-hive.com/apollo-router-download.sh | bash
```
Start the router:
```bash
HIVE_CDN_ENDPOINT="..." HIVE_CDN_KEY="..." ./router
```
- `HIVE_CDN_ENDPOINT` - the endpoint Hive generated for you in the previous step
- `HIVE_CDN_KEY` - the access key
> Apollo Router polls schema from the registry every 10 seconds. In order to change it to 15 seconds pass: `HIVE_CDN_POLL_INTERVAL=15`.
## Schema Stitching
Stitching could be done in many ways, that's why `@graphql-hive/client` provides generic functions, not something dedicated for stitching. Unfortunately the implementation of gateway + polling is up to you.
Here's an example of how it could work
```typescript
import { createServicesFetcher } from '@graphql-hive/client'
const fetchServices = createServicesFetcher({
endpoint: process.env.HIVE_CDN_ENDPOINT,
key: process.env.HIVE_CDN_KEY
})
startMyGraphQLGateway({
// a function that resolves a list of services to stitch them together
async stitchServices() {
const services = await fetchServices()
return services.map(service => {
return {
sdl: service.sdl,
url: service.url,
checksum: service.id // to check if service's schema was modified
}
})
},
pollingInSec: 10 // every 10s
})
```
- `HIVE_CDN_ENDPOINT` - the endpoint Hive generated for you in the previous step
- `HIVE_CDN_KEY` - the access key
The `createServicesFetcher` factory function returns another function that is responsible for fetching a list of services from Hive's high-availability endpoint..
The `startMyGraphQLGateway` represents your GraphQL gateway with built-in polling mechanism, in which the `stitchServices` method is called every 10 seconds.
## Other tools
Here's a list of available endpoints:
### Federation
- list of services - `<endpoint>/schema`
- supergraph - `<endpoint>/supergraph`
### Stitching
- list of services - `<endpoint>/schema`
### Single schema project
- Schema Information `<endpoint>/schema`
Here's an example of how it could work via cURL for a single schema project:
```bash
curl --request GET \
--url https://cdn.graphql-hive.com/asce7c12-753d-hive-bee-d7f2c803e232/schema \
--header 'X-Hive-CDN-Key: aabTxbEyC78NvSPQNO+qLrrRnBvODJJ8k4sL/2EtIwc=' \
--header 'Accept: application/json'
```
Output:
```json
{
"sdl": "<SDL-here>",
"date": "2021-08-20T15:16:40.024Z"
}
```
- Raw SDL `<endpoint>/sdl`
If you wish to get the raw SDL, without a wrapping JSON object, you can specify `/sdl` as suffix to your request.
- Introspection JSON `<endpoint>/introspection`
If you wish to get the introspection JSON for your GraphQL schema, without a wrapping JSON object, you can specify `/introspection` as suffix to your request.
This could be useful for tools that need to get the introspection as-is, like GraphQL Codegen, or GraphQL-Config.
- Metadata `<endpoint>/metadata`
To fetch metadata published along with your GraphQL schema (using `--metadata`), use `/metadata` endpoint
```bash
curl --request GET \
--url https://cdn.graphql-hive.com/asce7c12-753d-hive-bee-d7f2c803e232/metadata \
--header 'X-Hive-CDN-Key: YOUR_KEY_HERE'
```
The value returned is the value you stored, as JSON, with `Content-Type: application/json` header.