Skip to content

Commit

Permalink
Import/require enhancements (#8082)
Browse files Browse the repository at this point in the history
* Import/require enhancements

* Gogo

* More

* Fix tests

* default search

* Go

* Prettier

* Let's go

* Dynamic import

* Fix versions

* chore(dependencies): updated changesets for modified dependencies

* ..

* Go

* Lets go

* Changeset

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
ardatan and github-actions[bot] authored Dec 11, 2024
1 parent 19758dc commit 93fb364
Show file tree
Hide file tree
Showing 53 changed files with 315 additions and 435 deletions.
9 changes: 9 additions & 0 deletions .changeset/@graphql-mesh_compose-cli-8082-dependencies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@graphql-mesh/compose-cli": patch
---
dependencies updates:
- Updated dependency [`@graphql-mesh/fusion-composition@^0.7.13` ↗︎](https://www.npmjs.com/package/@graphql-mesh/fusion-composition/v/0.7.13) (from `workspace:^`, in `dependencies`)
- Updated dependency [`@graphql-mesh/include@^0.2.6` ↗︎](https://www.npmjs.com/package/@graphql-mesh/include/v/0.2.6) (from `workspace:^`, in `dependencies`)
- Updated dependency [`@graphql-mesh/string-interpolation@^0.5.7` ↗︎](https://www.npmjs.com/package/@graphql-mesh/string-interpolation/v/0.5.7) (from `workspace:^`, in `dependencies`)
- Updated dependency [`@graphql-mesh/types@^0.103.7` ↗︎](https://www.npmjs.com/package/@graphql-mesh/types/v/0.103.7) (from `workspace:^`, in `dependencies`)
- Updated dependency [`@graphql-mesh/utils@^0.103.7` ↗︎](https://www.npmjs.com/package/@graphql-mesh/utils/v/0.103.7) (from `workspace:^`, in `dependencies`)
9 changes: 9 additions & 0 deletions .changeset/@graphql-mesh_graphql-8082-dependencies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@graphql-mesh/graphql": patch
---
dependencies updates:
- Updated dependency [`@graphql-mesh/cross-helpers@^0.4.9` ↗︎](https://www.npmjs.com/package/@graphql-mesh/cross-helpers/v/0.4.9) (from `workspace:^`, in `dependencies`)
- Updated dependency [`@graphql-mesh/store@^0.103.7` ↗︎](https://www.npmjs.com/package/@graphql-mesh/store/v/0.103.7) (from `workspace:^`, in `dependencies`)
- Updated dependency [`@graphql-mesh/string-interpolation@^0.5.7` ↗︎](https://www.npmjs.com/package/@graphql-mesh/string-interpolation/v/0.5.7) (from `workspace:^`, in `dependencies`)
- Updated dependency [`@graphql-mesh/types@^0.103.7` ↗︎](https://www.npmjs.com/package/@graphql-mesh/types/v/0.103.7) (from `workspace:^`, in `dependencies`)
- Updated dependency [`@graphql-mesh/utils@^0.103.7` ↗︎](https://www.npmjs.com/package/@graphql-mesh/utils/v/0.103.7) (from `workspace:^`, in `dependencies`)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@graphql-mesh/plugin-operation-headers": patch
---
dependencies updates:
- Updated dependency [`@graphql-mesh/types@^0.103.7` ↗︎](https://www.npmjs.com/package/@graphql-mesh/types/v/0.103.7) (from `workspace:^`, in `dependencies`)
- Updated dependency [`@graphql-mesh/utils@^0.103.7` ↗︎](https://www.npmjs.com/package/@graphql-mesh/utils/v/0.103.7) (from `workspace:^`, in `dependencies`)
9 changes: 9 additions & 0 deletions .changeset/@graphql-mesh_supergraph-8082-dependencies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@graphql-mesh/supergraph": patch
---
dependencies updates:
- Updated dependency [`@graphql-mesh/cross-helpers@^0.4.9` ↗︎](https://www.npmjs.com/package/@graphql-mesh/cross-helpers/v/0.4.9) (from `workspace:^`, in `dependencies`)
- Updated dependency [`@graphql-mesh/store@^0.103.7` ↗︎](https://www.npmjs.com/package/@graphql-mesh/store/v/0.103.7) (from `workspace:^`, in `dependencies`)
- Updated dependency [`@graphql-mesh/string-interpolation@^0.5.7` ↗︎](https://www.npmjs.com/package/@graphql-mesh/string-interpolation/v/0.5.7) (from `workspace:^`, in `dependencies`)
- Updated dependency [`@graphql-mesh/types@^0.103.7` ↗︎](https://www.npmjs.com/package/@graphql-mesh/types/v/0.103.7) (from `workspace:^`, in `dependencies`)
- Updated dependency [`@graphql-mesh/utils@^0.103.7` ↗︎](https://www.npmjs.com/package/@graphql-mesh/utils/v/0.103.7) (from `workspace:^`, in `dependencies`)
11 changes: 11 additions & 0 deletions .changeset/@omnigraph_openapi-8082-dependencies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
"@omnigraph/openapi": patch
---
dependencies updates:
- Updated dependency [`@graphql-mesh/cross-helpers@^0.4.9` ↗︎](https://www.npmjs.com/package/@graphql-mesh/cross-helpers/v/0.4.9) (from `workspace:^`, in `dependencies`)
- Updated dependency [`@graphql-mesh/fusion-composition@^0.7.13` ↗︎](https://www.npmjs.com/package/@graphql-mesh/fusion-composition/v/0.7.13) (from `workspace:^`, in `dependencies`)
- Updated dependency [`@graphql-mesh/string-interpolation@^0.5.7` ↗︎](https://www.npmjs.com/package/@graphql-mesh/string-interpolation/v/0.5.7) (from `workspace:^`, in `dependencies`)
- Updated dependency [`@graphql-mesh/types@^0.103.7` ↗︎](https://www.npmjs.com/package/@graphql-mesh/types/v/0.103.7) (from `workspace:^`, in `dependencies`)
- Updated dependency [`@graphql-mesh/utils@^0.103.7` ↗︎](https://www.npmjs.com/package/@graphql-mesh/utils/v/0.103.7) (from `workspace:^`, in `dependencies`)
- Updated dependency [`@omnigraph/json-schema@^0.108.7` ↗︎](https://www.npmjs.com/package/@omnigraph/json-schema/v/0.108.7) (from `workspace:^`, in `dependencies`)
- Updated dependency [`json-machete@^0.97.5` ↗︎](https://www.npmjs.com/package/json-machete/v/0.97.5) (from `workspace:^`, in `dependencies`)
10 changes: 10 additions & 0 deletions .changeset/twelve-tigers-applaud.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
'@graphql-mesh/config': patch
'@graphql-mesh/cli': patch
'@graphql-mesh/include': patch
'@graphql-mesh/json-schema': patch
'@graphql-mesh/runtime': patch
'@graphql-mesh/utils': patch
---

Import enhancements
1 change: 1 addition & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ module.exports = {
plugins: [
'@babel/plugin-proposal-class-properties',
'@babel/plugin-proposal-explicit-resource-management',
'@babel/plugin-syntax-top-level-await',
],
};
57 changes: 35 additions & 22 deletions examples/federation-example/tests/polling.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,20 @@ describe('Polling Test', () => {
SUPERGRAPH_SOURCE,
},
});
if (process.env.DEBUG) {
buildCmd.stderr?.on('data', function stderrListener(data: string) {
console.error(data);
});
}
await new Promise<void>(resolve => {
buildCmd.stdout?.on('data', function stdoutListener(data: string) {
if (data.includes('Done!')) {
buildCmd.stdout?.off('data', stdoutListener);
resolve();
}
if (process.env.DEBUG) {
console.log(data);
}
});
});
const port = await getAvailablePort();
Expand All @@ -57,6 +65,11 @@ describe('Polling Test', () => {
SUPERGRAPH_SOURCE,
},
});
if (process.env.DEBUG) {
serveCmd.stderr?.on('data', function stderrListener(data: string) {
console.error(data);
});
}
await new Promise<void>(resolve => {
serveCmd.stdout?.on('data', function stdoutListener(data: string) {
if (process.env.DEBUG) {
Expand All @@ -70,13 +83,14 @@ describe('Polling Test', () => {
});
const hostname = await getLocalHostName(port);
const url = `http://${hostname}:${port}/graphql`;
const resp = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
query: `
await expect(
fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
query: `
{
__type(name:"Query") {
fields {
Expand All @@ -85,10 +99,9 @@ describe('Polling Test', () => {
}
}
`,
}),
});
const data = await resp.json();
expect(data).toEqual({
}),
}).then(data => data.json()),
).resolves.toEqual({
data: {
__type: {
fields: [
Expand All @@ -110,13 +123,14 @@ describe('Polling Test', () => {
});
changedSupergraph = true;
await new Promise(resolve => setTimeout(resolve, 3000));
const resp2 = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
query: `
await expect(
fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
query: `
{
__type(name:"Query") {
fields {
Expand All @@ -125,10 +139,9 @@ describe('Polling Test', () => {
}
}
`,
}),
});
const data2 = await resp2.json();
expect(data2).toEqual({
}),
}).then(data => data.json()),
).resolves.toEqual({
data: {
__type: {
fields: [
Expand Down
3 changes: 3 additions & 0 deletions examples/hello-world/.meshrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ documents:
query HelloWorld {
greeting
}
serve:
playground: true
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('Persisted Queries', () => {
});
});

afterAll(() => mesh.destroy());
afterAll(() => mesh?.destroy());

it('should give correct response for inline persisted operation', async () => {
const response = await meshHttp.fetch('/graphql', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { findAndParseConfig } from '@graphql-mesh/cli';
import { ProcessedConfig } from '@graphql-mesh/config';
import { getMesh, MeshInstance } from '@graphql-mesh/runtime';
import { printSchemaWithDirectives } from '@graphql-tools/utils';
import { dummyLogger } from '../../../packages/testing/dummyLogger';
import { createApp } from '../example-api/app';

describe('Batching Example', () => {
Expand All @@ -15,14 +16,7 @@ describe('Batching Example', () => {
dir: join(__dirname, '..'),
});
const app = createApp();
config.logger = {
info: () => {},
error: () => {},
warn: () => {},
debug: () => {},
log: () => {},
child: () => config.logger,
};
config.logger = dummyLogger;
config.fetchFn = jest.fn(async (url, options) => app.fetch(url, options));
mesh = await getMesh(config);
});
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"prettier": "prettier --write --list-different .",
"prettier:check": "prettier --check .",
"release": "yarn build && changeset publish",
"test": "cross-env \"JEST=1\" jest --detectOpenHandles --no-watchman",
"test": "cross-env \"JEST=1\" jest --forceExit --no-watchman",
"test:e2e": "cross-env \"JEST=1\" \"E2E_TEST=true\" jest --no-watchman --forceExit",
"test:integration": "cross-env \"JEST=1\" \"INTEGRATION_TEST=true\" jest --forceExit --no-watchman",
"test:integration:leak": "cross-env \"JEST=1\" \"LEAK_TEST=1\" \"INTEGRATION_TEST=true\" jest --detectOpenHandles --detectLeaks --no-watchman",
Expand All @@ -69,6 +69,7 @@
"@babel/core": "7.26.0",
"@babel/plugin-proposal-class-properties": "7.18.6",
"@babel/plugin-proposal-explicit-resource-management": "7.25.9",
"@babel/plugin-syntax-top-level-await": "7.14.5",
"@babel/preset-env": "7.26.0",
"@babel/preset-typescript": "7.26.0",
"@changesets/changelog-github": "0.5.0",
Expand Down
12 changes: 2 additions & 10 deletions packages/cache/redis/__integration_tests__/redis.spec.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
import RedisCache from '@graphql-mesh/cache-redis';
import type { Logger } from '@graphql-mesh/types';
import { dummyLogger } from '../../../testing/dummyLogger';

describe('Redis', () => {
const logger: Logger = {
log: jest.fn(),
debug: jest.fn(),
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
child: () => logger,
};
it('works', async () => {
using redisCache = new RedisCache<any>({
host: '{env.REDIS_HOST}',
port: '{env.REDIS_PORT}',
logger,
logger: dummyLogger,
});
const test = await redisCache.get('test');
expect(test).toBeUndefined();
Expand Down
10 changes: 1 addition & 9 deletions packages/cache/redis/test/cache.spec.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
/* eslint-disable no-new */
import Redis from 'ioredis';
import type { Logger } from '@graphql-mesh/types';
import { dummyLogger as logger } from '../../../testing/dummyLogger';
import RedisCache from '../src/index.js';

jest.mock('ioredis');

describe('redis', () => {
beforeEach(() => jest.clearAllMocks());
const logger: Logger = {
log: jest.fn(),
debug: jest.fn(),
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
child: () => logger,
};

describe('constructor', () => {
let redisUrlEnv: string;
Expand Down
10 changes: 5 additions & 5 deletions packages/compose-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@
},
"dependencies": {
"@commander-js/extra-typings": "^12.1.0",
"@graphql-mesh/fusion-composition": "workspace:^",
"@graphql-mesh/include": "workspace:^",
"@graphql-mesh/string-interpolation": "workspace:^",
"@graphql-mesh/types": "workspace:^",
"@graphql-mesh/utils": "workspace:^",
"@graphql-mesh/fusion-composition": "^0.7.13",
"@graphql-mesh/include": "^0.2.6",
"@graphql-mesh/string-interpolation": "^0.5.7",
"@graphql-mesh/types": "^0.103.7",
"@graphql-mesh/utils": "^0.103.7",
"@graphql-tools/code-file-loader": "^8.1.7",
"@graphql-tools/graphql-file-loader": "^8.0.5",
"@graphql-tools/load": "^8.0.1",
Expand Down
11 changes: 2 additions & 9 deletions packages/compose-cli/tests/loadGraphQLHTTPSubgraph.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,11 @@ import {
introspectionFromSchema,
printSchema,
} from 'graphql';
import type { Logger, MeshFetch } from '@graphql-mesh/types';
import type { MeshFetch } from '@graphql-mesh/types';
import { fakePromise } from '@graphql-tools/utils';
import { dummyLogger as logger } from '../../testing/dummyLogger';
import { loadGraphQLHTTPSubgraph } from '../src/loadGraphQLHTTPSubgraph';

const logger: Logger = {
log: jest.fn(),
debug: jest.fn(),
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
child: () => logger,
};
describe('loadGraphQLHTTPSubgraph', () => {
it('respects schemaHeaders in introspection query', async () => {
const fetchFn = jest.fn<Promise<Response>, Parameters<MeshFetch>>(() =>
Expand Down
10 changes: 1 addition & 9 deletions packages/fusion/composition/tests/loaders.spec.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
import { OperationTypeNode } from 'graphql';
import { createGatewayRuntime, useCustomFetch } from '@graphql-hive/gateway-runtime';
import type { Logger } from '@graphql-mesh/types';
import { loadJSONSchemaSubgraph } from '@omnigraph/json-schema';
import { dummyLogger as logger } from '../../../testing/dummyLogger';
import { composeSubgraphs } from '../src/compose';

describe('Loaders', () => {
const logger: Logger = {
log: jest.fn(),
debug: jest.fn(),
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
child: () => logger,
};
it('works', async () => {
const loadedSubgraph = loadJSONSchemaSubgraph('TEST', {
endpoint: 'http://localhost/my-test-api',
Expand Down
1 change: 1 addition & 0 deletions packages/include/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const jiti = createJiti(
'',
{
debug: !!process.env.DEBUG,
interopDefault: true,
},
);

Expand Down
10 changes: 4 additions & 6 deletions packages/legacy/cli/src/commands/ts-artifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +292,10 @@ export async function generateTsArtifacts(
plugin: async () => {
const importCodes = new Set([
...meshConfigImportCodes,
`import { getMesh, ExecuteMeshFn, SubscribeMeshFn, MeshContext as BaseMeshContext, MeshInstance } from '@graphql-mesh/runtime';`,
`import { getMesh, type ExecuteMeshFn, type SubscribeMeshFn, type MeshContext as BaseMeshContext, type MeshInstance } from '@graphql-mesh/runtime';`,
`import { MeshStore, FsStoreStorageAdapter } from '@graphql-mesh/store';`,
`import { path as pathModule } from '@graphql-mesh/cross-helpers';`,
`import { ImportFn } from '@graphql-mesh/types';`,
`import type { ImportFn } from '@graphql-mesh/types';`,
]);
const results = await Promise.all(
rawSources.map(async source => {
Expand Down Expand Up @@ -344,7 +344,7 @@ ${BASEDIR_ASSIGNMENT_COMMENT}
const importFn: ImportFn = <T>(moduleId: string) => {
const relativeModuleId = (pathModule.isAbsolute(moduleId) ? pathModule.relative(baseDir, moduleId) : moduleId).split('\\\\').join('/').replace(baseDir + '/', '');
switch(relativeModuleId) {${[...importedModulesSet]
.map((importedModuleName, importedModuleIndex) => {
.map(importedModuleName => {
const importPathRelativeToBaseDir = pathModule
.relative(baseDir, importedModuleName)
.split('\\')
Expand All @@ -360,11 +360,9 @@ const importFn: ImportFn = <T>(moduleId: string) => {
.join('/')}`;
importPath = replaceTypeScriptExtension(importPath);
}
const importIdentifier = `importedModule$${importedModuleIndex}`;
importCodes.add(`import * as ${importIdentifier} from ${JSON.stringify(importPath)};`);
return `
case ${JSON.stringify(importPathRelativeToBaseDir)}:
return Promise.resolve(${importIdentifier}) as T;
return import(${JSON.stringify(importPath)}) as T;
`;
})
.join('')}
Expand Down
4 changes: 2 additions & 2 deletions packages/legacy/cli/test/cli.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('runtime', () => {
const builtMesh = (await fs.promises.readFile(meshConfigPath)).toString();

// Check that the import of the main Mesh module has replaced the extension
expect(builtMesh).toMatch(/import \* as importedModule(.*) from ".*\/\.meshrc.js";/);
expect(builtMesh).toMatch(/import\(".*\/\.meshrc.js"\)/);

// Case should stay the same
expect(builtMesh).toMatch(/case "\.meshrc.ts":/);
Expand All @@ -43,7 +43,7 @@ describe('runtime', () => {
const builtMesh = (await fs.promises.readFile(meshConfigPath)).toString();

// Check that the import of the main Mesh module has kept the file's extension
expect(builtMesh).toMatch(/import \* as importedModule(.*) from ".*\/\.meshrc.js";/);
expect(builtMesh).toMatch(/import\(".*\/\.meshrc.js"\)/);

// Check that the reference to the relative module in the "importFn" function has kept the file's extension
expect(builtMesh).toMatch(/case "\.meshrc.js":/);
Expand Down
Loading

0 comments on commit 93fb364

Please sign in to comment.