Skip to content

Commit add9143

Browse files
authored
Hive Gateway Tester documentation (#7180)
1 parent bd80433 commit add9143

File tree

3 files changed

+99
-1
lines changed

3 files changed

+99
-1
lines changed

packages/web/docs/src/content/gateway/other-features/testing/_meta.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export default {
22
index: 'Overview',
3+
'gateway-tester': 'Gateway Tester',
34
mocking: 'Mocking',
45
debugging: 'Debugging',
56
snapshot: 'Upstream HTTP Snapshot',
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# Gateway Tester
2+
3+
`@graphql-hive/gateway-testing` lets you spin up a fully wired Hive Gateway inside your tests. It
4+
boots the gateway with mocked subgraphs or a proxy target and gives you helpers to execute
5+
operations without touching the network.
6+
7+
Under the hood every subgraph call goes through a shared `fetch` implementation powered by
8+
[@whatwg-node/server](https://github.com/ardatan/whatwg-node/tree/master/packages/server). No HTTP
9+
servers are started, so there is nothing to bind, close, or clean up—requests flow entirely through
10+
`Request => Response` functions.
11+
12+
## Install
13+
14+
```sh npm2yarn
15+
npm i -D @graphql-hive/gateway-testing
16+
```
17+
18+
## Quick start
19+
20+
```ts filename="gateway.spec.ts"
21+
import { expect, vi } from 'vitest'
22+
import { createGatewayTester } from '@graphql-hive/gateway-testing'
23+
24+
const onFetchFn = vi.fn()
25+
26+
const books = {
27+
name: 'books',
28+
schema: {
29+
typeDefs: /* GraphQL */ `
30+
type Query {
31+
book(id: ID!): Book
32+
}
33+
34+
type Book {
35+
id: ID!
36+
title: String!
37+
}
38+
`,
39+
resolvers: {
40+
Query: {
41+
book: (_, { id }) => ({ id, title: 'The Hive Handbook' })
42+
}
43+
}
44+
}
45+
}
46+
47+
await using gateway = createGatewayTester({
48+
subgraphs: [books],
49+
plugins: () => [
50+
{
51+
onFetch({ executionRequest }) {
52+
onFetchFn(executionRequest?.operationName)
53+
}
54+
}
55+
]
56+
})
57+
58+
await expect(
59+
gateway.execute({
60+
query: /* GraphQL */ `
61+
query Test {
62+
book(id: "1") {
63+
title
64+
}
65+
}
66+
`
67+
})
68+
).resolves.toStrictEqual({
69+
data: {
70+
book: {
71+
title: 'The Hive Handbook'
72+
}
73+
}
74+
})
75+
76+
expect(onFetchFn).toHaveBeenCalledWith('Test')
77+
```
78+
79+
Use `gateway.execute` when you want typed GraphQL results, or `gateway.fetch` if you prefer to issue
80+
raw HTTP calls and inspect headers. Both talk to the in-memory gateway, so your tests stay fast and
81+
isolated. The spy plugin in the snippet proves gateway plugins fire exactly as they would in a live
82+
deployment.
83+
84+
The tester implements `AsyncDisposable`. While no persistent network listeners are created, it's a
85+
best practice to use `await using` blocks or manual disposal to ensure all internal resources are
86+
properly cleaned up after each test.
87+
88+
## Pick the right mode
89+
90+
- `supergraph`: load an actual supergraph configuration when you want to mirror production.
91+
- `subgraphs`: pass inline schemas and let Hive compose a supergraph for you (best for unit-style
92+
gateway tests).
93+
- `proxy`: point the gateway at a single schema and capture how requests and headers flow through.
94+
95+
All gateway plugins and hooks still run in normal order in each mode, so you can exercise
96+
authentication, observability, and other logic exactly as you would in production.

packages/web/docs/src/content/gateway/other-features/testing/index.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import { Callout } from '@theguild/components'
99
Testing and debugging are essential parts of the development process. This section will help you
1010
understand how to test and debug your application.
1111

12-
- Testing Utility
12+
- [Gateway Tester](/docs/gateway/other-features/testing/gateway-tester): Run the Hive Gateway in
13+
memory for fast, isolated tests.
1314
- [Mocking](/docs/gateway/other-features/testing/mocking): Mock your GraphQL schema for testing.
1415
- [HTTP Details in Extensions](/docs/gateway/other-features/testing/debugging): Debugging HTTP
1516
details in the GraphQL response

0 commit comments

Comments
 (0)