Skip to content

Commit 4055476

Browse files
committed
feat: fixed the beforeSource remote hooks
1 parent 8284e3d commit 4055476

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

src/handleBeforeSourceHooks.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ export interface BeforeSourceHookBuildConfig {
2828
* @param fnBuildConfig Build configuration.
2929
*/
3030
const getBeforeSourceHookHandler = (fnBuildConfig: BeforeSourceHookBuildConfig) => {
31-
return async (fnExecConfig: SourceHookExecConfig) => {
31+
return async (fnExecConfig: SourceHookExecConfig): Promise<void> => {
3232
const { baseDir, logger, beforeSource, memoizedFns } = fnBuildConfig;
33-
const { payload, sourceName } = fnExecConfig;
33+
const { payload, sourceName, updateRequest } = fnExecConfig;
3434

3535
const beforeSourceHooks = beforeSource || [];
3636

@@ -65,10 +65,25 @@ const getBeforeSourceHookHandler = (fnBuildConfig: BeforeSourceHookBuildConfig)
6565
if (hookFn) {
6666
try {
6767
const hooksResponse = await hookFn(payload);
68+
if (!hooksResponse) {
69+
continue;
70+
}
6871
if (hookConfig.blocking) {
6972
if (hooksResponse.status.toUpperCase() === HookStatus.ERROR) {
7073
throw new Error(hooksResponse.message);
7174
}
75+
76+
// Handle request modification from hook data using callback pattern (like beforeAll)
77+
if (hooksResponse.data?.request) {
78+
// Use callback to apply modifications (consistent with beforeAll pattern)
79+
if (updateRequest) {
80+
updateRequest(hooksResponse.data.request);
81+
} else {
82+
logger.warn(
83+
'beforeSource hook returned request modifications but no updateRequest callback provided',
84+
);
85+
}
86+
}
7287
}
7388
} catch (err: unknown) {
7489
handleHookExecutionError(err, logger, 'beforeSource');

src/index.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,37 @@ export default async function hooksPlugin(config: PluginConfig): Promise<HooksPl
206206
sourceName,
207207
};
208208

209+
// Provide callback to update request
210+
const updateRequest = (modifications: RequestInit) => {
211+
const { headers: newHeaders, ...otherModifications } = modifications;
212+
213+
// Handle header merging properly
214+
if (newHeaders) {
215+
const originalHeaders = options.headers || {};
216+
let mergedHeaders: Record<string, string> = {};
217+
218+
// Handle different header formats
219+
if (originalHeaders instanceof Headers) {
220+
originalHeaders.forEach((value, key) => {
221+
mergedHeaders[key] = value;
222+
});
223+
} else if (originalHeaders && typeof originalHeaders === 'object') {
224+
mergedHeaders = { ...(originalHeaders as Record<string, string>) };
225+
}
226+
// Merge new headers
227+
Object.assign(mergedHeaders, newHeaders);
228+
options.headers = mergedHeaders;
229+
}
230+
// Apply other modifications (including body with modified query)
231+
Object.assign(options, otherModifications);
232+
};
233+
234+
// Execute hook with callback (consistent with beforeAll pattern)
209235
await beforeSourceHookHandler({
210236
payload,
211237
hookType: 'beforeSource',
212238
sourceName,
239+
updateRequest,
213240
});
214241
} catch (err: unknown) {
215242
throw new GraphQLError(

src/utils/hookResolver.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export interface SourceHookExecConfig {
5757
payload: BeforeSourceHookFunctionPayload | AfterSourceHookFunctionPayload;
5858
hookType: 'beforeSource' | 'afterSource';
5959
sourceName: string;
60+
updateRequest?: (modifications: RequestInit) => void; // Optional callback for beforeSource
6061
}
6162

6263
async function getHookFunction(

0 commit comments

Comments
 (0)