Skip to content

Commit

Permalink
skip empty schema files
Browse files Browse the repository at this point in the history
skip empty document files
  • Loading branch information
dotansimha committed Feb 3, 2019
1 parent cf5457c commit aba5eb3
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 31 deletions.
5 changes: 5 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@ module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testPathIgnorePatterns: ['/node_modules/', '/test-assets'],
globals: {
'ts-jest': {
diagnostics: false,
}
}
};
6 changes: 6 additions & 0 deletions src/loaders/documents/documents-from-glob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ export class DocumentsFromGlob implements DocumentLoader {
const fileContent = readFileSync(filePath, 'utf8');
const fileExt = extname(filePath);

if (!fileContent || fileContent.trim() === '') {
console['warn'](`Empty file found: "${filePath}", skipping...`);

return null;
}

if (graphQLExtensions.includes(fileExt)) {
return parse(new Source(fileContent, filePath));
}
Expand Down
50 changes: 19 additions & 31 deletions src/loaders/schema/schema-from-typedefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import * as isValidPath from 'is-valid-path';
import { DocumentNode, parse, Source } from 'graphql';
import * as glob from 'glob';
import { readFileSync } from 'fs';
import { extname } from 'path';
import { extractDocumentStringFromCodeFile } from '../../utils/extract-document-string-from-code-file';

export const graphQLExtensions = ['.graphql', '.graphqls', '.gql'];
Expand All @@ -15,23 +14,27 @@ function isGraphQLFile(globPath: string): boolean {
}

function loadSchemaFile(filepath: string): string {
const content = readFileSync(filepath, {
encoding: 'utf-8',
});
const content = readFileSync(filepath, 'utf-8');

if (/^\# import /i.test(content.trimLeft())) {
const { importSchema } = require('graphql-import');
if (content && content.trim() !== '') {
if (/^\#.*import /i.test(content.trimLeft())) {
const { importSchema } = require('graphql-import');

return importSchema(filepath);
}
return importSchema(filepath);
}

const foundDoc = extractDocumentStringFromCodeFile(new Source(content, filepath));

const foundDoc = extractDocumentStringFromCodeFile(new Source(content, filepath));
if (foundDoc) {
return foundDoc;
}

if (foundDoc) {
return foundDoc;
return content;
} else {
console['warn'](`Empty schema file found: "${filepath}", skipping...`);
}

return content;
return null;
}

export class SchemaFromTypedefs implements SchemaLoader {
Expand All @@ -46,27 +49,12 @@ export class SchemaFromTypedefs implements SchemaLoader {
throw new Error(`Unable to find matching files for glob: ${globPath} in directory: ${process.cwd()}`);
}

if (globFiles.length > 1) {
return mergeGraphQLSchemas(globFiles.map(filePath => this.loadFileContent(filePath)).filter(f => f));
} else {
return parse(loadSchemaFile(globFiles[0]));
}
}

loadFileContent(filePath: string): Source {
const fileContent = readFileSync(filePath, 'utf8');
const fileExt = extname(filePath);

if (graphQLExtensions.includes(fileExt)) {
return new Source(fileContent, filePath);
}

const foundDoc = extractDocumentStringFromCodeFile(new Source(fileContent, filePath));
const filesContent = globFiles.map(filePath => loadSchemaFile(filePath)).filter(f => f);

if (foundDoc) {
return new Source(foundDoc, filePath);
if (filesContent.length === 0) {
throw new Error(`All found files for glob expression "${globPath}" are not valid or empty, please check it and try again!`);
}

return null;
return mergeGraphQLSchemas(filesContent);
}
}
34 changes: 34 additions & 0 deletions tests/loaders/documents/documents-from-glob.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { join } from 'path';
import { DocumentsFromGlob } from '../../../src/loaders/documents/documents-from-glob';

describe('documentsFromGlob', () => {
it('Should load one GraphQL document from glob expression', async () => {
const glob = join(__dirname, './test-files/', '*.query.graphql');
const handler = new DocumentsFromGlob();
const canHandle = handler.canHandle(glob);
expect(canHandle).toBeTruthy();
const result = await handler.handle(glob);
expect(result.length).toBe(1);
expect(result[0].content).toBeDefined();
});

it('Should load multiple GraphQL document from glob expression', async () => {
const glob = join(__dirname, './test-files/', '*.graphql');
const handler = new DocumentsFromGlob();
const canHandle = handler.canHandle(glob);
expect(canHandle).toBeTruthy();
const result = await handler.handle(glob);
expect(result.length).toBe(2);
expect(result[0].content).toBeDefined();
expect(result[1].content).toBeDefined();
});

it('Should ignore empty files', async () => {
const glob = join(__dirname, './test-files/', '*.empty.graphql');
const handler = new DocumentsFromGlob();
const canHandle = handler.canHandle(glob);
expect(canHandle).toBeTruthy();
const result = await handler.handle(glob);
expect(result.length).toBe(0);
});
});
3 changes: 3 additions & 0 deletions tests/loaders/documents/test-files/1.query.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
query {
a
}
3 changes: 3 additions & 0 deletions tests/loaders/documents/test-files/2.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
query {
b
}
Empty file.
10 changes: 10 additions & 0 deletions tests/loaders/schema/schema-from-typedefs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ describe('schema from typedefs', () => {
expect(schema.getTypeMap()['Query']).toBeDefined();
});

it('should ignore empty files when using glob expressions', () => {
const glob = './tests/loaders/schema/test-files/schema-dir/*.empty.graphql';
const handler = new SchemaFromTypedefs();
const canHandle = handler.canHandle(glob);
expect(canHandle).toBeTruthy();
expect(() => {
handler.handle(glob);
}).toThrow(`All found files for glob expression "./tests/loaders/schema/test-files/schema-dir/*.empty.graphql" are not valid or empty, please check it and try again!`);
});

it('should work with graphql-tag', () => {
const schemaPath = './tests/loaders/schema/test-files/schema-dir/*.ts';
const handler = new SchemaFromTypedefs();
Expand Down
Empty file.

0 comments on commit aba5eb3

Please sign in to comment.