|
| 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. |
0 commit comments