Mirror: The highly customizable and versatile GraphQL client with which you add on features like normalized caching as you grow.
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

(graphcache) - loosen type constraint on the graphCache generic (#1675)

* loosen type constraint on graphCahce generic

* apply change to resolver as well

authored by

Jovi De Croock and committed by
GitHub
441505cb 00539ff2

+51 -5
+5
.changeset/swift-pumas-tie.md
··· 1 + --- 2 + '@urql/exchange-graphcache': patch 3 + --- 4 + 5 + Loosen the typing constraint on the cacheExchange generic
+2 -2
exchanges/graphcache/src/cacheExchange.ts
··· 30 30 import { addCacheOutcome, toRequestPolicy } from './helpers/operation'; 31 31 import { filterVariables, getMainOperation } from './ast'; 32 32 import { Store, noopDataState, hydrateData, reserveLayer } from './store'; 33 - import { Dependencies, CacheExchangeOpts } from './types'; 33 + import { Dependencies, GenericCacheExchangeOpts } from './types'; 34 34 35 35 type OperationResultWithMeta = OperationResult & { 36 36 outcome: CacheOutcome; ··· 42 42 type OptimisticDependencies = Map<number, Dependencies>; 43 43 type DependentOperations = Record<string, number[]>; 44 44 45 - export const cacheExchange = <C extends Partial<CacheExchangeOpts>>( 45 + export const cacheExchange = <C extends Partial<GenericCacheExchangeOpts>>( 46 46 opts?: C 47 47 ): Exchange => ({ forward, client, dispatchDebug }) => { 48 48 const store = new Store<C>(opts);
+2 -2
exchanges/graphcache/src/offlineExchange.ts
··· 24 24 SerializedRequest, 25 25 OptimisticMutationConfig, 26 26 Variables, 27 - CacheExchangeOpts, 27 + GenericCacheExchangeOpts, 28 28 } from './types'; 29 29 30 30 import { makeDict } from './helpers/dict'; ··· 66 66 error.networkError.message 67 67 )); 68 68 69 - export const offlineExchange = <C extends Partial<CacheExchangeOpts>>( 69 + export const offlineExchange = <C extends Partial<GenericCacheExchangeOpts>>( 70 70 opts: C 71 71 ): Exchange => input => { 72 72 const { storage } = opts;
+2 -1
exchanges/graphcache/src/store/store.ts
··· 15 15 OptimisticMutationConfig, 16 16 KeyingConfig, 17 17 Entity, 18 + GenericCacheExchangeOpts, 18 19 CacheExchangeOpts, 19 20 } from '../types'; 20 21 ··· 38 39 type RootField = 'query' | 'mutation' | 'subscription'; 39 40 40 41 export class Store< 41 - C extends Partial<CacheExchangeOpts> = Partial<CacheExchangeOpts> 42 + C extends Partial<GenericCacheExchangeOpts> = Partial<CacheExchangeOpts> 42 43 > implements Cache { 43 44 data: InMemoryData.InMemoryData; 44 45
+40
exchanges/graphcache/src/types.ts
··· 146 146 storage?: StorageAdapter; 147 147 }; 148 148 149 + /** 150 + * The following part is meant to support the generic type-generated part of graphcache, 151 + * we want to make the extends loose but the default type still has to work as DataFields. 152 + * You can recognize these by the "generic" prefix. 153 + */ 154 + type GenericResolver< 155 + ParentData extends any = DataFields, 156 + Args = Variables, 157 + Result = ResolverResult 158 + > = (parent: ParentData, args: Args, cache: Cache, info: ResolveInfo) => Result; 159 + 160 + interface GenericResolverConfig { 161 + [typeName: string]: { 162 + [fieldName: string]: GenericResolver; 163 + }; 164 + } 165 + 166 + type GenericUpdateResolver< 167 + ParentData extends any = DataFields, 168 + Args = Variables 169 + > = (parent: ParentData, args: Args, cache: Cache, info: ResolveInfo) => void; 170 + 171 + interface GenericUpdatesConfig { 172 + Mutation: { 173 + [fieldName: string]: GenericUpdateResolver; 174 + }; 175 + Subscription: { 176 + [fieldName: string]: GenericUpdateResolver; 177 + }; 178 + } 179 + 180 + export type GenericCacheExchangeOpts = { 181 + updates?: Partial<GenericUpdatesConfig>; 182 + resolvers?: GenericResolverConfig; 183 + optimistic?: OptimisticMutationConfig; 184 + keys?: KeyingConfig; 185 + schema?: IntrospectionData; 186 + storage?: StorageAdapter; 187 + }; 188 + 149 189 // Cache resolvers are user-defined to overwrite an entity field result 150 190 export type Resolver< 151 191 ParentData = DataFields,