Skip to content

Commit d57f66d

Browse files
committed
fix wrap fetch
1 parent 8698330 commit d57f66d

File tree

8 files changed

+59
-23
lines changed

8 files changed

+59
-23
lines changed

packages/plugins/prometheus/src/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import { type GatewayPlugin } from '@graphql-hive/gateway-runtime';
1+
import type { GatewayPlugin, OnFetchHook } from '@graphql-hive/gateway-runtime';
22
import type { Logger } from '@graphql-hive/logger';
33
import type { OnSubgraphExecuteHook } from '@graphql-mesh/fusion-runtime';
44
import type { TransportEntry } from '@graphql-mesh/transport-common';
55
import type {
66
ImportFn,
77
MeshFetchRequestInit,
88
MeshPlugin,
9-
OnFetchHook,
109
} from '@graphql-mesh/types';
1110
import {
1211
defaultImportFn,

packages/runtime/src/createGatewayRuntime.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,7 @@ import {
2323
import { useHmacUpstreamSignature } from '@graphql-mesh/hmac-upstream-signature';
2424
import useMeshResponseCache from '@graphql-mesh/plugin-response-cache';
2525
import { TransportContext } from '@graphql-mesh/transport-common';
26-
import type {
27-
KeyValueCache,
28-
OnDelegateHook,
29-
OnFetchHook,
30-
} from '@graphql-mesh/types';
26+
import type { KeyValueCache, OnDelegateHook } from '@graphql-mesh/types';
3127
import {
3228
dispose,
3329
getHeadersObj,
@@ -113,6 +109,7 @@ import type {
113109
OnCacheDeleteHook,
114110
OnCacheGetHook,
115111
OnCacheSetHook,
112+
OnFetchHook,
116113
UnifiedGraphConfig,
117114
} from './types';
118115
import {
@@ -156,6 +153,7 @@ export function createGatewayRuntime<
156153
const onCacheDeleteHooks: OnCacheDeleteHook[] = [];
157154
const wrappedFetchFn = wrapFetchWithHooks(
158155
onFetchHooks,
156+
log,
159157
() => instrumentation,
160158
);
161159
const wrappedCache: KeyValueCache | undefined = config.cache

packages/runtime/src/plugins/useCustomAgent.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
import type { Agent as HttpAgent } from 'node:http';
33
// eslint-disable-next-line import/no-nodejs-modules
44
import type { Agent as HttpsAgent } from 'node:https';
5-
import type { OnFetchHookPayload } from '@graphql-mesh/types';
6-
import type { GatewayContext, GatewayPlugin } from '../types';
5+
import type {
6+
GatewayContext,
7+
GatewayPlugin,
8+
OnFetchHookPayload,
9+
} from '../types';
710

811
export type AgentFactory<TContext> = (
912
payload: OnFetchHookPayload<

packages/runtime/src/plugins/usePropagateHeaders.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { subgraphNameByExecutionRequest } from '@graphql-mesh/fusion-runtime';
2-
import type { OnFetchHookDone } from '@graphql-mesh/types';
32
import { handleMaybePromise } from '@whatwg-node/promise-helpers';
4-
import type { GatewayPlugin } from '../types';
3+
import type { GatewayPlugin, OnFetchHookDone } from '../types';
54

65
interface FromClientToSubgraphsPayload {
76
request: Request;

packages/runtime/src/types.ts

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ import type { ResponseCacheConfig } from '@graphql-mesh/plugin-response-cache';
1313
import type {
1414
KeyValueCache,
1515
MeshFetch,
16-
OnFetchHook,
16+
MeshFetchRequestInit,
1717
} from '@graphql-mesh/types';
1818
import type { FetchInstrumentation } from '@graphql-mesh/utils';
1919
import type { HTTPExecutorOptions } from '@graphql-tools/executor-http';
2020
import type {
21+
ExecutionRequest,
2122
IResolvers,
2223
MaybePromise,
2324
TypeSource,
@@ -35,6 +36,7 @@ import type {
3536
Plugin as YogaPlugin,
3637
YogaServerOptions,
3738
} from 'graphql-yoga';
39+
import { GraphQLResolveInfo } from 'graphql/type';
3840
import type { UnifiedGraphConfig } from './handleUnifiedGraphConfig';
3941
import type { UseContentEncodingOpts } from './plugins/useContentEncoding';
4042
import type { AgentFactory } from './plugins/useCustomAgent';
@@ -96,7 +98,7 @@ export type GatewayPlugin<
9698
TContext extends Record<string, any> = Record<string, any>,
9799
> = YogaPlugin<Partial<TPluginContext> & GatewayContext & TContext> &
98100
UnifiedGraphPlugin<Partial<TPluginContext> & GatewayContext & TContext> & {
99-
onFetch?: OnFetchHook<Partial<TPluginContext> & GatewayContext & TContext>;
101+
onFetch?: OnFetchHook<Partial<TPluginContext> & TContext>;
100102
onCacheGet?: OnCacheGetHook;
101103
onCacheSet?: OnCacheSetHook;
102104
onCacheDelete?: OnCacheDeleteHook;
@@ -112,6 +114,38 @@ export type GatewayPlugin<
112114
>;
113115
};
114116

117+
export interface OnFetchHookPayload<TContext> {
118+
url: string;
119+
setURL(url: URL | string): void;
120+
options: MeshFetchRequestInit;
121+
setOptions(options: MeshFetchRequestInit): void;
122+
/**
123+
* The context is not available in cases where "fetch" is done in
124+
* order to pull a supergraph or do some internal work.
125+
*
126+
* The logger will be available in all cases.
127+
*/
128+
context: (GatewayContext & TContext) | { log: Logger };
129+
info: GraphQLResolveInfo;
130+
fetchFn: MeshFetch;
131+
setFetchFn: (fetchFn: MeshFetch) => void;
132+
executionRequest?: ExecutionRequest;
133+
endResponse: (response$: MaybePromise<Response>) => void;
134+
}
135+
136+
export interface OnFetchHookDonePayload {
137+
response: Response;
138+
setResponse: (response: Response) => void;
139+
}
140+
141+
export type OnFetchHookDone = (
142+
payload: OnFetchHookDonePayload,
143+
) => MaybePromise<void>;
144+
145+
export type OnFetchHook<TContext> = (
146+
payload: OnFetchHookPayload<TContext>,
147+
) => MaybePromise<void | OnFetchHookDone>;
148+
115149
export type OnCacheGetHook = (
116150
payload: OnCacheGetHookEventPayload,
117151
) => MaybePromise<OnCacheGetHookResult | void>;

packages/runtime/src/wrapFetchWithHooks.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import { getInstrumented } from '@envelop/instrumentation';
2-
import type {
3-
MeshFetch,
4-
OnFetchHook,
5-
OnFetchHookDone,
6-
} from '@graphql-mesh/types';
7-
import { type ExecutionRequest, type MaybePromise } from '@graphql-tools/utils';
2+
import type { Logger } from '@graphql-hive/logger';
3+
import type { MeshFetch } from '@graphql-mesh/types';
4+
import type { ExecutionRequest, MaybePromise } from '@graphql-tools/utils';
85
import { handleMaybePromise, iterateAsync } from '@whatwg-node/promise-helpers';
6+
import { OnFetchHook, OnFetchHookDone } from './types';
97

108
export type FetchInstrumentation = {
119
fetch?: (
@@ -15,7 +13,8 @@ export type FetchInstrumentation = {
1513
};
1614

1715
export function wrapFetchWithHooks<TContext>(
18-
// onFetchHooks: OnFetchHook<TContext>[], TODO: move over onfetchhook types with new signature
16+
onFetchHooks: OnFetchHook<TContext>[],
17+
log: Logger,
1918
instrumentation?: () => FetchInstrumentation | undefined,
2019
): MeshFetch {
2120
let wrappedFetchFn = function wrappedFetchFn(url, options, context, info) {
@@ -41,7 +40,7 @@ export function wrapFetchWithHooks<TContext>(
4140
setOptions(newOptions) {
4241
options = newOptions;
4342
},
44-
context,
43+
context: { log, ...context },
4544
// @ts-expect-error TODO: why?
4645
info,
4746
get executionRequest() {

packages/runtime/tests/contentEncoding.test.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { getUnifiedGraphGracefully } from '@graphql-mesh/fusion-composition';
2-
import type { OnFetchHookDonePayload } from '@graphql-mesh/types';
32
import { getSupportedEncodings, useContentEncoding } from '@whatwg-node/server';
43
import {
54
createSchema,
@@ -8,7 +7,11 @@ import {
87
type YogaInitialContext,
98
} from 'graphql-yoga';
109
import { afterEach, describe, expect, it, vi } from 'vitest';
11-
import { createGatewayRuntime, useCustomFetch } from '../src/index';
10+
import {
11+
createGatewayRuntime,
12+
OnFetchHookDonePayload,
13+
useCustomFetch,
14+
} from '../src/index';
1215

1316
describe('contentEncoding', () => {
1417
const fooResolver = vi.fn((_, __, _context: YogaInitialContext) => {

yarn.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3934,6 +3934,7 @@ __metadata:
39343934
"@envelop/core": "npm:^5.2.3"
39353935
"@envelop/disable-introspection": "npm:^7.0.0"
39363936
"@envelop/generic-auth": "npm:^9.0.0"
3937+
"@envelop/instrumentation": "npm:^1.0.0"
39373938
"@graphql-hive/core": "npm:^0.10.0"
39383939
"@graphql-hive/logger": "workspace:^"
39393940
"@graphql-hive/logger-json": "workspace:^"

0 commit comments

Comments
 (0)