Skip to content

Commit

Permalink
feat: useful query utils for optimistic updates
Browse files Browse the repository at this point in the history
  • Loading branch information
OrJDev committed Oct 8, 2024
1 parent 90d8cdc commit 0ce559f
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 40 deletions.
5 changes: 5 additions & 0 deletions .changeset/thick-balloons-compete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@solid-mediakit/prpc': patch
---

feat: useful query utils for optimistic updates
58 changes: 19 additions & 39 deletions packages/prpc/solid/src/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,25 @@ import {
createQuery,
CreateQueryResult,
FunctionedParams,
InvalidateOptions,
InvalidateQueryFilters,
Query,
QueryKey,
SolidQueryOptions,
useQueryClient,
} from '@tanstack/solid-query'
import { Accessor } from 'solid-js'
import type {
DeepPartial,
EmptySchema,
ExpectedFn,
ExpectedSchema,
Fn$Output,
IMiddleware,
Infer$PayLoad,
OmitQueryData,
QueryKeyKnown,
QueryRes,
} from './types'
import type { PRPCClientError } from './error'
import { tryAndWrap } from './wrap'
import { ZodSchema } from 'zod'

type QueryRes<
Mw extends IMiddleware[],
Fn extends ExpectedFn<ZObj, Mw>,
ZObj extends ExpectedSchema = EmptySchema,
> = {
useUtils: () => {
invalidate(
input?: DeepPartial<Infer$PayLoad<ZObj>>,
filters?: Omit<InvalidateQueryFilters, 'predicate'> & {
predicate?: (
query: Query<
Infer$PayLoad<ZObj>,
ZObj extends ZodSchema ? PRPCClientError<ZObj> : PRPCClientError,
Infer$PayLoad<ZObj>,
QueryKeyKnown<
Infer$PayLoad<ZObj>,
Infer$PayLoad<ZObj> extends { cursor?: any } | void
? 'infinite'
: 'query'
>
>,
) => boolean
},
options?: InvalidateOptions,
): Promise<void>
}
(
input: ZObj extends EmptySchema
? EmptySchema
: Accessor<Infer$PayLoad<ZObj>>,
opts?: FCreateQueryOptions<ZObj, Infer$PayLoad<ZObj>>,
): CreateQueryResult<Fn$Output<Fn, ZObj, Mw>>
}

export const query$ = <
Mw extends IMiddleware[],
Fn extends ExpectedFn<ZObj, Mw>,
Expand All @@ -85,6 +47,24 @@ export const query$ = <
options,
)
},
cancel(_input, options) {
return queryClient.cancelQueries(
{
queryKey: queryKey(_input),
},
options,
)
},
setData(_input, updater, options) {
return queryClient.setQueryData(
queryKey(_input),
updater as any,
options,
)
},
getData(_input) {
return queryClient.getQueryData(queryKey(_input))
},
} satisfies ReturnType<R['useUtils']>
}
const actualFn = (
Expand Down
56 changes: 55 additions & 1 deletion packages/prpc/solid/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@ import type zod from 'zod'
import type { getRequestEvent } from 'solid-js/web'
import { Accessor } from 'solid-js'
import { FCreateQueryOptions } from './query'
import { CreateMutationResult, CreateQueryResult } from '@tanstack/solid-query'
import {
CancelOptions,
CreateMutationResult,
CreateQueryResult,
InvalidateOptions,
InvalidateQueryFilters,
Query,
SetDataOptions,
Updater,
} from '@tanstack/solid-query'
import { FCreateMutationOptions } from './mutation'
import { ZodSchema } from 'zod'
import { PRPCClientError } from './error'
Expand Down Expand Up @@ -145,3 +154,48 @@ export type QueryBuilder<
>
}
: {})

export type QueryRes<
Mw extends IMiddleware[],
Fn extends ExpectedFn<ZObj, Mw>,
ZObj extends ExpectedSchema = EmptySchema,
> = {
useUtils: () => {
getData(input?: Infer$PayLoad<ZObj>): Fn$Output<Fn, ZObj, Mw> | undefined
setData(
input: Infer$PayLoad<ZObj>,
updater: Updater<
Fn$Output<Fn, ZObj, Mw> | undefined,
Fn$Output<Fn, ZObj, Mw> | undefined
>,
options?: SetDataOptions,
): void

cancel(input?: Infer$PayLoad<ZObj>, options?: CancelOptions): Promise<void>
invalidate(
input?: DeepPartial<Infer$PayLoad<ZObj>>,
filters?: Omit<InvalidateQueryFilters, 'predicate'> & {
predicate?: (
query: Query<
Infer$PayLoad<ZObj>,
ZObj extends ZodSchema ? PRPCClientError<ZObj> : PRPCClientError,
Infer$PayLoad<ZObj>,
QueryKeyKnown<
Infer$PayLoad<ZObj>,
Infer$PayLoad<ZObj> extends { cursor?: any } | void
? 'infinite'
: 'query'
>
>,
) => boolean
},
options?: InvalidateOptions,
): Promise<void>
}
(
input: ZObj extends EmptySchema
? EmptySchema
: Accessor<Infer$PayLoad<ZObj>>,
opts?: FCreateQueryOptions<ZObj, Infer$PayLoad<ZObj>>,
): CreateQueryResult<Fn$Output<Fn, ZObj, Mw>>
}

0 comments on commit 0ce559f

Please sign in to comment.