Skip to content
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

Support custom loaders in ESM format #6660

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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: 4 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ jobs:
timeout_minutes: 10
max_attempts: 5
command: yarn test:leaks --ci
- name: Tests using Node.js runner
if: ${{ matrix.node-version >= 22 }}
run: yarn test:node

trackback:
name: trackback rc dependencies
Expand Down Expand Up @@ -158,7 +161,7 @@ jobs:
with:
timeout_minutes: 10
max_attempts: 5
command: TEST_BROWSER=true yarn jest --no-watchman --ci browser
command: TEST_BROWSER=true yarn jest --no-watchman --ci browser
analyze:
name: Analyze
runs-on: ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"test": "jest --no-watchman",
"test-fed-compat": "ts-node --transpileOnly --compiler-options='{\"module\":\"commonjs\"}' scripts/fetch-federation-tests && yarn test federation-compat",
"test:leaks": "cross-env \"LEAK_TEST=1\" jest --no-watchman --detectOpenHandles --detectLeaks --logHeapUsage",
"test:node": "node --experimental-strip-types --test **/tests/**/*.node.*s",
"ts:check": "tsc --noEmit"
},
"devDependencies": {
Expand Down
38 changes: 25 additions & 13 deletions packages/load/src/utils/custom-loader.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
import { createRequire } from 'module';
import { join as joinPaths } from 'path';
import { isAbsolute, join as joinPaths } from 'path';

export function getCustomLoaderByPath(path: string, cwd: string) {
function extractLoaderFromModule(loaderModule: any) {
if (loaderModule) {
if (loaderModule.default && typeof loaderModule.default === 'function') {
return loaderModule.default;
}
if (typeof loaderModule === 'function') {
return loaderModule;
}
}
}

export async function getCustomLoaderByPath(path: string, cwd: string) {
try {
const requireFn = createRequire(joinPaths(cwd, 'noop.js'));
const requiredModule = requireFn(path);
const absolutePath = isAbsolute(path) ? path : joinPaths(cwd, path);
const importedModule = await import(absolutePath);
return extractLoaderFromModule(importedModule);
} catch (e: any) {}

if (requiredModule) {
if (requiredModule.default && typeof requiredModule.default === 'function') {
return requiredModule.default;
}
return null;
}

if (typeof requiredModule === 'function') {
return requiredModule;
}
}
function getCustomLoaderByPathSync(path: string, cwd: string) {
try {
const requireFn = createRequire(joinPaths(cwd, 'noop.js'));
const requiredModule = requireFn(path);
return extractLoaderFromModule(requiredModule);
} catch (e: any) {}

return null;
Expand All @@ -40,7 +52,7 @@ export function useCustomLoaderSync(loaderPointer: any, cwd: string) {
let loader;

if (typeof loaderPointer === 'string') {
loader = getCustomLoaderByPath(loaderPointer, cwd);
loader = getCustomLoaderByPathSync(loaderPointer, cwd);
} else if (typeof loaderPointer === 'function') {
loader = loaderPointer;
}
Expand Down
20 changes: 20 additions & 0 deletions packages/load/tests/custom-loader.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { buildSchema, parse } from 'graphql';

const loader = function (_, { customLoaderContext: { loaderType }, fooFieldName }) {
if (loaderType === 'documents') {
return parse(/* GraphQL */ `
query TestQuery {
${fooFieldName}
}
`);
} else if (loaderType === 'schema') {
return buildSchema(/* GraphQL */ `
type Query {
${fooFieldName}: String
}
`);
}
return 'I like turtles';
};

export default loader;
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ describe('documentsFromGlob', () => {
loaderType: 'documents',
};
const pointerOptions = {
loader: join(__dirname, '../../custom-loader.js'),
loader: join(__dirname, '../../custom-loader.cjs'),
fooFieldName: 'myFooField',
};
const result = await load(
Expand Down
2 changes: 1 addition & 1 deletion packages/load/tests/loaders/schema/integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ describe('loadSchema', () => {
loaderType: 'schema',
};
const pointerOptions = {
loader: join(__dirname, '../../custom-loader.js'),
loader: join(__dirname, '../../custom-loader.cjs'),
fooFieldName: 'myFooField',
};
const result = await load(
Expand Down
26 changes: 26 additions & 0 deletions packages/load/tests/use-custom-loader.node.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import assert from 'node:assert';
import { describe, it } from 'node:test';
import { versionInfo as graphqlVersionInfo } from 'graphql';
import { useCustomLoader, useCustomLoaderSync } from '../src/utils/custom-loader.ts';

describe('useCustomLoader', () => {
const skipCjsTests = graphqlVersionInfo.major >= 17;

it('can load a custom cjs loader from a file path', { skip: skipCjsTests }, async () => {
const loader = await useCustomLoader(`./custom-loader.cjs`, import.meta.dirname);
const result = await loader('some-name', { customLoaderContext: {} });
assert.strictEqual(result, 'I like turtles');
});

it('can load a custom cjs loader synchronously', { skip: skipCjsTests }, async () => {
const loader = useCustomLoaderSync(`./custom-loader.cjs`, import.meta.dirname);
const result = await loader('some-name', { customLoaderContext: {} });
assert.strictEqual(result, 'I like turtles');
});

it('can load a custom mjs loader from a file path', async () => {
const loader = await useCustomLoader(`./custom-loader.mjs`, import.meta.dirname);
const result = await loader('some-name', { customLoaderContext: {} });
assert.strictEqual(result, 'I like turtles');
});
});
2 changes: 1 addition & 1 deletion packages/load/tests/use-custom-loader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { getCustomLoaderByPath } from '../src/utils/custom-loader.js';

describe('getCustomLoaderByPath', () => {
it('can load a custom loader from a file path', async () => {
const loader = getCustomLoaderByPath('./custom-loader.js', __dirname);
const loader = await getCustomLoaderByPath('./custom-loader.cjs', __dirname);
expect(loader).toBeDefined();
expect(loader('some-name', { customLoaderContext: {} })).toEqual('I like turtles');
});
Expand Down
Loading