Skip to content
This repository has been archived by the owner on Aug 20, 2020. It is now read-only.

Adding ability to process js, graphql, and other arbitrary file types (e.g. .gql) #51

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/ExtractGQL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class ExtractGQL {
public queryTransformers: QueryTransformer[] = [];

// The file extension to load queries from
public extension: string;
public extension: Array<string> = ['graphql'];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should rename this to be a plural thing rather than leave it "extension" since it is now an array

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only reason I didn't change this is the CLI arg is extension - and since renaming the arg would be a breaking change, elected to go with consistency.

Obv easy / happy to do this, but wanted to explain my thinking.


// Whether to look for standalone .graphql files or template literals in JavaScript code
public inJsCode: boolean = false;
Expand Down Expand Up @@ -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()))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of doing this, we should just have a this.extensions that we can pass in.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you clarify this? If it's coming from the CLI how else do you propose I pass in an array of things? I could just let the variable be a string, and split it later?

this.inJsCode = inJsCode;
}

Expand Down Expand Up @@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, should just be able to an array "contains" check rather than doing indexing on a string

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume you mean array.includes(val) - the reason I didn't do this is the tsconfig.json is set for es6, and array.includes(val) is part of the es7 lib. Do you want me to include that?

if (this.inJsCode && extension === 'js') {
// Read from a JS file
return ExtractGQL.readFile(inputFile).then((result) => {
const literalContents = findTaggedTemplateLiteralsInJS(result, this.literalTag);
Expand Down
14 changes: 14 additions & 0 deletions test/fixtures/arbitrary_filetypes/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import firstName from './fragment.gql';
import lastName from './fragment.graphql';

const query = gql`
query {
author {
...firstName
...lastName
}
}

${firstName}
${lastName}
`;
3 changes: 3 additions & 0 deletions test/fixtures/arbitrary_filetypes/fragment.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fragment firstName on Author {
firstName
}
3 changes: 3 additions & 0 deletions test/fixtures/arbitrary_filetypes/fragment.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fragment lastName on Author {
lastName
}
19 changes: 19 additions & 0 deletions test/fixtures/multiple_filetypes/code.js
Original file line number Diff line number Diff line change
@@ -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}
`;
3 changes: 3 additions & 0 deletions test/fixtures/multiple_filetypes/fragment.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fragment lastName on Author {
lastName
}
68 changes: 68 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down