Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dependency maintenance #76

Merged
merged 1 commit into from
Jan 9, 2024
Merged
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
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({
Copy link
Contributor Author

Choose a reason for hiding this comment

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

All formatting changes were done by eslint upgrades and running lint --fix.

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) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the bug fix I mentioned.

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
Loading