|
1 | | -import { createGatewayRuntime } from '@graphql-hive/gateway-runtime'; |
| 1 | +import { |
| 2 | + createGatewayRuntime, |
| 3 | + useCustomFetch, |
| 4 | +} from '@graphql-hive/gateway-runtime'; |
| 5 | +import InMemoryLRUCache from '@graphql-mesh/cache-inmemory-lru'; |
2 | 6 | import { getUnifiedGraphGracefully } from '@graphql-mesh/fusion-composition'; |
3 | 7 | import { createDefaultExecutor } from '@graphql-mesh/transport-common'; |
4 | 8 | import { isDebug } from '@internal/testing'; |
5 | | -import { createSchema } from 'graphql-yoga'; |
| 9 | +import { createSchema, createYoga } from 'graphql-yoga'; |
6 | 10 | import { Registry, register as registry } from 'prom-client'; |
7 | 11 | import { beforeEach, describe, expect, it } from 'vitest'; |
8 | 12 | import usePrometheus, { |
@@ -274,4 +278,67 @@ describe('Prometheus', () => { |
274 | 278 | expect(metrics).toContain('subgraphName="TEST_SUBGRAPH"'); |
275 | 279 | expect(metrics).toContain('operationType="query"'); |
276 | 280 | }); |
| 281 | + |
| 282 | + it('should not increment fetch count on cached responses', async () => { |
| 283 | + const registry = new Registry(); |
| 284 | + const subgraph = createYoga({ schema: subgraphSchema }); |
| 285 | + await using cache = new InMemoryLRUCache(); |
| 286 | + const gateway = createGatewayRuntime({ |
| 287 | + supergraph: getUnifiedGraphGracefully([ |
| 288 | + { |
| 289 | + name: 'TEST_SUBGRAPH', |
| 290 | + schema: subgraphSchema, |
| 291 | + url: 'http://subgraph/graphql', |
| 292 | + }, |
| 293 | + ]), |
| 294 | + cache, |
| 295 | + responseCaching: { |
| 296 | + session: () => null, |
| 297 | + }, |
| 298 | + plugins: (ctx) => [ |
| 299 | + // @ts-expect-error |
| 300 | + useCustomFetch(subgraph.fetch), |
| 301 | + usePrometheus({ |
| 302 | + ...ctx, |
| 303 | + registry, |
| 304 | + metrics: { |
| 305 | + graphql_gateway_subgraph_execute_duration: true, |
| 306 | + graphql_gateway_subgraph_execute_errors: true, |
| 307 | + graphql_gateway_fetch_duration: true, |
| 308 | + }, |
| 309 | + }), |
| 310 | + ], |
| 311 | + }); |
| 312 | + |
| 313 | + for (let i = 0; i < 3; i++) { |
| 314 | + const res = await gateway.fetch('http://gateway/graphql', { |
| 315 | + method: 'POST', |
| 316 | + headers: { |
| 317 | + 'Content-Type': 'application/json', |
| 318 | + }, |
| 319 | + body: JSON.stringify({ |
| 320 | + query: /* GraphQL */ ` |
| 321 | + { |
| 322 | + hello |
| 323 | + } |
| 324 | + `, |
| 325 | + }), |
| 326 | + }); |
| 327 | + await expect(res.json()).resolves.toEqual({ |
| 328 | + data: { hello: 'Hello world!' }, |
| 329 | + }); |
| 330 | + } |
| 331 | + |
| 332 | + const metric = await registry |
| 333 | + .getSingleMetric('graphql_gateway_fetch_duration') |
| 334 | + ?.get(); |
| 335 | + |
| 336 | + const count = metric?.values.find( |
| 337 | + (v) => |
| 338 | + // @ts-expect-error metricName does exist |
| 339 | + v.metricName === 'graphql_gateway_fetch_duration_count', |
| 340 | + )?.value; |
| 341 | + |
| 342 | + expect(count).toBe(1); |
| 343 | + }); |
277 | 344 | }); |
0 commit comments