Skip to content

fix: Correct path to file containing custom fetch function when building Mesh project #5841

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/stupid-cobras-give.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-mesh/config': patch
---

Fix path to file containing custom fetch function when building Mesh project
5 changes: 4 additions & 1 deletion packages/config/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,10 @@ export async function resolveCustomFetch({
additionalPrefixes: additionalPackagePrefixes,
});

importCode += `import fetchFn from ${JSON.stringify(moduleName)};\n`;
const processedModuleName = path.isAbsolute(moduleName)
? moduleName
: path.join('..', moduleName);
importCode += `import fetchFn from ${JSON.stringify(processedModuleName)};\n`;

return {
fetchFn,
Expand Down
6 changes: 6 additions & 0 deletions packages/config/test/mesh-config-sources/custom-fetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { MeshFetchRequestInit } from '@graphql-mesh/types';
import { fetch } from '@whatwg-node/fetch';

export default function (url: string, options?: MeshFetchRequestInit) {
return fetch(url, options);
}
52 changes: 52 additions & 0 deletions packages/config/test/processConfig.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { path as pathModule } from '@graphql-mesh/cross-helpers';
import { writeFile } from '@graphql-mesh/utils';
import { processConfig } from '../src/process';

describe('processConfig', () => {
const generatedMeshConfiguration = '.mesh';

it('should be possible to load custom fetch function', async () => {
const config = await processConfig(
{
customFetch: './mesh-config-sources/custom-fetch.ts',
sources: [],
},
{
dir: __dirname,
generateCode: true,
},
);

// Verify that the code has been generated
expect(config).toBeDefined();
expect(config.importCodes).toBeDefined();

let meshConfigContent = '';

// Find the custom fetch
const importCodesIterator = config.importCodes.values();
let importCodesIteratorResult = importCodesIterator.next();
let includesCustomFetch;
while (!importCodesIteratorResult.done) {
if (importCodesIteratorResult.value.startsWith('import fetchFn from')) {
meshConfigContent = meshConfigContent.concat(importCodesIteratorResult.value, '\n');
includesCustomFetch = true;
break;
}
importCodesIteratorResult = importCodesIterator.next();
}
expect(includesCustomFetch).toBeTruthy();

expect(meshConfigContent).toBeDefined();

// Adding export of fetch function so its resolution is actually attempted
meshConfigContent = meshConfigContent.concat('export { fetchFn };', '\n');

// Create a .ts file with the codes and importCodes content
const meshConfigPath = pathModule.join(__dirname, generatedMeshConfiguration, '/index.ts');
await writeFile(meshConfigPath, meshConfigContent);

const { fetchFn } = await import(meshConfigPath);
expect(fetchFn).toBeDefined();
});
});