Skip to content

Commit

Permalink
Dependency maintenance
Browse files Browse the repository at this point in the history
- Upgrade everything
- Swap del for rimraf, as del is ESM only and old versions have vulnerabilities (e.g. via https://security.snyk.io/vuln/SNYK-JS-INFLIGHT-6095116).
- Fix bug with unions resulting in infinite recursion except for depthLimit. I came across this while manually testing the changes here
in one of my repos - I think it was an unintended consequence of #44, and the test introduced in that PR
still passes, so I think my small change here is likely ok?
  • Loading branch information
AaronMoat committed Jan 7, 2024
1 parent 681c19d commit 697aca4
Show file tree
Hide file tree
Showing 6 changed files with 3,195 additions and 2,652 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish-npm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- uses: actions/checkout@v1
- uses: actions/setup-node@v1
with:
node-version: 12
node-version: 18
- run: npm install
- run: npm test
- uses: JS-DevTools/npm-publish@v1
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:

strategy:
matrix:
node-version: [12.x, 14.x, 16.x]
node-version: [18.x, 20.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
Expand Down
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 2.0.0 - 2024-01-08

There are no interface changes in this release, but the minimum required Node.js version is now 18.

### Changes

- BREAKING: Require Node.js 18 or higher.
- Fix issue where includeCrossReferences would incorrectly behave as if it was set to false in the presence of unions.
- Upgrade dependencies: `commander`, `graphql` to latest
- Remove dependency `del`, instead `rimraf` is now used.
52 changes: 35 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ const fs = require('fs');
const path = require('path');
const program = require('commander');
const { Source, buildSchema } = require('graphql');
const del = require('del');
const { rimrafSync } = require('rimraf');

function main ({
function main({
schemaFilePath,
destDirPath,
depthLimit = 100,
Expand All @@ -23,7 +23,7 @@ function main ({
const source = new Source(typeDef);
const gqlSchema = buildSchema(source, { assumeValidSDL: assume });

del.sync(destDirPath);
rimrafSync(destDirPath);
path.resolve(destDirPath).split(path.sep).reduce((before, cur) => {
const pathTmp = path.join(before, cur + path.sep);
if (!fs.existsSync(pathTmp)) {
Expand Down Expand Up @@ -61,15 +61,15 @@ function main ({
* Generate variables string
* @param dict dictionary of arguments
*/
const getArgsToVarsStr = dict => Object.entries(dict)
const getArgsToVarsStr = (dict) => Object.entries(dict)
.map(([varName, arg]) => `${arg.name}: $${varName}`)
.join(', ');

/**
* Generate types string
* @param dict dictionary of arguments
*/
const getVarsToTypesStr = dict => Object.entries(dict)
const getVarsToTypesStr = (dict) => Object.entries(dict)
.map(([varName, arg]) => `$${varName}: ${arg.type}`)
.join(', ');

Expand Down Expand Up @@ -108,19 +108,25 @@ function main ({
) {
return '';
}
if (!fromUnion) {
crossReferenceKeyList.push(crossReferenceKey);
}
crossReferenceKeyList.push(crossReferenceKey);
const childKeys = Object.keys(curType.getFields());
childQuery = childKeys
.filter((fieldName) => {
/* Exclude deprecated fields */
const fieldSchema = gqlSchema.getType(curType).getFields()[fieldName];
return includeDeprecatedFields || !fieldSchema.deprecationReason;
})
.map(cur => generateQuery(cur, curType, curName, argumentsDict, duplicateArgCounts,
crossReferenceKeyList, curDepth + 1, fromUnion).queryStr)
.filter(cur => Boolean(cur))
.map((cur) => generateQuery(
cur,
curType,
curName,
argumentsDict,
duplicateArgCounts,
crossReferenceKeyList,
curDepth + 1,
fromUnion,
).queryStr)
.filter((cur) => Boolean(cur))
.join('\n');
}

Expand All @@ -143,15 +149,23 @@ function main ({
const indent = `${' '.repeat(curDepth)}`;
const fragIndent = `${' '.repeat(curDepth + 1)}`;
queryStr += '{\n';
queryStr += `${fragIndent}__typename\n`
queryStr += `${fragIndent}__typename\n`;

for (let i = 0, len = types.length; i < len; i++) {
const valueTypeName = types[i];
const valueType = gqlSchema.getType(valueTypeName);
const unionChildQuery = Object.keys(valueType.getFields())
.map(cur => generateQuery(cur, valueType, curName, argumentsDict, duplicateArgCounts,
crossReferenceKeyList, curDepth + 2, true).queryStr)
.filter(cur => Boolean(cur))
.map((cur) => generateQuery(
cur,
valueType,
curName,
argumentsDict,
duplicateArgCounts,
crossReferenceKeyList,
curDepth + 2,
true,
).queryStr)
.filter((cur) => Boolean(cur))
.join('\n');

/* Exclude empty unions */
Expand Down Expand Up @@ -249,18 +263,22 @@ function main ({
fs.writeFileSync(path.join(destDirPath, 'index.js'), indexJsExportAll);
}

module.exports = main
module.exports = main;

if (require.main === module) {
program
.name('gqlg')
.option('--schemaFilePath [value]', 'path of your graphql schema file')
.option('--destDirPath [value]', 'dir you want to store the generated queries')
.option('--depthLimit [value]', 'query depth you want to limit (The default is 100)')
.option('--assumeValid [value]', 'assume the SDL is valid (The default is false)')
.option('--ext [value]', 'extension file to use', 'gql')
.option('-C, --includeDeprecatedFields [value]', 'Flag to include deprecated fields (The default is to exclude)')
.option('-R, --includeCrossReferences', 'Flag to include fields that have been added to parent queries already (The default is to exclude)')
.showHelpAfterError()
.parse(process.argv);

return main({...program, fileExtension: program.ext })
const { ext, ...opts } = program.opts();

main({ ...opts, fileExtension: ext });
}
Loading

0 comments on commit 697aca4

Please sign in to comment.