From 125baaff18a9b1f7f7b10cf8b72c9b0a0e417290 Mon Sep 17 00:00:00 2001 From: Scott Silvi Date: Thu, 26 Oct 2017 12:04:57 -0700 Subject: [PATCH] Adding ability to process js, graphql, and other arbitrary file types (e.g. gql files) --- src/ExtractGQL.ts | 10 +-- test/fixtures/arbitrary_filetypes/code.js | 14 ++++ .../fixtures/arbitrary_filetypes/fragment.gql | 3 + .../arbitrary_filetypes/fragment.graphql | 3 + test/fixtures/multiple_filetypes/code.js | 19 ++++++ .../multiple_filetypes/fragment.graphql | 3 + test/index.ts | 68 +++++++++++++++++++ 7 files changed, 115 insertions(+), 5 deletions(-) create mode 100644 test/fixtures/arbitrary_filetypes/code.js create mode 100644 test/fixtures/arbitrary_filetypes/fragment.gql create mode 100644 test/fixtures/arbitrary_filetypes/fragment.graphql create mode 100644 test/fixtures/multiple_filetypes/code.js create mode 100644 test/fixtures/multiple_filetypes/fragment.graphql diff --git a/src/ExtractGQL.ts b/src/ExtractGQL.ts index cf1f93f..97e0c1b 100644 --- a/src/ExtractGQL.ts +++ b/src/ExtractGQL.ts @@ -61,7 +61,7 @@ export class ExtractGQL { public queryTransformers: QueryTransformer[] = []; // The file extension to load queries from - public extension: string; + public extension: Array = ['graphql']; // Whether to look for standalone .graphql files or template literals in JavaScript code public inJsCode: boolean = false; @@ -109,13 +109,13 @@ export class ExtractGQL { inputFilePath, outputFilePath = 'extracted_queries.json', queryTransformers = [], - extension = 'graphql', + extension = '', inJsCode = false, }: ExtractGQLOptions) { this.inputFilePath = inputFilePath; this.outputFilePath = outputFilePath; this.queryTransformers = queryTransformers; - this.extension = extension; + this.extension = this.extension.concat(extension.split(',').map(e => e.trim())) this.inJsCode = inJsCode; } @@ -193,8 +193,8 @@ export class ExtractGQL { return Promise.resolve().then(() => { const extension = ExtractGQL.getFileExtension(inputFile); - if (extension === this.extension) { - if (this.inJsCode) { + if (this.extension.indexOf(extension) > -1) { + if (this.inJsCode && extension === 'js') { // Read from a JS file return ExtractGQL.readFile(inputFile).then((result) => { const literalContents = findTaggedTemplateLiteralsInJS(result, this.literalTag); diff --git a/test/fixtures/arbitrary_filetypes/code.js b/test/fixtures/arbitrary_filetypes/code.js new file mode 100644 index 0000000..76b7f2f --- /dev/null +++ b/test/fixtures/arbitrary_filetypes/code.js @@ -0,0 +1,14 @@ +import firstName from './fragment.gql'; +import lastName from './fragment.graphql'; + +const query = gql` +query { + author { + ...firstName + ...lastName + } +} + +${firstName} +${lastName} +`; diff --git a/test/fixtures/arbitrary_filetypes/fragment.gql b/test/fixtures/arbitrary_filetypes/fragment.gql new file mode 100644 index 0000000..5438b28 --- /dev/null +++ b/test/fixtures/arbitrary_filetypes/fragment.gql @@ -0,0 +1,3 @@ +fragment firstName on Author { + firstName +} diff --git a/test/fixtures/arbitrary_filetypes/fragment.graphql b/test/fixtures/arbitrary_filetypes/fragment.graphql new file mode 100644 index 0000000..56205ae --- /dev/null +++ b/test/fixtures/arbitrary_filetypes/fragment.graphql @@ -0,0 +1,3 @@ +fragment lastName on Author { + lastName +} diff --git a/test/fixtures/multiple_filetypes/code.js b/test/fixtures/multiple_filetypes/code.js new file mode 100644 index 0000000..268627a --- /dev/null +++ b/test/fixtures/multiple_filetypes/code.js @@ -0,0 +1,19 @@ +import lastName from './fragment.graphql'; + +const frag = gql` +fragment firstName on Author { + firstName +} +`; + +const query = gql` +query { + author { + ...firstName + ...lastName + } +} + +${frag} +${lastName} +`; diff --git a/test/fixtures/multiple_filetypes/fragment.graphql b/test/fixtures/multiple_filetypes/fragment.graphql new file mode 100644 index 0000000..56205ae --- /dev/null +++ b/test/fixtures/multiple_filetypes/fragment.graphql @@ -0,0 +1,3 @@ +fragment lastName on Author { + lastName +} diff --git a/test/index.ts b/test/index.ts index 78e9b58..a914c16 100644 --- a/test/index.ts +++ b/test/index.ts @@ -446,6 +446,74 @@ describe('ExtractGQL', () => { ); }); }); + + it('should process both a JS file and a graphql file', () => { + const expectedQuery = gql` + query { + author { + ...firstName + ...lastName + } + } + fragment firstName on Author { + firstName + } + fragment lastName on Author { + lastName + } + `; + + const jsEgql = new ExtractGQL({ + inputFilePath: 'idk', + extension: 'js', + inJsCode: true, + outputFilePath: 'idk', + }); + + return jsEgql.processInputPath('./test/fixtures/multiple_filetypes') + .then((result: OutputMap) => { + const keys = Object.keys(result); + assert.equal(keys.length, 1); + assert.equal( + keys[0], + print(expectedQuery) + ); + }); + }); + + it('should process arbitrary file extensions', () => { + const expectedQuery = gql` + query { + author { + ...firstName + ...lastName + } + } + fragment firstName on Author { + firstName + } + fragment lastName on Author { + lastName + } + `; + + const jsEgql = new ExtractGQL({ + inputFilePath: 'idk', + extension: 'js,gql', + inJsCode: true, + outputFilePath: 'idk', + }); + + return jsEgql.processInputPath('./test/fixtures/arbitrary_filetypes') + .then((result: OutputMap) => { + const keys = Object.keys(result); + assert.equal(keys.length, 1); + assert.equal( + keys[0], + print(expectedQuery) + ); + }); + }); it('should process a JS file with queries', () => { const expectedQuery = gql`