@coderich/graphql-shape
Advanced tools
Comparing version 1.0.0 to 1.1.0
{ | ||
"name": "@coderich/graphql-shape", | ||
"main": "src/GraphQLShape.js", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"publishConfig": { | ||
@@ -18,3 +18,3 @@ "access": "public" | ||
"dependencies": { | ||
"@coderich/util": "0.1.15", | ||
"@coderich/util": "0.1.16", | ||
"jsonpath-plus": "^8.0.0" | ||
@@ -21,0 +21,0 @@ }, |
@@ -20,2 +20,3 @@ const Util = require('@coderich/util'); | ||
// Parse out directives while building transforms | ||
const query = print(visit(ast, { | ||
@@ -102,24 +103,41 @@ enter: (node) => { | ||
// Finalizations due to unpredictable order for AST | ||
thunks.forEach(thunk => thunk()); | ||
return { query, transforms: transforms.reverse(), fragments }; | ||
// Preprocess transforms meta-data (for better performance) | ||
transforms.reverse().forEach((transform) => { | ||
if (transform.map) { | ||
transform.map = Util.ensureArray(transform.map).map((mix) => { | ||
const [, name = mix, args = ''] = mix.match(/(\w+)\s*\((.*?)\)|(.*)/); | ||
return { name, args: args.split(',').map(el => el.trim()) }; | ||
}); | ||
} | ||
}); | ||
return { query, transforms, fragments }; | ||
} | ||
static transform(data, transforms = []) { | ||
const hoisted = []; | ||
// Apply transformations (in place) | ||
transforms.forEach(({ key, path = '$', ...rest }) => { | ||
Util.pathmap(key, data, (json) => { | ||
Util.pathmap(key, data, (json, info) => { | ||
let value = JSONPath({ path, json, wrap: false }); | ||
// Apply the rest (in the order they are defined!) | ||
if (value !== null) { | ||
if (value != null) { | ||
Object.entries(rest).forEach(([fn, mixed]) => { | ||
switch (fn) { | ||
case 'map': { | ||
Util.map(mixed, (mix) => { | ||
const [, name = mix, args = ''] = mix.match(/(\w+)\s*\((.*?)\)|(.*)/); | ||
const $args = args.split(',').map(el => el.trim()); | ||
value = Util.map(value, v => GraphQLShape.#resolveValueFunction(v, name, $args)); | ||
Util.map(mixed, ({ name, args }) => { | ||
value = Util.map(value, v => GraphQLShape.#resolveValueFunction(v, name, args)); | ||
}); | ||
break; | ||
} | ||
case 'hoist': { | ||
if (!mixed) hoisted.push(info); | ||
Object.assign(info.parent, value); | ||
break; | ||
} | ||
default: { | ||
@@ -138,2 +156,5 @@ value = GraphQLShape.#resolveValueFunction(value, fn, mixed); | ||
// Delete any hoisted keys | ||
hoisted.forEach(({ key, parent }) => delete parent[key]); | ||
return data; // For convenience (and testing) | ||
@@ -150,2 +171,4 @@ } | ||
case 'NullValue': return null; | ||
case 'IntValue': return parseInt(node.value, 10); | ||
case 'StringValue': return `${node.value}`; | ||
case 'ListValue': return node.values.map(GraphQLShape.#resolveNodeValue); | ||
@@ -152,0 +175,0 @@ case 'EnumValueDefinition': return node.name.value; |
6340
165
+ Added@coderich/util@0.1.16(transitive)
- Removed@coderich/util@0.1.15(transitive)
Updated@coderich/util@0.1.16