Skip to content

Commit

Permalink
v1.1.2 - Improvements in Utils, Helpers; Readme added new info
Browse files Browse the repository at this point in the history
### Changed
 - _helpers / updateQueryHelper_ - added _sourceDefault_ option (details in _Readme_)
 - _utils_ - improved _get_ utility
 - _Readme_ - added _sourceDefault_ definition
 - upgrade version
  • Loading branch information
kosiakMD committed Jun 4, 2019
1 parent a56f2b7 commit 0727cc9
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 24 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## Unreleased

## v1.1.2
### Changed
- _helpers / updateQueryHelper_ - added _sourceDefault_ option (details in _Readme_)
- _utils_ - improved _get_ utility
- _Readme_ - added _sourceDefault_ definition

## v1.1.1
### Changed
- _Readme.md_ content link fix, fixed explanation comments' mistakes
Expand Down
44 changes: 24 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
πŸš€ Apollo πŸ› β€ Tool represented as InMemoryCache πŸ§™ wrapper for πŸ—„ storing / πŸ—ƒ restoring βœ… selected only πŸ—‚οΈ queries and for updating β›“ linked / nested without πŸ†” IDs

## Content
- [Install](#install)
- [Usage](#usage)
- [Creating Enchanted InMemoryCache Config](#creating-enchanted-inmemorycache-config)
- [Basic usage](#basic-usage)
- [API](#api)
- [SubscribedQuery](#subscribedquery)
- [updateQueryHelper: Updater](#updatequeryhelper-updater)
- [Types](#types)
- [License](#license)

- [Install](#install)
- [Usage](#usage)
- [Creating Enchanted InMemoryCache Config](#creating-enchanted-inmemorycache-config)
- [Basic usage](#basic-usage)
- [API](#api)
- [SubscribedQuery](#subscribedquery)
- [updateQueryHelper: Updater](#updatequeryhelper-updater)
- [Types](#types)
- [License](#license)

## Install

Expand Down Expand Up @@ -171,6 +172,8 @@ const inMemoryCache = new InMemoryCache({
// ...
});
// ...
const logCacheWrite = true; // for debug/log reasons

const cache = createEnchantedInMemoryCache(
inMemoryCache,
subscribedQueries,
Expand Down Expand Up @@ -221,13 +224,14 @@ Array\<SubscribedQuery>

#### updateQueryHelper: Updater

| Prop | Type | Default | Note |
| ------------- | ----------------- | ------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `sourceQuery` | `QueryObject` | _(required)_ | Object Data of source Query tracked for updating target Query |
| `sourcePath` | `ObjectPath` | `[]` | path to Data source Query object field |
| `targetQuery` | `QueryObject` | | Object Data of target Query should be updated |
| `targetPath` | `ObjectPath` | `[]` | path to Data target Query object field |
| `updateType` | `UpdateTypesEnum` | `replace` | `replace` - just replacing target Data at object some field by source Data <br/> `rootMerge` - merge target Data Object at object field by source Data with replacing tested Data <br/> `deepMerge` - merge target Object Data at all fields (`sourcePath` of `sourceQuery`) by source Object Data with same fields (`targetPath` of `targetQuery`); begins at source Object field and goes recursively into the depths |
| Prop | Type | Default | Note |
| --------------- | ----------------- | ------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `sourceQuery` | `QueryObject` | _(required)_ | Object Data of source Query tracked for updating target Query |
| `sourcePath` | `ObjectPath` | `[]` | path to Data source Query object field |
| `targetQuery` | `QueryObject` | | Object Data of target Query should be updated |
| `targetPath` | `ObjectPath` | `[]` | path to Data target Query object field |
| `updateType` | `UpdateTypesEnum` | `replace` | `replace` - just replacing target Data at object some field by source Data <br/> `rootMerge` - merge target Data Object at object field by source Data with replacing tested Data <br/> `deepMerge` - merge target Object Data at all fields (`sourcePath` of `sourceQuery`) by source Object Data with same fields (`targetPath` of `targetQuery`); begins at source Object field and goes recursively into the depths |
| `sourceDefault` | `any` | `null` | data to be used for updating the target Object if no present in the source Object |

## Types

Expand All @@ -236,7 +240,7 @@ type ArrayPath = Array<string | number>; // ['a', 'b', 0, 'c', 1]

type ObjectPath = ArrayPath | string; // ['a', 'b', 'c', 0] | 'a.b.c.0'

type QueryObject<Data> = { Object, Data }; // Query result data
type QueryObject<Data> = { Object; Data }; // Query result data

type Updater = <T1, T2, T3>(
sourceQuery: QueryObject<T1>,
Expand All @@ -253,7 +257,7 @@ type LinkedQuery = {
queryNode: DocumentNode; // Apollo Query definition, returned by gql`...`
updateName: string;
updater?: Updater;
}
};

type StoredQuery = {
name: string;
Expand All @@ -262,9 +266,9 @@ type StoredQuery = {
nest?: ObjectPath;
retrieveField?: string;
retriever?: Retriever;
}
};

type SubscribedQuery = LinkedQuery | StoredQuery
type SubscribedQuery = LinkedQuery | StoredQuery;

type SubscribedQueries = Array<SubscribedQuery>;

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "apollo-enchanted-cache-inmemory",
"version": "1.1.1",
"version": "1.1.2",
"description": "Apollo InMemoryCache wrapper for storing selected only queries and for updating linked/nested without IDs",
"main": "src/index.js",
"scripts": {
Expand Down
4 changes: 3 additions & 1 deletion src/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const UpdateTypesEnum = {
* updateType?: UpdateTypesEnum
* withMerge?: Boolean
* withMergeRootOnly?: Boolean
* sourceDefault?: any
* }} UpdateInput
* withMerge - deep merge or replace; DEFAULT = FALSE
* withMergeRootOnly - either root merge with replacing nested
Expand All @@ -33,8 +34,9 @@ export const updateQueryHelper = updateInput => {
withMerge = false,
withMergeRootOnly = false,
updateType = UpdateTypesEnum.replace,
sourceDefault = null,
} = updateInput;
const newData = get(sourceQuery, sourcePath);
const newData = get(sourceQuery, sourcePath, sourceDefault);
let setData = null;
if (updateType === UpdateTypesEnum.replace) {
// just replace
Expand Down
3 changes: 1 addition & 2 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ export function get(obj, path, def) {
return fullPath.every(everyFunc) ? obj : def;

function everyFunc(step) {
// eslint-disable-next-line
return !(step != null && (obj = obj[step]) === undefined);
return !(obj == null || (step != null && (obj = obj[step]) === undefined));
}
}

Expand Down

0 comments on commit 0727cc9

Please sign in to comment.