Skip to content

Commit 4dfa5c2

Browse files
authored
fix github, json, apollo and prisma loader (#3189)
* sfix: skip pointers that cannot be loaded * fix: json file loader * fix prisma and apollo loaders
1 parent e9fa613 commit 4dfa5c2

File tree

6 files changed

+36
-6
lines changed

6 files changed

+36
-6
lines changed

packages/loaders/apollo-engine/src/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ export class ApolloEngineLoader implements Loader<ApolloEngineOptions> {
5858
return typeof ptr === 'string' && ptr === 'apollo-engine';
5959
}
6060

61-
async load(pointer: 'apollo-engine', options: ApolloEngineOptions): Promise<Source[]> {
61+
async load(pointer: string, options: ApolloEngineOptions): Promise<Source[]> {
62+
if (!(await this.canLoad(pointer))) {
63+
return [];
64+
}
6265
const fetchArgs = this.getFetchArgs(options);
6366
const response = await fetch(...fetchArgs);
6467

@@ -72,7 +75,10 @@ export class ApolloEngineLoader implements Loader<ApolloEngineOptions> {
7275
return [source];
7376
}
7477

75-
loadSync(pointer: 'apollo-engine', options: ApolloEngineOptions): Source[] {
78+
loadSync(pointer: string, options: ApolloEngineOptions): Source[] {
79+
if (!this.canLoadSync(pointer)) {
80+
return [];
81+
}
7682
const fetchArgs = this.getFetchArgs(options);
7783
const response = syncFetch(...fetchArgs);
7884

packages/loaders/github/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ export class GithubLoader implements Loader<GithubLoaderOptions> {
6060
}
6161

6262
async load(pointer: string, options: GithubLoaderOptions): Promise<Source[]> {
63+
if (!(await this.canLoad(pointer))) {
64+
return [];
65+
}
6366
const { owner, name, ref, path } = extractData(pointer);
6467
const request = await fetch('https://api.github.com/graphql', {
6568
method: 'POST',

packages/loaders/github/tests/schema-from-github.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,12 @@ test('load schema from GitHub', async () => {
9898
// schema
9999
expect(print(source.document)).toEqual(printSchema(buildSchema(typeDefs)));
100100
});
101+
102+
test('simply skips schema for path that cannot be loaded', async () => {
103+
const loader = new GithubLoader();
104+
105+
const result = await loader.load("./test/123", {
106+
token,
107+
});
108+
expect(result).toEqual([])
109+
})

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ export class JsonFileLoader implements Loader {
6060
if (isValidPath(pointer)) {
6161
if (FILE_EXTENSIONS.find(extension => pointer.endsWith(extension))) {
6262
const normalizedFilePath = isAbsolute(pointer) ? pointer : resolve(options.cwd || cwd(), pointer);
63-
6463
return existsSync(normalizedFilePath);
6564
}
6665
}
@@ -70,6 +69,9 @@ export class JsonFileLoader implements Loader {
7069

7170
async load(pointer: string, options: JsonFileLoaderOptions): Promise<Source[]> {
7271
const normalizedFilePath = isAbsolute(pointer) ? pointer : resolve(options.cwd || cwd(), pointer);
72+
if (!(await this.canLoad(normalizedFilePath, options))) {
73+
return [];
74+
}
7375

7476
try {
7577
const jsonContent: string = await readFile(normalizedFilePath, { encoding: 'utf8' });
@@ -80,13 +82,16 @@ export class JsonFileLoader implements Loader {
8082
}
8183

8284
loadSync(pointer: string, options: JsonFileLoaderOptions): Source[] {
83-
const normalizedFilepath = isAbsolute(pointer) ? pointer : resolve(options.cwd || cwd(), pointer);
85+
const normalizedFilePath = isAbsolute(pointer) ? pointer : resolve(options.cwd || cwd(), pointer);
86+
if (!this.canLoadSync(normalizedFilePath, options)) {
87+
return [];
88+
}
8489

8590
try {
86-
const jsonContent = readFileSync(normalizedFilepath, 'utf8');
91+
const jsonContent = readFileSync(normalizedFilePath, 'utf8');
8792
return [parseGraphQLJSON(pointer, jsonContent, options)];
8893
} catch (e) {
89-
throw new Error(`Unable to read JSON file: ${normalizedFilepath}: ${e.message || /* istanbul ignore next */ e}`);
94+
throw new Error(`Unable to read JSON file: ${normalizedFilePath}: ${e.message || /* istanbul ignore next */ e}`);
9095
}
9196
}
9297
}

packages/loaders/json-file/tests/loader.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ describe('JsonFileLoader', () => {
6565
it('should throw when the file content is malformed', async () => {
6666
await expect(load(getPointer('malformed.json'), {})).rejects.toThrowError('Unable to read JSON file');
6767
});
68+
it('should skip file it cannot load', async () => {
69+
const result = await load(getPointer('id_do_not_exist.json'), {});
70+
expect(result).toEqual([])
71+
})
6872
});
6973
});
7074
});

packages/loaders/prisma/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ export class PrismaLoader extends UrlLoader {
4242
}
4343

4444
async load(prismaConfigFilePath: string, options: PrismaLoaderOptions) {
45+
if (!(await this.canLoad(prismaConfigFilePath, options))) {
46+
return [];
47+
}
4548
const { graceful, envVars } = options;
4649
const home = homedir();
4750
const env = new Environment(home);

0 commit comments

Comments
 (0)