-
Notifications
You must be signed in to change notification settings - Fork 123
Hive Gateway Tester documentation #7180
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
packages/web/docs/src/content/gateway/other-features/testing/_meta.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
packages/web/docs/src/content/gateway/other-features/testing/gateway-tester.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| # Gateway Tester | ||
|
|
||
| `@graphql-hive/gateway-testing` lets you spin up a fully wired Hive Gateway inside your tests. It | ||
| boots the gateway with mocked subgraphs or a proxy target and gives you helpers to execute | ||
| operations without touching the network. | ||
|
|
||
| Under the hood every subgraph call goes through a shared `fetch` implementation powered by | ||
| [@whatwg-node/server](https://github.com/ardatan/whatwg-node/tree/master/packages/server). No HTTP | ||
| servers are started, so there is nothing to bind, close, or clean up—requests flow entirely through | ||
| `Request => Response` functions. | ||
|
|
||
| ## Install | ||
|
|
||
| ```sh npm2yarn | ||
| npm i -D @graphql-hive/gateway-testing | ||
| ``` | ||
|
|
||
| ## Quick start | ||
|
|
||
| ```ts filename="gateway.spec.ts" | ||
| import { expect, vi } from 'vitest' | ||
| import { createGatewayTester } from '@graphql-hive/gateway-testing' | ||
|
|
||
| const onFetchFn = vi.fn() | ||
|
|
||
| const books = { | ||
| name: 'books', | ||
| schema: { | ||
| typeDefs: /* GraphQL */ ` | ||
| type Query { | ||
| book(id: ID!): Book | ||
| } | ||
|
|
||
| type Book { | ||
| id: ID! | ||
| title: String! | ||
| } | ||
| `, | ||
| resolvers: { | ||
| Query: { | ||
| book: (_, { id }) => ({ id, title: 'The Hive Handbook' }) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| await using gateway = createGatewayTester({ | ||
| subgraphs: [books], | ||
| plugins: () => [ | ||
| { | ||
| onFetch({ executionRequest }) { | ||
| onFetchFn(executionRequest?.operationName) | ||
| } | ||
| } | ||
| ] | ||
| }) | ||
|
|
||
| await expect( | ||
| gateway.execute({ | ||
| query: /* GraphQL */ ` | ||
| query Test { | ||
| book(id: "1") { | ||
| title | ||
| } | ||
| } | ||
| ` | ||
| }) | ||
| ).resolves.toStrictEqual({ | ||
| data: { | ||
| book: { | ||
| title: 'The Hive Handbook' | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| expect(onFetchFn).toHaveBeenCalledWith('Test') | ||
| ``` | ||
|
|
||
| Use `gateway.execute` when you want typed GraphQL results, or `gateway.fetch` if you prefer to issue | ||
| raw HTTP calls and inspect headers. Both talk to the in-memory gateway, so your tests stay fast and | ||
| isolated. The spy plugin in the snippet proves gateway plugins fire exactly as they would in a live | ||
| deployment. | ||
|
|
||
| The tester implements `AsyncDisposable`. While no persistent network listeners are created, it's a | ||
| best practice to use `await using` blocks or manual disposal to ensure all internal resources are | ||
| properly cleaned up after each test. | ||
|
|
||
| ## Pick the right mode | ||
|
|
||
| - `supergraph`: load an actual supergraph configuration when you want to mirror production. | ||
| - `subgraphs`: pass inline schemas and let Hive compose a supergraph for you (best for unit-style | ||
| gateway tests). | ||
| - `proxy`: point the gateway at a single schema and capture how requests and headers flow through. | ||
|
|
||
| All gateway plugins and hooks still run in normal order in each mode, so you can exercise | ||
| authentication, observability, and other logic exactly as you would in production. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.