Skip to content

Commit 658f984

Browse files
authored
should not throw pattern is too long from minimatch dependency when SDL schema contain more than 65536 characters (#1418)
* aa * aa * aa
1 parent 5eca929 commit 658f984

File tree

8 files changed

+36
-12
lines changed

8 files changed

+36
-12
lines changed

.changeset/seven-steaks-melt.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'graphql-config': patch
3+
---
4+
5+
should not throw `pattern is too long` from minimatch dependency when SDL schema contain more than 65536 characters

src/project-config.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,11 @@ export class GraphQLProjectConfig {
191191
}
192192
}
193193

194-
// XXX: it works but uses nodejs - expose normalization of file and dir paths in config
194+
function isSDLSchemaLike(schema: string): boolean {
195+
return schema.includes('\n');
196+
}
197+
198+
// XXX: it works but uses Node.js - expose normalization of file and dir paths in config
195199
function match(filepath: string, dirpath: string, pointer?: Pointer): boolean {
196200
if (!pointer) {
197201
return false;
@@ -202,6 +206,10 @@ function match(filepath: string, dirpath: string, pointer?: Pointer): boolean {
202206
}
203207

204208
if (typeof pointer === 'string') {
209+
if (isSDLSchemaLike(pointer)) {
210+
return false;
211+
}
212+
205213
const normalizedFilepath = normalize(isAbsolute(filepath) ? relative(dirpath, filepath) : filepath);
206214
return minimatch(normalizedFilepath, normalize(pointer), { dot: true });
207215
}

test/config.spec.ts

+13-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import { buildSchema, buildASTSchema } from 'graphql';
22
import path from 'path';
33
import { TempDir } from './utils/temp-dir';
44
import { runTests } from './utils/runner';
5-
import { loadConfig, loadConfigSync, ConfigNotFoundError } from 'graphql-config';
6-
import { beforeEach, beforeAll, test, describe, expect, afterAll } from 'vitest';
5+
import { loadConfig, loadConfigSync, ConfigNotFoundError, GraphQLConfig } from 'graphql-config';
76

87
const temp = new TempDir();
98

@@ -278,3 +277,15 @@ runTests({ async: loadConfig, sync: loadConfigSync })((load, mode) => {
278277
});
279278
});
280279
});
280+
281+
describe('GraphQLConfig', () => {
282+
const MINIMATCH_MAX_LENGTH = 65_536;
283+
284+
// https://github.com/dimaMachina/graphql-eslint/issues/2046
285+
it(`should not throw \`pattern is too long\` from minimatch dependency when SDL schema contain more than ${MINIMATCH_MAX_LENGTH} characters`, async () => {
286+
const schema = Array.from({ length: 2_150 }, (_, i) => `type Query${i} { foo: String }`).join('\n');
287+
const graphQLConfig = new GraphQLConfig({ config: { schema }, filepath: '' }, []);
288+
expect(schema.length).toBeGreaterThan(MINIMATCH_MAX_LENGTH);
289+
expect(() => graphQLConfig.getProjectForFile('foo')).not.toThrow();
290+
});
291+
});

test/loaders.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { DirectiveDefinitionNode, buildSchema, GraphQLSchema, Kind } from 'graphql';
22
import { Loader, Source } from '@graphql-tools/utils';
3-
import { beforeAll, test, describe, expect, vi, Mock } from 'vitest';
43
import { LoadersRegistry } from 'graphql-config';
4+
import { Mock } from 'vitest';
55
import { loadTypedefsSync, loadSchemaSync, loadSchema, LoadSchemaOptions } from '@graphql-tools/load';
66

77
vi.mock('@graphql-tools/load', async () => {

test/utils/runner.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { describe } from 'vitest';
2-
31
type PromiseOf<T extends (...args: any[]) => any> = T extends (...args: any[]) => Promise<infer R> ? R : ReturnType<T>;
42

53
export function runTests<
@@ -27,8 +25,8 @@ export function runTests<
2725
}, 'sync');
2826
});
2927
// async
30-
describe(`async`, () => {
31-
testRunner((...args) => executeAsync(...args) as any, 'async');
28+
describe('async', () => {
29+
testRunner(executeAsync as any, 'async');
3230
});
3331
};
3432
}

test/utils/temp-dir.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import path from 'path';
1+
import path from 'node:path';
22
import { deleteSync } from 'del';
33
import makeDir from 'make-dir';
44
import parentModule from 'parent-module';
5-
import os from 'os';
6-
import { Mock, SpyInstance } from 'vitest';
5+
import os from 'node:os';
6+
import type { Mock, MockInstance } from 'vitest';
77
import fs from 'node:fs';
88

99
function normalizeDirectorySlash(pathname: string): string {
@@ -60,7 +60,7 @@ export class TempDir {
6060
fs.writeFileSync(filePath, `${contents}\n`);
6161
}
6262

63-
getSpyPathCalls(spy: Mock | SpyInstance): string[] {
63+
getSpyPathCalls(spy: Mock | MockInstance): string[] {
6464
const calls = spy.mock.calls;
6565

6666
const result = calls.map((call): string => {

tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"noUnusedLocals": true,
1414
"noUnusedParameters": true,
1515
"resolveJsonModule": true,
16+
"types": ["vitest/globals"],
1617
"paths": {
1718
"graphql-config": ["./src/index.ts"]
1819
}

vitest.config.ts

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const CWD = process.cwd();
66

77
export default defineConfig({
88
test: {
9+
globals: true,
910
alias: {
1011
'graphql-config': path.join(CWD, 'src', 'index.ts'),
1112
// fixes Duplicate "graphql" modules cannot be used at the same time since different

0 commit comments

Comments
 (0)