Skip to content

Commit

Permalink
fix: Correct path to file containing custom fetch function when build…
Browse files Browse the repository at this point in the history
…ing Mesh project (#5841)
  • Loading branch information
cweckesser authored Aug 22, 2023
1 parent 4180409 commit afa35c8
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
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();
});
});

0 comments on commit afa35c8

Please sign in to comment.