Skip to content

Commit

Permalink
should not throw pattern is too long from minimatch dependency when…
Browse files Browse the repository at this point in the history
… SDL schema contain more than 65536 characters (#1418)

* aa

* aa

* aa
  • Loading branch information
dimaMachina authored Aug 3, 2024
1 parent 5eca929 commit 658f984
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/seven-steaks-melt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'graphql-config': patch
---

should not throw `pattern is too long` from minimatch dependency when SDL schema contain more than 65536 characters
10 changes: 9 additions & 1 deletion src/project-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,11 @@ export class GraphQLProjectConfig {
}
}

// XXX: it works but uses nodejs - expose normalization of file and dir paths in config
function isSDLSchemaLike(schema: string): boolean {
return schema.includes('\n');
}

// XXX: it works but uses Node.js - expose normalization of file and dir paths in config
function match(filepath: string, dirpath: string, pointer?: Pointer): boolean {
if (!pointer) {
return false;
Expand All @@ -202,6 +206,10 @@ function match(filepath: string, dirpath: string, pointer?: Pointer): boolean {
}

if (typeof pointer === 'string') {
if (isSDLSchemaLike(pointer)) {
return false;
}

const normalizedFilepath = normalize(isAbsolute(filepath) ? relative(dirpath, filepath) : filepath);
return minimatch(normalizedFilepath, normalize(pointer), { dot: true });
}
Expand Down
15 changes: 13 additions & 2 deletions test/config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { buildSchema, buildASTSchema } from 'graphql';
import path from 'path';
import { TempDir } from './utils/temp-dir';
import { runTests } from './utils/runner';
import { loadConfig, loadConfigSync, ConfigNotFoundError } from 'graphql-config';
import { beforeEach, beforeAll, test, describe, expect, afterAll } from 'vitest';
import { loadConfig, loadConfigSync, ConfigNotFoundError, GraphQLConfig } from 'graphql-config';

const temp = new TempDir();

Expand Down Expand Up @@ -278,3 +277,15 @@ runTests({ async: loadConfig, sync: loadConfigSync })((load, mode) => {
});
});
});

describe('GraphQLConfig', () => {
const MINIMATCH_MAX_LENGTH = 65_536;

// https://github.com/dimaMachina/graphql-eslint/issues/2046
it(`should not throw \`pattern is too long\` from minimatch dependency when SDL schema contain more than ${MINIMATCH_MAX_LENGTH} characters`, async () => {
const schema = Array.from({ length: 2_150 }, (_, i) => `type Query${i} { foo: String }`).join('\n');
const graphQLConfig = new GraphQLConfig({ config: { schema }, filepath: '' }, []);
expect(schema.length).toBeGreaterThan(MINIMATCH_MAX_LENGTH);
expect(() => graphQLConfig.getProjectForFile('foo')).not.toThrow();
});
});
2 changes: 1 addition & 1 deletion test/loaders.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DirectiveDefinitionNode, buildSchema, GraphQLSchema, Kind } from 'graphql';
import { Loader, Source } from '@graphql-tools/utils';
import { beforeAll, test, describe, expect, vi, Mock } from 'vitest';
import { LoadersRegistry } from 'graphql-config';
import { Mock } from 'vitest';
import { loadTypedefsSync, loadSchemaSync, loadSchema, LoadSchemaOptions } from '@graphql-tools/load';

vi.mock('@graphql-tools/load', async () => {
Expand Down
6 changes: 2 additions & 4 deletions test/utils/runner.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { describe } from 'vitest';

type PromiseOf<T extends (...args: any[]) => any> = T extends (...args: any[]) => Promise<infer R> ? R : ReturnType<T>;

export function runTests<
Expand Down Expand Up @@ -27,8 +25,8 @@ export function runTests<
}, 'sync');
});
// async
describe(`async`, () => {
testRunner((...args) => executeAsync(...args) as any, 'async');
describe('async', () => {
testRunner(executeAsync as any, 'async');
});
};
}
8 changes: 4 additions & 4 deletions test/utils/temp-dir.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import path from 'path';
import path from 'node:path';
import { deleteSync } from 'del';
import makeDir from 'make-dir';
import parentModule from 'parent-module';
import os from 'os';
import { Mock, SpyInstance } from 'vitest';
import os from 'node:os';
import type { Mock, MockInstance } from 'vitest';
import fs from 'node:fs';

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

getSpyPathCalls(spy: Mock | SpyInstance): string[] {
getSpyPathCalls(spy: Mock | MockInstance): string[] {
const calls = spy.mock.calls;

const result = calls.map((call): string => {
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"noUnusedLocals": true,
"noUnusedParameters": true,
"resolveJsonModule": true,
"types": ["vitest/globals"],
"paths": {
"graphql-config": ["./src/index.ts"]
}
Expand Down
1 change: 1 addition & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const CWD = process.cwd();

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

0 comments on commit 658f984

Please sign in to comment.