@coderich/graphql-shape
Advanced tools
Comparing version 2.2.1 to 2.2.2
{ | ||
"name": "@coderich/graphql-shape", | ||
"main": "src/GraphQLShape.js", | ||
"version": "2.2.1", | ||
"version": "2.2.2", | ||
"publishConfig": { | ||
@@ -6,0 +6,0 @@ "access": "public" |
109
README.md
@@ -9,3 +9,3 @@ # GraphQLShape | ||
It includes [JSONPath+](https://www.npmjs.com/package/jsonpath-plus) to help select and transform data to the exact shape required! | ||
It includes [JSONPath+](https://www.npmjs.com/package/jsonpath-plus) to help select and transform data to it's required shape! | ||
@@ -40,3 +40,3 @@ --- | ||
#### Example | ||
### Transformations (by example) | ||
```graphql | ||
@@ -49,6 +49,13 @@ query { | ||
title | ||
author @shape(self: "name") { | ||
name | ||
author @shape(self: "name") { name } | ||
published: publishDate @shape(Date: "new", toISOString: null) | ||
# Must specify "parent" because self/field/compound is made up (removed from query) | ||
compound @_shape(parent: "$[isbn,title]", map: [{ toLowerCase: null }, { replace: [" ", "-"] }, { join: ":" }]) | ||
# Hoist all attributes and remove "detail" | ||
detail @shape(hoist: false) { | ||
summary | ||
rating | ||
} | ||
details @shape(pick: ["summary", "rating"], hoist: false) # Schemaless JSON | ||
} | ||
@@ -59,3 +66,2 @@ } | ||
``` | ||
```json | ||
@@ -68,2 +74,4 @@ { | ||
"author": "Herman Melville", | ||
"published": "1851-10-18T04:56:02.000Z", | ||
"compound": "0-061-96436-0:moby-dick", | ||
"summary": "A legendary tale...", | ||
@@ -78,3 +86,2 @@ "rating": "4.90" | ||
### API | ||
Each transformation falls into 1 of the following lookup tables (referenced in order of preference): | ||
@@ -85,55 +92,51 @@ | ||
--- | --- | --- | ||
`self` | {string\|array} JSONPath | Select from the current field | ||
`parent` | {string\|array} JSONPath | Select from the field's parent | ||
`root` | {string\|array} JSONPath | Select from the root object | ||
`map` | {Object\|AoO} Transform | Iterate field value(s) and apply transformation(s) to each | ||
`assign` | {*} Value | Assign a value to the field | ||
`rename` | {string} Key | Rename the field key | ||
`hoist` | {boolean} Keep | Hoist all field attributes to the parent and optionally delete field | ||
`self` | JSONPath | JSONPath from the current field | ||
`parent` | JSONPath | JSONPath from the field's parent | ||
`root` | JSONPath | JSONPath from the root object | ||
`map` | Transform(s) | Iterate field value(s) and apply transform(s) to each | ||
`assign` | Value | Assign any value to the field | ||
`rename` | Key | Rename the field key | ||
`hoist` | Keep? | Hoist all field attributes to the parent and optionally keep field | ||
#### Core | ||
Javascript core object references. Cannot be re-defined. | ||
name | arg | type | description | example | ||
--- | --- | --- | --- | --- | ||
`*` | Method | String | Invoke a core object method | `Date.now(value, ...args)` | ||
`*` | null | null |Invoke a core object (no method) | `Boolean(value, ...args)` | ||
`*` | "new" | String | Instantiate a core object | `new Array(value, ...args)` | ||
name | arg | description | eg. to produce | ||
--- | --- | --- | --- | ||
`*` | null |Invoke a core object (no method) | `String(value)` | ||
`*` | "new" | Instantiate a core object | `new Array(value)` | ||
`*` | Method | Invoke a core object method | `Date.now(value)` | ||
> Where `*` is one of `[Object, Array, Number, String, Boolean, Symbol, Date, RegExp, Set, Map, WeakMap, WeakSet, Buffer, Math, JSON, Intl]` | ||
#### User | ||
Useful set of transformations. **Can** be re-defined. | ||
name | arg | type | description | ||
--- | --- | --- | --- | ||
`push` | Any | String, Array | `Array.concat` alias | ||
`pop` | null | null | `Array.pop`; return array | ||
`shift` | null | null | `Array.shift`; return array | ||
`unshift` | Any | String, Array | `Array.unshift`; return array | ||
`in` | Values | Array | Return boolean if value in values | ||
`nin` | Values | Array | Return boolean if value not in values | ||
`eq` | Values | Array | Hoist all field attributes to the parent and optionally delete field | ||
`ne` | Keep? | Boolean | Hoist all field attributes to the parent and optionally delete field | ||
`gt` | Keep? | Boolean | Hoist all field attributes to the parent and optionally delete field | ||
`gte` | Keep? | Boolean | Hoist all field attributes to the parent and optionally delete field | ||
`lt` | Keep? | Boolean | Hoist all field attributes to the parent and optionally delete field | ||
`lte` | Keep? | Boolean | Hoist all field attributes to the parent and optionally delete field | ||
`not` | Keep? | Boolean | Hoist all field attributes to the parent and optionally delete field | ||
`or` | Keep? | Boolean | Hoist all field attributes to the parent and optionally delete field | ||
`and` | Keep? | Boolean | Hoist all field attributes to the parent and optionally delete field | ||
`add` | Keep? | Boolean | Hoist all field attributes to the parent and optionally delete field | ||
`sub` | Keep? | Boolean | Hoist all field attributes to the parent and optionally delete field | ||
`div` | Keep? | Boolean | Hoist all field attributes to the parent and optionally delete field | ||
`mul` | Keep? | Boolean | Hoist all field attributes to the parent and optionally delete field | ||
`mod` | Keep? | Boolean | Hoist all field attributes to the parent and optionally delete field | ||
`get` | Keep? | Boolean | Hoist all field attributes to the parent and optionally delete field | ||
`set` | Keep? | Boolean | Hoist all field attributes to the parent and optionally delete field | ||
`nvl` | Keep? | Boolean | Hoist all field attributes to the parent and optionally delete field | ||
`uvl` | Keep? | Boolean | Hoist all field attributes to the parent and optionally delete field | ||
`default` | Keep? | Boolean | Hoist all field attributes to the parent and optionally delete field | ||
`pick` | Keep? | Boolean | Hoist all field attributes to the parent and optionally delete field | ||
`pairs` | Keep? | Boolean | Hoist all field attributes to the parent and optionally delete field | ||
`flatten` | Keep? | Boolean | Hoist all field attributes to the parent and optionally delete field | ||
`unflatten` | Keep? | Boolean | Hoist all field attributes to the parent and optionally delete field | ||
name | arg | description | ||
--- | --- | --- | ||
`push` | Value(s) | Push value(s); return array | ||
`unshift` | Value(s) | Unshift value(s); return array | ||
`in` | Value(s) | Boolean: if value in values | ||
`nin` | Value(s) | Boolean: if value **not** in values | ||
`eq` | [v1, r1, v2, r2, ..., v?] | Return first **r#** if **value === v#**; else **v?** | ||
`ne` | [v1, r1, v2, r2, ..., v?] | Return first **r#** if **value !== v#**; else **v?** | ||
`gt` | [v1, r1, v2, r2, ..., v?] | Return first **r#** if **value > v#**; else **v?** | ||
`gte` | [v1, r1, v2, r2, ..., v?] | Return first **r#** if **value >= v#**; else **v?** | ||
`lt` | [v1, r1, v2, r2, ..., v?] | Return first **r#** if **value < v#**; else **v?** | ||
`lte` | [v1, r1, v2, r2, ..., v?] | Return first **r#** if **value <= v#**; else **v?** | ||
`not` | null | Negate value | ||
`or` | Value(s) | Boolean: if **any** value.concat(values) is truthy | ||
`and` | Value(s) | Boolean: if **all** value.concat(values) are truthy | ||
`add` | Number(s) | Add (sum) | ||
`sub` | Number(s) | Subtract | ||
`div` | Number(s) | Divide | ||
`mul` | Number(s) | Multiply | ||
`mod` | Number(s) | Modulus | ||
`get` | Path(s) | Lodash.get like | ||
`set` | [Key, Value] | Lodash.set like | ||
`nvl` | Value(s) | Return *first* **! === null** value from [value, ...values] | ||
`uvl` | Value(s) | Return *first* **! === undefined** value from [value, ...values] | ||
`default` | Value(s) | Return *first* **! == null** value from [value, ...values] | ||
`pick` | Key(s) | Pick only the key(s) you want from the field/object | ||
`pairs` | null | Transform flat array to 2D elements of 2 (pair) length | ||
`flatten` | * | Flat.flatten like | ||
`unflatten` | * | Flat.unflatten like | ||
#### Value | ||
Lastly, invoke value.key(...args) if function; otherwise return value. | ||
Lastly, invoke `value.key(...args)` if function; otherwise return *value* (noop). | ||
@@ -140,0 +143,0 @@ ### Extension |
@@ -39,7 +39,7 @@ const get = require('lodash.get'); | ||
// Math | ||
exports.add = (v, ...args) => args.flat().reduce((prev, curr) => prev + curr, v); | ||
exports.sub = (v, ...args) => args.flat().reduce((prev, curr) => prev - curr, v); | ||
exports.div = (v, ...args) => args.flat().reduce((prev, curr) => prev / curr, v); | ||
exports.mul = (v, ...args) => args.flat().reduce((prev, curr) => prev * curr, v); | ||
exports.mod = (v, ...args) => args.flat().reduce((prev, curr) => prev % curr, v); | ||
exports.add = (v, ...args) => args.flat().reduce((prev, curr) => prev + Number(curr), Number(v)); | ||
exports.sub = (v, ...args) => args.flat().reduce((prev, curr) => prev - Number(curr), Number(v)); | ||
exports.div = (v, ...args) => args.flat().reduce((prev, curr) => prev / Number(curr), Number(v)); | ||
exports.mul = (v, ...args) => args.flat().reduce((prev, curr) => prev * Number(curr), Number(v)); | ||
exports.mod = (v, ...args) => args.flat().reduce((prev, curr) => prev % Number(curr), Number(v)); | ||
@@ -46,0 +46,0 @@ // Utility methods |
143
15832