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.

(svelte) - Fix typings for subscription's differing results (#1731)

authored by

Phil Pluckthun and committed by
GitHub
db28dda4 395788e2

+18 -12
+5
.changeset/loud-ghosts-sparkle.md
··· 1 + --- 2 + '@urql/svelte': patch 3 + --- 4 + 5 + Improve `OperationStore` and `subscription` types to allow for result types of `data` that differ from the original `Data` type, which may be picked up from `TypedDocumentNode`.
+8 -8
packages/svelte-urql/src/operationStore.ts
··· 13 13 * This Svelte store wraps both a `GraphQLRequest` and an `OperationResult`. 14 14 * It can be used to update the query and read the subsequent result back. 15 15 */ 16 - export interface OperationStore<Data = any, Vars = any> 17 - extends Readable<OperationStore<Data, Vars>> { 16 + export interface OperationStore<Data = any, Vars = any, Result = Data> 17 + extends Readable<OperationStore<Data, Vars, Result>> { 18 18 // Input properties 19 19 query: DocumentNode | TypedDocumentNode<Data, Vars> | string; 20 20 variables: Vars | null; ··· 22 22 // Output properties 23 23 readonly stale: boolean; 24 24 readonly fetching: boolean; 25 - readonly data: Data | undefined; 25 + readonly data: Result | undefined; 26 26 readonly error: CombinedError | undefined; 27 27 readonly extensions: Record<string, any> | undefined; 28 28 // Writable properties 29 - set(value: Partial<OperationStore<Data, Vars>>): void; 30 - update(updater: Updater<Partial<OperationStore<Data, Vars>>>): void; 29 + set(value: Partial<OperationStore<Data, Vars, Result>>): void; 30 + update(updater: Updater<Partial<OperationStore<Data, Vars, Result>>>): void; 31 31 } 32 32 33 - export function operationStore<Data = any, Vars = object>( 33 + export function operationStore<Data = any, Vars = object, Result = Data>( 34 34 query: string | DocumentNode | TypedDocumentNode<Data, Vars>, 35 35 variables?: Vars | null, 36 36 context?: Partial<OperationContext & { pause: boolean }> 37 - ): OperationStore<Data, Vars> { 37 + ): OperationStore<Data, Vars, Result> { 38 38 const internal = { 39 39 query, 40 40 variables: variables || null, ··· 47 47 data: undefined, 48 48 error: undefined, 49 49 extensions: undefined, 50 - } as OperationStore<Data, Vars>; 50 + } as OperationStore<Data, Vars, Result>; 51 51 52 52 const svelteStore = writable(state); 53 53 let _internalUpdate = false;
+5 -4
packages/svelte-urql/src/operations.ts
··· 41 41 extensions: undefined, 42 42 }; 43 43 44 - function toSource<Data, Variables>(store: OperationStore<Data, Variables>) { 44 + function toSource<Data, Variables, Result>( 45 + store: OperationStore<Data, Variables, Result> 46 + ) { 45 47 return make<SourceRequest<Data, Variables>>(observer => { 46 48 let $request: void | GraphQLRequest<Data, Variables>; 47 49 let $contextKey: void | string; ··· 111 113 export type SubscriptionHandler<T, R> = (prev: R | undefined, data: T) => R; 112 114 113 115 export function subscription<Data = any, Result = Data, Variables = object>( 114 - store: OperationStore<Result, Variables>, 116 + store: OperationStore<Data, Variables, Result>, 115 117 handler?: SubscriptionHandler<Data, Result> 116 - ): OperationStore<Result, Variables> { 118 + ): OperationStore<Data, Variables, Result> { 117 119 const client = getClient(); 118 120 const subscription = pipe( 119 121 toSource(store), ··· 182 184 183 185 _markStoreUpdate(update); 184 186 store.set(update); 185 - 186 187 return client 187 188 .mutation(store.query, store.variables as any, store.context) 188 189 .toPromise()