@nerdwallet/apollo-cache-policies
Advanced tools
Comparing version 3.1.1 to 3.2.0
@@ -0,1 +1,5 @@ | ||
3.2.0 (Dan Reynolds) | ||
Add support for an `orderBy` field to `fragmentWhere` API. | ||
3.1.1 (Dan Reynolds) | ||
@@ -2,0 +6,0 @@ |
import { InMemoryCache, Cache, NormalizedCacheObject, Reference, StoreObject } from "@apollo/client/core"; | ||
import InvalidationPolicyManager from "../policies/InvalidationPolicyManager"; | ||
import { EntityStoreWatcher, EntityTypeMap } from "../entity-store"; | ||
import { FragmentWhereFilter, InvalidationPolicyCacheConfig } from "./types"; | ||
import { FragmentWhereFilter, FragmentWhereOrderBy, InvalidationPolicyCacheConfig } from "./types"; | ||
import { CacheResultProcessor } from "./CacheResultProcessor"; | ||
@@ -52,2 +52,3 @@ import { InvalidationPolicies, InvalidationPolicyEvent, ReadFieldOptions } from "../policies/types"; | ||
limit?: number; | ||
orderBy?: FragmentWhereOrderBy; | ||
}): FragmentType[]; | ||
@@ -58,2 +59,3 @@ readReferenceWhere<T>(options: { | ||
limit?: number; | ||
orderBy?: FragmentWhereOrderBy; | ||
}): readonly Reference[]; | ||
@@ -60,0 +62,0 @@ writeFragmentWhere<FragmentType, TVariables = any>(options: Cache.ReadFragmentOptions<FragmentType, TVariables> & { |
@@ -24,2 +24,3 @@ "use strict"; | ||
const isFunction_1 = __importDefault(require("lodash/isFunction")); | ||
const orderBy_1 = __importDefault(require("lodash/orderBy")); | ||
const InvalidationPolicyManager_1 = __importDefault(require("../policies/InvalidationPolicyManager")); | ||
@@ -435,3 +436,3 @@ const entity_store_1 = require("../entity-store"); | ||
readFragmentWhere(options) { | ||
const { fragment, filter, limit } = options, restOptions = __rest(options, ["fragment", "filter", "limit"]); | ||
const { fragment, filter, limit, orderBy } = options, restOptions = __rest(options, ["fragment", "filter", "limit", "orderBy"]); | ||
const fragmentDefinition = fragment.definitions[0]; | ||
@@ -443,2 +444,3 @@ const __typename = fragmentDefinition.typeCondition.name.value; | ||
limit, | ||
orderBy, | ||
}); | ||
@@ -451,3 +453,3 @@ const matchingFragments = matchingRefs.map(ref => this.readFragment(Object.assign(Object.assign({}, restOptions), { fragment, id: ref.__ref }))); | ||
readReferenceWhere(options) { | ||
const { __typename, filter, limit } = options; | ||
const { __typename, filter, limit, orderBy } = options; | ||
const collectionEntityId = (0, utils_1.collectionEntityIdForType)(__typename); | ||
@@ -458,25 +460,29 @@ // If a stale collection is accessed while it has a pending update, then eagerly update it before the read. | ||
} | ||
const entityReferences = this.readField('data', (0, core_1.makeReference)(collectionEntityId)); | ||
if (!entityReferences) { | ||
let references = this.readField('data', (0, core_1.makeReference)(collectionEntityId)); | ||
if (!references) { | ||
return []; | ||
} | ||
if (!filter) { | ||
return entityReferences; | ||
const readField = this.readField.bind(this); | ||
if (filter) { | ||
references = references.filter(ref => { | ||
if ((0, isFunction_1.default)(filter)) { | ||
return filter(ref, readField); | ||
} | ||
const entityFilterResults = Object.keys(filter).map(filterField => { | ||
// @ts-ignore | ||
const filterValue = filter[filterField]; | ||
const entityValueForFilter = this.readField(filterField, ref); | ||
return filterValue === entityValueForFilter; | ||
}); | ||
return (0, every_1.default)(entityFilterResults, Boolean); | ||
}); | ||
} | ||
const filteredReferences = entityReferences.filter(ref => { | ||
if ((0, isFunction_1.default)(filter)) { | ||
return filter(ref, this.readField.bind(this)); | ||
} | ||
const entityFilterResults = Object.keys(filter).map(filterField => { | ||
// @ts-ignore | ||
const filterValue = filter[filterField]; | ||
const entityValueForFilter = this.readField(filterField, ref); | ||
return filterValue === entityValueForFilter; | ||
}); | ||
return (0, every_1.default)(entityFilterResults, Boolean); | ||
}); | ||
if (orderBy) { | ||
const { field, descending } = orderBy; | ||
references = (0, orderBy_1.default)(references, (ref) => readField(field, ref), [descending ? 'desc' : 'asc']); | ||
} | ||
if (!(0, isNil_1.default)(limit)) { | ||
return (0, take_1.default)(filteredReferences, limit); | ||
return (0, take_1.default)(references, limit); | ||
} | ||
return filteredReferences; | ||
return references; | ||
} | ||
@@ -483,0 +489,0 @@ writeFragmentWhere(options) { |
@@ -19,2 +19,6 @@ import { Cache, InMemoryCacheConfig, Reference } from "@apollo/client/core"; | ||
export type FragmentWhereFilter<T> = Partial<Record<keyof T, any>> | ((__ref: Reference, readField: InvalidationPolicyCache['readField']) => boolean); | ||
export type FragmentWhereOrderBy = { | ||
field: string; | ||
descending: boolean; | ||
}; | ||
//# sourceMappingURL=types.d.ts.map |
@@ -65,5 +65,7 @@ "use strict"; | ||
const limitVar = (0, core_1.makeVar)(options.limit); | ||
const orderByVar = (0, core_1.makeVar)(options.orderBy); | ||
const query = (0, utils_1.buildWatchFragmentWhereQuery)(Object.assign(Object.assign({}, options), { fieldName, | ||
filterVar, | ||
limitVar, cache: this.cache, policies: this.policies })); | ||
limitVar, | ||
orderByVar, cache: this.cache, policies: this.policies })); | ||
return this.watchQueryForField(query, fieldName); | ||
@@ -70,0 +72,0 @@ } |
import { DocumentNode } from 'graphql'; | ||
import { FragmentWhereFilter } from '../cache/types'; | ||
import { FragmentWhereFilter, FragmentWhereOrderBy } from '../cache/types'; | ||
export type WatchFragmentOptions = { | ||
@@ -11,3 +11,4 @@ fragment: DocumentNode; | ||
limit?: number; | ||
orderBy?: FragmentWhereOrderBy; | ||
}; | ||
//# sourceMappingURL=types.d.ts.map |
@@ -6,3 +6,3 @@ import { DocumentNode } from 'graphql'; | ||
import { ReactiveVar } from '@apollo/client/core'; | ||
import { FragmentWhereFilter } from '../cache/types'; | ||
import { FragmentWhereFilter, FragmentWhereOrderBy } from '../cache/types'; | ||
export declare function buildWatchFragmentQuery(options: WatchFragmentOptions & { | ||
@@ -17,4 +17,5 @@ fieldName: string; | ||
limitVar: ReactiveVar<number | undefined>; | ||
orderByVar: ReactiveVar<FragmentWhereOrderBy | undefined>; | ||
fieldName: string; | ||
}): DocumentNode; | ||
//# sourceMappingURL=utils.d.ts.map |
@@ -67,3 +67,3 @@ "use strict"; | ||
function buildWatchFragmentWhereQuery(options) { | ||
const { fragment, filterVar, limitVar, policies, cache, fieldName, } = options; | ||
const { fragment, filterVar, limitVar, orderByVar, policies, cache, fieldName, } = options; | ||
const fragmentDefinition = fragment.definitions[0]; | ||
@@ -88,2 +88,3 @@ const __typename = fragmentDefinition.typeCondition.name.value; | ||
limit: limitVar(), | ||
orderBy: orderByVar(), | ||
}); | ||
@@ -90,0 +91,0 @@ } |
import { DocumentNode } from 'graphql'; | ||
import { FragmentWhereFilter } from '../cache/types'; | ||
import { FragmentWhereFilter, FragmentWhereOrderBy } from '../cache/types'; | ||
export default function useFragmentWhere<FragmentType>(fragment: DocumentNode, options?: { | ||
@@ -7,2 +7,3 @@ filter?: FragmentWhereFilter<FragmentType>; | ||
limit?: number; | ||
orderBy?: FragmentWhereOrderBy; | ||
}): { | ||
@@ -9,0 +10,0 @@ data: FragmentType[]; |
@@ -15,3 +15,10 @@ "use strict"; | ||
const filter = options === null || options === void 0 ? void 0 : options.filter; | ||
const filterVarRef = (0, react_1.useRef)((0, client_2.makeVar)(filter)); | ||
const filterVar = filterVarRef.current; | ||
const limit = options === null || options === void 0 ? void 0 : options.limit; | ||
const limitVarRef = (0, react_1.useRef)((0, client_2.makeVar)(limit)); | ||
const limitVar = limitVarRef.current; | ||
const orderBy = options === null || options === void 0 ? void 0 : options.orderBy; | ||
const orderByVarRef = (0, react_1.useRef)((0, client_2.makeVar)(orderBy)); | ||
const orderByVar = orderByVarRef.current; | ||
const context = (0, react_1.useContext)((0, client_1.getApolloContext)()); | ||
@@ -21,6 +28,2 @@ const client = context.client; | ||
const fieldName = (0, useFragmentTypePolicyFieldName_1.useFragmentTypePolicyFieldName)(); | ||
const filterVarRef = (0, react_1.useRef)((0, client_2.makeVar)(filter)); | ||
const limitVarRef = (0, react_1.useRef)((0, client_2.makeVar)(limit)); | ||
const limitVar = limitVarRef.current; | ||
const filterVar = filterVarRef.current; | ||
const emptyValue = (0, react_1.useRef)([]); | ||
@@ -37,2 +40,7 @@ (0, react_1.useEffect)(() => { | ||
}, [limit]); | ||
(0, react_1.useEffect)(() => { | ||
if (typeof orderBy === 'function' && orderBy !== orderByVar() || !(0, equality_1.equal)(orderBy, orderByVar())) { | ||
orderByVar(orderBy); | ||
} | ||
}, [orderBy]); | ||
const query = (0, utils_2.useOnce)(() => (0, utils_1.buildWatchFragmentWhereQuery)({ | ||
@@ -42,2 +50,3 @@ filter, | ||
limitVar, | ||
orderByVar, | ||
fragment, | ||
@@ -44,0 +53,0 @@ fieldName, |
{ | ||
"name": "@nerdwallet/apollo-cache-policies", | ||
"version": "3.1.1", | ||
"version": "3.2.0", | ||
"description": "An extension to the InMemoryCache from Apollo that adds additional cache policies.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
338450
2521