format-graphql
Advanced tools
Comparing version 1.3.1 to 1.3.2
@@ -8,4 +8,2 @@ "use strict"; | ||
var _mapObj = _interopRequireDefault(require("map-obj")); | ||
var _graphql = require("graphql"); | ||
@@ -15,4 +13,2 @@ | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
const sortSchema = (key, value, options) => { | ||
@@ -26,3 +22,3 @@ const { | ||
if (sortDefinitions && key === 'definitions' || sortFields && key === 'fields' || sortArguments && key === 'arguments') { | ||
return [key, value.slice().sort((a, b) => { | ||
return value.slice().sort((a, b) => { | ||
if (a.kind === 'SchemaDefinition') { | ||
@@ -37,22 +33,66 @@ return -1; | ||
return a.name.value.localeCompare(b.name.value); | ||
})]; | ||
}); | ||
} | ||
return [key, value]; | ||
return value; | ||
}; | ||
/** | ||
* We only care about rearranging: | ||
* - definitions | ||
* - fields | ||
* - arguments | ||
* | ||
* A GraphQL Schema AST looks something like this: | ||
* | ||
* { | ||
* "definitions": [ | ||
* { | ||
* fields: [ | ||
* { | ||
* arguments: [ | ||
* ... | ||
* ] | ||
* } | ||
* ... | ||
* ], | ||
* }, | ||
* ... | ||
* ] | ||
* } | ||
* | ||
* Note that there are no cycles - we don't need to recurse through the whole | ||
* AST. There's a finite nest depth of 3 node types for us to walk down: | ||
* | ||
* <start> -> definitions -> fields -> arguments | ||
*/ | ||
const formatSdl = (schemaSdl, options) => { | ||
return (0, _graphql.print)((0, _mapObj.default)((0, _graphql.parse)(schemaSdl), (key, value) => { | ||
if (['definitions', 'fields', 'arguments'].includes(key) && Array.isArray(value)) { | ||
return sortSchema(key, value, (0, _optionalize.getOptions)(options)); | ||
} | ||
return [key, value]; | ||
}, { | ||
deep: true | ||
})); | ||
const walkAST = (node, options, key) => { | ||
// Map a node type to the child node type we should walk down next | ||
const nextKey = { | ||
arguments: null, | ||
definitions: 'fields', | ||
fields: 'arguments' | ||
}; | ||
if (!key) { | ||
return node; | ||
} | ||
if (!Array.isArray(node[key])) { | ||
return node; | ||
} | ||
node[key] = sortSchema(key, node[key], options).map(child => { | ||
return walkAST(child, options, nextKey[key]); | ||
}); | ||
return node; | ||
}; | ||
const formatSdl = (schemaSdl, options) => { | ||
return (0, _graphql.print)(walkAST((0, _graphql.parse)(schemaSdl), (0, _optionalize.getOptions)(options), 'definitions')); | ||
}; | ||
var _default = formatSdl; | ||
exports.default = _default; | ||
//# sourceMappingURL=formatSdl.js.map |
@@ -25,3 +25,2 @@ { | ||
"graphql": "^15.1.0", | ||
"map-obj": "^4.1.0", | ||
"yargs": "^15.3.1" | ||
@@ -91,3 +90,3 @@ }, | ||
}, | ||
"version": "1.3.1" | ||
"version": "1.3.2" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
25333
2
178
- Removedmap-obj@^4.1.0
- Removedmap-obj@4.3.0(transitive)