Skip to content

Commit

Permalink
fix(vue): fix variables typing (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
arkandias committed Jan 8, 2025
1 parent e7f08e0 commit cce4f84
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 52 deletions.
4 changes: 2 additions & 2 deletions packages/core/src/utils/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ export const keyDocument = (node: string | DocumentNode): KeyedDocumentNode => {

/** Creates a `GraphQLRequest` from the passed parameters.
*
* @param q - A string of a document or a {@link DocumentNode}
* @param variables - A variables object for the defined GraphQL operation.
* @param _query - A string of a document or a {@link DocumentNode}
* @param _variables - A variables object for the defined GraphQL operation.
* @returns A {@link GraphQLRequest}
*
* @remarks
Expand Down
15 changes: 6 additions & 9 deletions packages/vue-urql/src/useQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import type {

import { useClient } from './useClient';

import type { MaybeRefOrGetter, MaybeRefOrGetterObj } from './utils';
import type { MaybeRefOrGetter } from './utils';
import { useRequestState, useClientState } from './utils';

/** Input arguments for the {@link useQuery} function.
Expand Down Expand Up @@ -73,9 +73,7 @@ export type UseQueryArgs<
* documentation on the `pause` option.
*/
pause?: MaybeRefOrGetter<boolean>;
} & MaybeRefOrGetterObj<
GraphQLRequestParams<Data, MaybeRefOrGetterObj<Variables>>
>;
} & GraphQLRequestParams<Data, MaybeRefOrGetter<Variables>>;

/** State of the current query, your {@link useQuery} function is executing.
*
Expand Down Expand Up @@ -248,11 +246,10 @@ export function callUseQuery<T = any, V extends AnyVariables = AnyVariables>(
const { fetching, operation, extensions, stale, error, hasNext } =
useRequestState<T, V>();

const { isPaused, source, pause, resume, execute, teardown } = useClientState(
args,
client,
'executeQuery'
);
const { isPaused, source, pause, resume, execute, teardown } = useClientState<
T,
V
>(args, client, 'executeQuery');

const teardownQuery = watchEffect(
onInvalidate => {
Expand Down
15 changes: 6 additions & 9 deletions packages/vue-urql/src/useSubscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import type {

import { useClient } from './useClient';

import type { MaybeRefOrGetter, MaybeRefOrGetterObj } from './utils';
import type { MaybeRefOrGetter } from './utils';
import { useRequestState, useClientState } from './utils';

/** Input arguments for the {@link useSubscription} function.
Expand Down Expand Up @@ -55,9 +55,7 @@ export type UseSubscriptionArgs<
* ```
*/
context?: MaybeRefOrGetter<Partial<OperationContext>>;
} & MaybeRefOrGetterObj<
GraphQLRequestParams<Data, MaybeRefOrGetterObj<Variables>>
>;
} & GraphQLRequestParams<Data, MaybeRefOrGetter<Variables>>;

/** Combines previous data with an incoming subscription result’s data.
*
Expand Down Expand Up @@ -248,11 +246,10 @@ export function callUseSubscription<
V
>();

const { isPaused, source, pause, resume, execute, teardown } = useClientState(
args,
client,
'executeSubscription'
);
const { isPaused, source, pause, resume, execute, teardown } = useClientState<
T,
V
>(args, client, 'executeSubscription');

const teardownSubscription = watchEffect(onInvalidate => {
if (source.value) {
Expand Down
44 changes: 12 additions & 32 deletions packages/vue-urql/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {
Client,
CombinedError,
DocumentInput,
GraphQLRequest,
Operation,
OperationContext,
OperationResult,
Expand All @@ -15,49 +16,28 @@ import type { UseSubscriptionArgs } from './useSubscription';
import type { UseQueryArgs } from './useQuery';

export type MaybeRefOrGetter<T> = T | (() => T) | Ref<T>;
export type MaybeRefOrGetterObj<T> =
| MaybeRefOrGetter<T>
| (T extends {} ? { [K in keyof T]: MaybeRefOrGetter<T[K]> } : T);

type UnwrapDeep<T> = T extends Ref<infer V> | (() => infer V)
? UnwrapDeep<V>
: T extends {}
? { [K in keyof T]: UnwrapDeep<T[K]> }
: T;

const isFunction = <T>(
val: MaybeRefOrGetter<T> | MaybeRefOrGetterObj<T>
): val is () => T => typeof val === 'function';
const isPlainObject = (val: unknown): val is object =>
typeof val === 'object' && val !== null && val.constructor === Object;
const isFunction = <T>(val: MaybeRefOrGetter<T>): val is () => T =>
typeof val === 'function';

const toValue = <T>(source: MaybeRefOrGetter<T>): T =>
isFunction(source) ? source() : unref(source);

const toValueDeep = <T>(
source: MaybeRefOrGetter<T> | MaybeRefOrGetterObj<T>
): UnwrapDeep<T> =>
(isRef<T>(source) || isFunction<T>(source)
? toValueDeep(toValue(source))
: Array.isArray(source)
? source.map(val => toValueDeep(val))
: isPlainObject(source)
? Object.fromEntries(
Object.entries(source).map(([key, val]) => [key, toValueDeep(val)])
)
: source) as UnwrapDeep<T>;

export const createRequestWithArgs = <
T = any,
V extends AnyVariables = AnyVariables,
>(
args:
| UseQueryArgs<T, V>
| UseSubscriptionArgs<T, V>
| { query: MaybeRefOrGetter<DocumentInput<T, V>>; variables: V }
) => {
return createRequest<T, V>(toValue(args.query), toValueDeep(args.variables));
};
| { query: DocumentInput<T, V>; variables: V }
): GraphQLRequest<T, V> =>
createRequest<T, V>(
args.query,
'variables' in args
? toValue(args.variables as MaybeRefOrGetter<V>)
: ({} as V)
);

export const useRequestState = <
T = any,
Expand Down Expand Up @@ -93,7 +73,7 @@ export function useClientState<T = any, V extends AnyVariables = AnyVariables>(
? computed(args.pause)
: ref(!!args.pause);

const request = computed(() => createRequestWithArgs(args));
const request = computed(() => createRequestWithArgs<T, V>(args));

const requestOptions = computed(() => {
return 'requestPolicy' in args
Expand Down

0 comments on commit cce4f84

Please sign in to comment.