Skip to content

Commit 10ba2c4

Browse files
authored
fix: bundling env support (#8150)
* fix: bundler support * fix: bundling env support * .. * Lets go
1 parent e12242b commit 10ba2c4

File tree

5 files changed

+63
-31
lines changed

5 files changed

+63
-31
lines changed

.changeset/plenty-trees-live.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@graphql-mesh/config': patch
3+
'@graphql-mesh/utils': patch
4+
---
5+
6+
Fix support for bundling environments like Next.js
7+
Regression introduced in [#8082](https://github.com/ardatan/graphql-mesh/pull/8082)

packages/legacy/config/src/process.ts

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ export async function processConfig(
104104
const importCodes = new Set([
105105
`import type { GetMeshOptions } from '@graphql-mesh/runtime';`,
106106
`import type { YamlConfig } from '@graphql-mesh/types';`,
107-
`import { defaultImportFn } from '@graphql-mesh/utils';`,
107+
`import { defaultImportFn, handleImport } from '@graphql-mesh/utils';`,
108108
]);
109109
const codes = new Set([
110110
`export const rawServeConfig: YamlConfig.Config['serve'] = ${JSON.stringify(
@@ -124,7 +124,7 @@ export async function processConfig(
124124
if (config.require) {
125125
await Promise.all(config.require.map(mod => importFn(mod)));
126126
for (const mod of config.require) {
127-
importCodes.add(`import '${mod}';`);
127+
codes.add(`await import('${mod}');`);
128128
}
129129
}
130130

@@ -208,11 +208,10 @@ export async function processConfig(
208208
additionalPrefixes: additionalPackagePrefixes,
209209
}).then(({ resolved: HandlerCtor, moduleName }) => {
210210
if (options.generateCode) {
211-
const handlerImportName = pascalCase(handlerName + '_Handler');
211+
const handlerImportName = pascalCase(handlerVariableName);
212212
codes.add(
213-
`const ${handlerImportName} = await defaultImportFn(${JSON.stringify(moduleName)});`,
214-
);
215-
codes.add(`const ${handlerVariableName} = new ${handlerImportName}({
213+
`const ${handlerImportName} = await import(${JSON.stringify(moduleName)}).then(handleImport);\n` +
214+
`const ${handlerVariableName} = new ${handlerImportName}({
216215
name: ${JSON.stringify(source.name)},
217216
config: ${JSON.stringify(handlerConfig)},
218217
baseDir,
@@ -221,7 +220,8 @@ export async function processConfig(
221220
store: sourcesStore.child(${JSON.stringify(source.name)}),
222221
logger: logger.child(${JSON.stringify(source.name)}),
223222
importFn,
224-
});`);
223+
});`,
224+
);
225225
}
226226
return new HandlerCtor({
227227
name: source.name,
@@ -248,19 +248,19 @@ export async function processConfig(
248248
});
249249

250250
if (options.generateCode) {
251-
const transformImportName = pascalCase(transformName + '_Transform');
251+
const transformImportName = pascalCase(transformsVariableName);
252252
codes.add(
253-
`const ${transformImportName} = await defaultImportFn(${JSON.stringify(moduleName)});`,
254-
);
255-
codes.add(`${transformsVariableName}[${transformIndex}] = new ${transformImportName}({
253+
`const ${transformImportName} = await import(${JSON.stringify(moduleName)}).then(handleImport);\n` +
254+
`${transformsVariableName}[${transformIndex}] = new ${transformImportName}({
256255
apiName: ${JSON.stringify(source.name)},
257256
config: ${JSON.stringify(transformConfig)},
258257
baseDir,
259258
cache,
260259
pubsub,
261260
importFn,
262261
logger,
263-
});`);
262+
});`,
263+
);
264264
}
265265

266266
return new TransformCtor({
@@ -304,18 +304,19 @@ export async function processConfig(
304304
});
305305

306306
if (options.generateCode) {
307-
const transformImportName = pascalCase(transformName + '_Transform');
308-
importCodes.add(`import ${transformImportName} from ${JSON.stringify(moduleName)};`);
309-
310-
codes.add(`transforms[${transformIndex}] = new (${transformImportName} as any)({
307+
const transformImportName = pascalCase('Root_Transform_' + transformIndex);
308+
codes.add(
309+
`const ${transformImportName} = await import(${JSON.stringify(moduleName)}).then(handleImport);\n` +
310+
`transforms[${transformIndex}] = new ${transformImportName}({
311311
apiName: '',
312312
config: ${JSON.stringify(transformConfig)},
313313
baseDir,
314314
cache,
315315
pubsub,
316316
importFn,
317317
logger,
318-
})`);
318+
})`,
319+
);
319320
}
320321
return new TransformLibrary({
321322
apiName: '',
@@ -337,7 +338,7 @@ export async function processConfig(
337338
if (options.generateCode) {
338339
const importProp = `[${JSON.stringify(importName)}]`;
339340
codes.add(
340-
`const ${importName} = await defaultImportFn(${JSON.stringify(moduleName)}).then(m => m?.${importProp});`,
341+
`const ${importName} = await import(${JSON.stringify(moduleName)}).then(m => m?.${importProp});`,
341342
);
342343
codes.add(
343344
`additionalEnvelopPlugins[${pluginIndex}] = await ${importName}(${JSON.stringify(
@@ -367,7 +368,7 @@ export async function processConfig(
367368
if (options.generateCode) {
368369
const importName = camelCase('use_' + pluginName);
369370
codes.add(
370-
`const ${importName} = await defaultImportFn(${JSON.stringify(moduleName)});`,
371+
`const ${importName} = await import(${JSON.stringify(moduleName)}).then(handleImport);`,
371372
);
372373
codes.add(`additionalEnvelopPlugins[${pluginIndex}] = await ${importName}({
373374
...(${JSON.stringify(pluginConfig, null, 2)}),
@@ -548,7 +549,9 @@ export async function processConfig(
548549

549550
const mergerLoggerPrefix = `${mergerName}Merger`;
550551
if (options.generateCode) {
551-
codes.add(`const Merger = await defaultImportFn(${JSON.stringify(mergerModuleName)});`);
552+
codes.add(
553+
`const Merger = await import(${JSON.stringify(mergerModuleName)}).then(handleImport);`,
554+
);
552555
codes.add(`const merger = new Merger({
553556
cache,
554557
pubsub,
@@ -566,9 +569,9 @@ export async function processConfig(
566569

567570
if (config.additionalEnvelopPlugins) {
568571
codes.add(
569-
`const importedAdditionalEnvelopPlugins = await defaultImportFn(${JSON.stringify(
572+
`const importedAdditionalEnvelopPlugins = await import(${JSON.stringify(
570573
pathModule.join('..', config.additionalEnvelopPlugins).split('\\').join('/'),
571-
)});`,
574+
)}).then(handleImport);`,
572575
);
573576
const importedAdditionalEnvelopPlugins = await importFn(
574577
pathModule.isAbsolute(config.additionalEnvelopPlugins)
@@ -649,7 +652,7 @@ export async function processConfig(
649652
${[...documentHashMapCodes].join(',\n')}
650653
}`);
651654
codes.add(
652-
`const usePersistedOperations = await defaultImportFn('@graphql-yoga/plugin-persisted-operations').then(m => m?.usePersistedOperations);`,
655+
`const usePersistedOperations = await import('@graphql-yoga/plugin-persisted-operations').then(m => m?.usePersistedOperations);`,
653656
);
654657
codes.add(`additionalEnvelopPlugins.push(usePersistedOperations({
655658
getPersistedOperation(key) {

packages/legacy/config/src/utils.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ export async function resolveCustomFetch({
120120
if (!fetchConfig) {
121121
return {
122122
fetchFn: defaultFetch,
123-
code: `const fetchFn = await defaultImportFn('@whatwg-node/fetch').then(m => m?.fetch || m);`,
123+
code: `const fetchFn = await import('@whatwg-node/fetch').then(m => m?.fetch || m);`,
124124
};
125125
}
126126
const { moduleName, resolved: fetchFn } = await getPackage<MeshFetch>({
@@ -132,7 +132,7 @@ export async function resolveCustomFetch({
132132
});
133133

134134
const processedModuleName = moduleName.startsWith('.') ? path.join('..', moduleName) : moduleName;
135-
const code = `const fetchFn = await defaultImportFn(${JSON.stringify(processedModuleName)}).then(m => m?.fetch || m);`;
135+
const code = `const fetchFn = await import(${JSON.stringify(processedModuleName)}).then(m => m?.fetch || m);`;
136136

137137
return {
138138
fetchFn,
@@ -173,7 +173,7 @@ export async function resolveCache(
173173
logger,
174174
});
175175

176-
const code = `const MeshCache = await defaultImportFn(${JSON.stringify(moduleName)});
176+
const code = `const MeshCache = await import(${JSON.stringify(moduleName)}).then(handleImport);
177177
const cache = new MeshCache({
178178
...${JSON.stringify(config)},
179179
importFn,
@@ -218,7 +218,7 @@ export async function resolvePubSub(
218218

219219
const pubsub = new PubSub(pubsubConfig);
220220

221-
const code = `const PubSub = await defaultImportFn(${JSON.stringify(moduleName)});
221+
const code = `const PubSub = await import(${JSON.stringify(moduleName)}).then(handleImport);
222222
const pubsub = new PubSub(${JSON.stringify(pubsubConfig)});`;
223223

224224
return {
@@ -280,7 +280,7 @@ export async function resolveLogger(
280280
return {
281281
logger,
282282
importCode: ``,
283-
code: `const logger = await defaultImportFn(${JSON.stringify(processedModuleName)});`,
283+
code: `const logger = await import(${JSON.stringify(processedModuleName)}).then(handleImport);`,
284284
};
285285
}
286286
const logger = new DefaultLogger(initialLoggerPrefix);

packages/legacy/config/test/processConfig.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe('processConfig', () => {
2828
let codesIteratorResult = codesIterator.next();
2929
let includesCustomFetch;
3030
while (!codesIteratorResult.done) {
31-
if (codesIteratorResult.value.startsWith('const fetchFn = await defaultImportFn(')) {
31+
if (codesIteratorResult.value.startsWith('const fetchFn = await import(')) {
3232
meshConfigContent = meshConfigContent.concat(codesIteratorResult.value, '\n');
3333
includesCustomFetch = true;
3434
break;
@@ -42,7 +42,7 @@ describe('processConfig', () => {
4242
// Adding export of fetch function so its resolution is actually attempted
4343
meshConfigContent = meshConfigContent.concat('export { fetchFn };', '\n');
4444

45-
meshConfigContent = meshConfigContent.replace('await defaultImportFn', 'fakeImport');
45+
meshConfigContent = meshConfigContent.replace('await import', 'fakeImport');
4646
meshConfigContent =
4747
'const fakeImport = m => require("@graphql-tools/utils").fakePromise(require(m));\n' +
4848
meshConfigContent;

packages/legacy/utils/src/defaultImportFn.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,26 @@ function defaultImportFn(path: string): Promise<any> {
3636
);
3737
}
3838

39-
export { defaultImportFn };
39+
function handleImport<T>(module: T): T {
40+
let i = 0;
41+
while ((module as any)?.default != null) {
42+
if (i > 10 || module === (module as any).default) {
43+
break;
44+
}
45+
module = (module as any).default;
46+
i++;
47+
}
48+
if (typeof module === 'object' && module != null) {
49+
const prototypeOfObject = Object.getPrototypeOf(module);
50+
if (prototypeOfObject == null || prototypeOfObject === Object.prototype) {
51+
const normalizedVal: Record<string, any> = {};
52+
for (const key in module) {
53+
normalizedVal[key] = module[key];
54+
}
55+
return normalizedVal as T;
56+
}
57+
}
58+
return module;
59+
}
60+
61+
export { defaultImportFn, handleImport };

0 commit comments

Comments
 (0)