Skip to content

Commit bbb5746

Browse files
authored
ensure indent is covered via test (#3162)
* ensure indent is covered * allow providing config via constructor * fix: ensure next loader will be used if first one does not yield a result
1 parent ce9c398 commit bbb5746

File tree

5 files changed

+92
-21
lines changed

5 files changed

+92
-21
lines changed

.changeset/metal-apes-sell.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-tools/code-file-loader': minor
3+
---
4+
5+
allow supplying config via constructor

packages/load/src/load-typedefs/load-file.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Source, Maybe } from '@graphql-tools/utils';
1+
import { Source, Maybe, isSome } from '@graphql-tools/utils';
22
import { env } from 'process';
33
import { LoadTypedefsOptions } from '../load-typedefs';
44

@@ -15,6 +15,9 @@ export async function loadFile(pointer: string, options: LoadTypedefsOptions): P
1515

1616
if (canLoad) {
1717
const loadedValue = await loader.load(pointer, options);
18+
if (!isSome(loadedValue) || loadedValue.length === 0) {
19+
continue;
20+
}
1821
return loadedValue;
1922
}
2023
} catch (error) {
@@ -41,7 +44,11 @@ export function loadFileSync(pointer: string, options: LoadTypedefsOptions): May
4144

4245
if (canLoad) {
4346
// We check for the existence so it is okay to force non null
44-
return loader.loadSync!(pointer, options);
47+
const loadedValue = loader.loadSync!(pointer, options);
48+
if (!isSome(loadedValue) || loadedValue.length === 0) {
49+
continue;
50+
}
51+
return loadedValue;
4552
}
4653
} catch (error) {
4754
if (env['DEBUG']) {

packages/load/tests/loaders/documents/documents-from-glob.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,5 +133,12 @@ describe('documentsFromGlob', () => {
133133
});
134134
expect(result.length).toBe(1);
135135
});
136+
test(`should try next loader if first one fails`, async () => {
137+
const glob = join(__dirname, './test-with-brackets/', '**/*.ts');
138+
const result = await load(glob, {
139+
loaders: [new GraphQLFileLoader(), new CodeFileLoader()],
140+
});
141+
expect(result.length).toBe(1);
142+
})
136143
})
137144
});

packages/loaders/code-file/src/index.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,19 @@ import { createRequire } from 'module';
2626

2727
const { readFile, access } = fsPromises;
2828

29+
export type CodeFileLoaderConfig = {
30+
pluckConfig?: GraphQLTagPluckOptions;
31+
noPluck?: boolean;
32+
noRequire?: boolean;
33+
};
34+
2935
/**
3036
* Additional options for loading from a code file
3137
*/
3238
export type CodeFileLoaderOptions = {
3339
require?: string | string[];
34-
pluckConfig?: GraphQLTagPluckOptions;
35-
noPluck?: boolean;
36-
noRequire?: boolean;
37-
} & BaseLoaderOptions;
40+
} & CodeFileLoaderConfig &
41+
BaseLoaderOptions;
3842

3943
const FILE_EXTENSIONS = ['.ts', '.tsx', '.js', '.jsx', '.vue'];
4044

@@ -57,11 +61,21 @@ function createGlobbyOptions(options: CodeFileLoaderOptions): GlobbyOptions {
5761
* Supported extensions include: `.ts`, `.tsx`, `.js`, `.jsx`, `.vue`
5862
*/
5963
export class CodeFileLoader implements Loader<CodeFileLoaderOptions> {
64+
private config: CodeFileLoaderConfig;
65+
constructor(config?: CodeFileLoaderConfig) {
66+
this.config = config ?? {};
67+
}
68+
69+
private getMergedOptions(options: CodeFileLoaderOptions): CodeFileLoaderOptions {
70+
return { ...this.config, ...options };
71+
}
72+
6073
loaderId(): string {
6174
return 'code-file';
6275
}
6376

6477
async canLoad(pointer: string, options: CodeFileLoaderOptions): Promise<boolean> {
78+
options = this.getMergedOptions(options);
6579
if (isGlob(pointer)) {
6680
// FIXME: parse to find and check the file extensions?
6781
return true;
@@ -83,6 +97,7 @@ export class CodeFileLoader implements Loader<CodeFileLoaderOptions> {
8397
}
8498

8599
canLoadSync(pointer: string, options: CodeFileLoaderOptions): boolean {
100+
options = this.getMergedOptions(options);
86101
if (isGlob(pointer)) {
87102
// FIXME: parse to find and check the file extensions?
88103
return true;
@@ -99,16 +114,19 @@ export class CodeFileLoader implements Loader<CodeFileLoaderOptions> {
99114
}
100115

101116
async resolveGlobs(glob: string, options: CodeFileLoaderOptions) {
117+
options = this.getMergedOptions(options);
102118
const ignores = asArray(options.ignore || []);
103119
return globby([glob, ...ignores.map(v => `!(${v})`).map(v => unixify(v))], createGlobbyOptions(options));
104120
}
105121

106122
resolveGlobsSync(glob: string, options: CodeFileLoaderOptions) {
123+
options = this.getMergedOptions(options);
107124
const ignores = asArray(options.ignore || []);
108125
return globby.sync([glob, ...ignores.map(v => `!(${v})`).map(v => unixify(v))], createGlobbyOptions(options));
109126
}
110127

111128
async load(pointer: string, options: CodeFileLoaderOptions): Promise<Source[] | null> {
129+
options = this.getMergedOptions(options);
112130
if (isGlob(pointer)) {
113131
const resolvedPaths = await this.resolveGlobs(pointer, options);
114132
const finalResult: Source[] = [];
@@ -127,6 +145,7 @@ export class CodeFileLoader implements Loader<CodeFileLoaderOptions> {
127145
}
128146

129147
loadSync(pointer: string, options: CodeFileLoaderOptions): Source[] | null {
148+
options = this.getMergedOptions(options);
130149
if (isGlob(pointer)) {
131150
const resolvedPaths = this.resolveGlobsSync(pointer, options);
132151
const finalResult: Source[] = [];
@@ -143,6 +162,7 @@ export class CodeFileLoader implements Loader<CodeFileLoaderOptions> {
143162
}
144163

145164
async handleSinglePath(location: string, options: CodeFileLoaderOptions): Promise<Source[] | null> {
165+
options = this.getMergedOptions(options);
146166
const normalizedFilePath = ensureAbsolutePath(location, options);
147167

148168
const errors: Error[] = [];
@@ -192,6 +212,7 @@ export class CodeFileLoader implements Loader<CodeFileLoaderOptions> {
192212
}
193213

194214
handleSinglePathSync(location: string, options: CodeFileLoaderOptions): Source[] | null {
215+
options = this.getMergedOptions(options);
195216
const normalizedFilePath = ensureAbsolutePath(location, options);
196217

197218
const errors: Error[] = [];

packages/loaders/code-file/tests/load-from-code-file.spec.ts

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -117,30 +117,61 @@ describe('loadFromCodeFileSync', () => {
117117
it('should support loading many in same file', () => {
118118
const loaded = loader.loadSync('./test-files/multiple-from-file.ts', {
119119
cwd: __dirname,
120+
pluckConfig: {
121+
skipIndent: true,
122+
},
120123
});
121124
expect(loaded?.length).toEqual(3);
122125
expect(loaded?.[0].rawSDL).toBeDefined();
123126
expect(loaded?.[0].rawSDL).toMatchInlineSnapshot(`
124-
"query Foo {
125-
Tweets {
126-
id
127+
"
128+
query Foo {
129+
Tweets {
130+
id
131+
}
127132
}
128-
}"
133+
"
129134
`);
130135
expect(loaded?.[1].rawSDL).toBeDefined();
131136
expect(loaded?.[1].rawSDL).toMatchInlineSnapshot(`
132-
"fragment Lel on Tweet {
133-
id
134-
body
135-
}"
136-
`);
137+
"
138+
fragment Lel on Tweet {
139+
id
140+
body
141+
}
142+
"
143+
`);
137144
expect(loaded?.[2].rawSDL).toBeDefined();
138145
expect(loaded?.[2].rawSDL).toMatchInlineSnapshot(`
139-
"query Bar {
140-
Tweets {
141-
...Lel
142-
}
143-
}"
144-
`);
146+
"
147+
query Bar {
148+
Tweets {
149+
...Lel
150+
}
151+
}
152+
"
153+
`);
145154
});
155+
156+
it('can inherit config options from constructor', () => {
157+
const loader = new CodeFileLoader({
158+
pluckConfig: {
159+
skipIndent: true
160+
}
161+
})
162+
const loaded = loader.loadSync('./test-files/multiple-from-file.ts', {
163+
cwd: __dirname,
164+
});
165+
expect(loaded?.length).toEqual(3);
166+
expect(loaded?.[0].rawSDL).toBeDefined();
167+
expect(loaded?.[0].rawSDL).toMatchInlineSnapshot(`
168+
"
169+
query Foo {
170+
Tweets {
171+
id
172+
}
173+
}
174+
"
175+
`);
176+
})
146177
});

0 commit comments

Comments
 (0)