Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 44 additions & 15 deletions packages/plugin-solid-query/src/components/Query.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,39 +36,47 @@ function getParams({ paramsType, paramsCasing, pathParamsType, dataReturnType, t
const TError = `ResponseErrorConfig<${typeSchemas.errors?.map((item) => item.name).join(' | ') || 'Error'}>`

if (paramsType === 'object') {
// Wrap pathParams in accessor functions for Solid reactivity
const pathParamsChildren = typeSchemas.pathParams
? Object.fromEntries(
Object.entries(getPathParams(typeSchemas.pathParams, { typed: true, casing: paramsCasing }))
.map(([key, value]) => [key, value ? { ...value, type: `() => ${value.type}` } : value])
)
: {}

return FunctionParams.factory({
data: {
mode: 'object',
children: {
...getPathParams(typeSchemas.pathParams, { typed: true, casing: paramsCasing }),
...pathParamsChildren,
data: typeSchemas.request?.name
? {
type: typeSchemas.request?.name,
type: `() => ${typeSchemas.request?.name}`,
optional: isOptional(typeSchemas.request?.schema),
}
: undefined,
params: typeSchemas.queryParams?.name
? {
type: typeSchemas.queryParams?.name,
type: `() => ${typeSchemas.queryParams?.name}`,
optional: isOptional(typeSchemas.queryParams?.schema),
}
: undefined,
headers: typeSchemas.headerParams?.name
? {
type: typeSchemas.headerParams?.name,
type: `() => ${typeSchemas.headerParams?.name}`,
optional: isOptional(typeSchemas.headerParams?.schema),
}
: undefined,
},
},
options: {
type: `
type: `() =>
{
query?: Partial<UseBaseQueryOptions<${[TData, TError, 'TData', 'TQueryData', 'TQueryKey'].join(', ')}>> & { client?: QueryClient },
client?: ${typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: typeof fetch }` : 'Partial<RequestConfig> & { client?: typeof fetch }'}
}
`,
default: '{}',
default: '() => ({})',
},
})
}
Expand All @@ -83,30 +91,30 @@ function getParams({ paramsType, paramsCasing, pathParamsType, dataReturnType, t
: undefined,
data: typeSchemas.request?.name
? {
type: typeSchemas.request?.name,
type: `() => ${typeSchemas.request?.name}`,
optional: isOptional(typeSchemas.request?.schema),
}
: undefined,
params: typeSchemas.queryParams?.name
? {
type: typeSchemas.queryParams?.name,
type: `() => ${typeSchemas.queryParams?.name}`,
optional: isOptional(typeSchemas.queryParams?.schema),
}
: undefined,
headers: typeSchemas.headerParams?.name
? {
type: typeSchemas.headerParams?.name,
type: `() => ${typeSchemas.headerParams?.name}`,
optional: isOptional(typeSchemas.headerParams?.schema),
}
: undefined,
options: {
type: `
type: `() =>
{
query?: Partial<UseBaseQueryOptions<${[TData, TError, 'TData', 'TQueryData', 'TQueryKey'].join(', ')}>> & { client?: QueryClient },
client?: ${typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: typeof fetch }` : 'Partial<RequestConfig> & { client?: typeof fetch }'}
}
`,
default: '{}',
default: '() => ({})',
},
})
}
Expand Down Expand Up @@ -147,7 +155,28 @@ export function Query({
typeSchemas,
})

const queryOptions = `${queryOptionsName}(${queryOptionsParams.toCall()}) as unknown as UseBaseQueryOptions`
// Build the unwrapped parameter calls using transformName to call accessor functions
const unwrappedQueryKeyCall = queryKeyParams.toCall({
transformName(name) {
const param = params.flatParams[name]
if (param && param.type?.startsWith('() =>')) {
return `${name}?.()`
}
return name
},
})

const unwrappedQueryOptionsCall = queryOptionsParams.toCall({
transformName(name) {
const param = params.flatParams[name]
if (param && param.type?.startsWith('() =>')) {
return `${name}?.()`
}
return name
},
})

const queryOptions = `${queryOptionsName}(${unwrappedQueryOptionsCall}) as unknown as UseBaseQueryOptions`

return (
<File.Source name={name} isExportable isIndexable>
Expand All @@ -160,10 +189,10 @@ export function Query({
comments: getComments(operation),
}}
>
{`
const { query: queryConfig = {}, client: config = {} } = options ?? {}
{`
const { query: queryConfig = {}, client: config = {} } = options?.() ?? {}
const { client: queryClient, ...queryOptions } = queryConfig
const queryKey = queryOptions?.queryKey ?? ${queryKeyName}(${queryKeyParams.toCall()})
const queryKey = queryOptions?.queryKey ?? ${queryKeyName}(${unwrappedQueryKeyCall})

const query = useQuery(() => ({
...${queryOptions},
Expand Down
64 changes: 47 additions & 17 deletions packages/plugin-solid-query/src/components/QueryOptions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,36 +27,44 @@ type GetParamsProps = {

function getParams({ paramsType, paramsCasing, pathParamsType, typeSchemas }: GetParamsProps) {
if (paramsType === 'object') {
// Wrap pathParams in accessor functions for Solid reactivity
const pathParamsChildren = typeSchemas.pathParams
? Object.fromEntries(
Object.entries(getPathParams(typeSchemas.pathParams, { typed: true, casing: paramsCasing }))
.map(([key, value]) => [key, value ? { ...value, type: `() => ${value.type}` } : value])
)
: {}

return FunctionParams.factory({
data: {
mode: 'object',
children: {
...getPathParams(typeSchemas.pathParams, { typed: true, casing: paramsCasing }),
...pathParamsChildren,
data: typeSchemas.request?.name
? {
type: typeSchemas.request?.name,
type: `() => ${typeSchemas.request?.name}`,
optional: isOptional(typeSchemas.request?.schema),
}
: undefined,
params: typeSchemas.queryParams?.name
? {
type: typeSchemas.queryParams?.name,
type: `() => ${typeSchemas.queryParams?.name}`,
optional: isOptional(typeSchemas.queryParams?.schema),
}
: undefined,
headers: typeSchemas.headerParams?.name
? {
type: typeSchemas.headerParams?.name,
type: `() => ${typeSchemas.headerParams?.name}`,
optional: isOptional(typeSchemas.headerParams?.schema),
}
: undefined,
},
},
config: {
type: typeSchemas.request?.name
? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: typeof fetch }`
: 'Partial<RequestConfig> & { client?: typeof fetch }',
default: '{}',
? `() => Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: typeof fetch }`
: '() => Partial<RequestConfig> & { client?: typeof fetch }',
default: '() => ({})',
},
})
}
Expand All @@ -71,27 +79,27 @@ function getParams({ paramsType, paramsCasing, pathParamsType, typeSchemas }: Ge
: undefined,
data: typeSchemas.request?.name
? {
type: typeSchemas.request?.name,
type: `() => ${typeSchemas.request?.name}`,
optional: isOptional(typeSchemas.request?.schema),
}
: undefined,
params: typeSchemas.queryParams?.name
? {
type: typeSchemas.queryParams?.name,
type: `() => ${typeSchemas.queryParams?.name}`,
optional: isOptional(typeSchemas.queryParams?.schema),
}
: undefined,
headers: typeSchemas.headerParams?.name
? {
type: typeSchemas.headerParams?.name,
type: `() => ${typeSchemas.headerParams?.name}`,
optional: isOptional(typeSchemas.headerParams?.schema),
}
: undefined,
config: {
type: typeSchemas.request?.name
? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: typeof fetch }`
: 'Partial<RequestConfig> & { client?: typeof fetch }',
default: '{}',
? `() => Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: typeof fetch }`
: '() => Partial<RequestConfig> & { client?: typeof fetch }',
default: '() => ({})',
},
})
}
Expand All @@ -114,8 +122,29 @@ export function QueryOptions({ name, clientName, typeSchemas, paramsCasing, para
typeSchemas,
})

// Build the unwrapped parameter calls using transformName to call accessor functions
const unwrappedQueryKeyCall = queryKeyParams.toCall({
transformName(name) {
const param = params.flatParams[name]
if (param && param.type?.startsWith('() =>')) {
return `${name}?.()`
}
return name
},
})

const unwrappedClientCall = clientParams.toCall({
transformName(name) {
const param = params.flatParams[name]
if (param && param.type?.startsWith('() =>')) {
return `${name}?.()`
}
return name
},
})

const enabled = Object.entries(queryKeyParams.flatParams)
.map(([key, item]) => (item && !item.optional ? key : undefined))
.map(([key, item]) => (item && !item.optional ? `${key}?.()` : undefined))
.filter(Boolean)
.join('&& ')

Expand All @@ -125,13 +154,14 @@ export function QueryOptions({ name, clientName, typeSchemas, paramsCasing, para
<File.Source name={name} isExportable isIndexable>
<Function name={name} export params={params.toConstructor()}>
{`
const queryKey = ${queryKeyName}(${queryKeyParams.toCall()})
const queryKey = ${queryKeyName}(${unwrappedQueryKeyCall})
return queryOptions<${TData}, ${TError}, ${TData}, typeof queryKey>({
${enabledText}
queryKey,
queryFn: async ({ signal }) => {
config.signal = signal
return ${clientName}(${clientParams.toCall()})
const unwrappedConfig = config?.() ?? {}
unwrappedConfig.signal = signal
return ${clientName}(${unwrappedClientCall})
},
})
`}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ export async function findPetsByTags(
}

export function findPetsByTagsQueryOptions(
headers: FindPetsByTagsHeaderParams,
params?: FindPetsByTagsQueryParams,
config: Partial<RequestConfig> & { client?: typeof fetch } = {},
headers: () => FindPetsByTagsHeaderParams,
params?: () => FindPetsByTagsQueryParams,
config: () => Partial<RequestConfig> & { client?: typeof fetch } = () => ({}),
) {
const queryKey = findPetsByTagsQueryKey(params)
const queryKey = findPetsByTagsQueryKey(params?.())
return queryOptions<
ResponseConfig<FindPetsByTagsQueryResponse>,
ResponseErrorConfig<FindPetsByTags400>,
Expand All @@ -47,8 +47,9 @@ export function findPetsByTagsQueryOptions(
>({
queryKey,
queryFn: async ({ signal }) => {
config.signal = signal
return findPetsByTags(headers, params, config)
const unwrappedConfig = config?.() ?? {}
unwrappedConfig.signal = signal
return findPetsByTags(headers?.(), params?.(), config?.())
},
})
}
Expand All @@ -63,22 +64,22 @@ export function createFindPetsByTags<
TQueryData = ResponseConfig<FindPetsByTagsQueryResponse>,
TQueryKey extends QueryKey = FindPetsByTagsQueryKey,
>(
headers: FindPetsByTagsHeaderParams,
params?: FindPetsByTagsQueryParams,
options: {
headers: () => FindPetsByTagsHeaderParams,
params?: () => FindPetsByTagsQueryParams,
options: () => {
query?: Partial<UseBaseQueryOptions<ResponseConfig<FindPetsByTagsQueryResponse>, ResponseErrorConfig<FindPetsByTags400>, TData, TQueryData, TQueryKey>> & {
client?: QueryClient
}
client?: Partial<RequestConfig> & { client?: typeof fetch }
} = {},
} = () => ({}),
) {
const { query: queryConfig = {}, client: config = {} } = options ?? {}
const { query: queryConfig = {}, client: config = {} } = options?.() ?? {}
const { client: queryClient, ...queryOptions } = queryConfig
const queryKey = queryOptions?.queryKey ?? findPetsByTagsQueryKey(params)
const queryKey = queryOptions?.queryKey ?? findPetsByTagsQueryKey(params?.())

const query = useQuery(
() => ({
...(findPetsByTagsQueryOptions(headers, params, config) as unknown as UseBaseQueryOptions),
...(findPetsByTagsQueryOptions(headers?.(), params?.(), config) as unknown as UseBaseQueryOptions),
queryKey,
initialData: null,
...(queryOptions as unknown as Omit<UseBaseQueryOptions, 'queryKey'>),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,17 @@ export async function findPetsByTags(
}

export function findPetsByTagsQueryOptions(
headers: FindPetsByTagsHeaderParams,
params?: FindPetsByTagsQueryParams,
config: Partial<RequestConfig> & { client?: typeof fetch } = {},
headers: () => FindPetsByTagsHeaderParams,
params?: () => FindPetsByTagsQueryParams,
config: () => Partial<RequestConfig> & { client?: typeof fetch } = () => ({}),
) {
const queryKey = findPetsByTagsQueryKey(params)
const queryKey = findPetsByTagsQueryKey(params?.())
return queryOptions<FindPetsByTagsQueryResponse, ResponseErrorConfig<FindPetsByTags400>, FindPetsByTagsQueryResponse, typeof queryKey>({
queryKey,
queryFn: async ({ signal }) => {
config.signal = signal
return findPetsByTags(headers, params, config)
const unwrappedConfig = config?.() ?? {}
unwrappedConfig.signal = signal
return findPetsByTags(headers?.(), params?.(), config?.())
},
})
}
Expand All @@ -58,22 +59,22 @@ export function createFindPetsByTags<
TQueryData = FindPetsByTagsQueryResponse,
TQueryKey extends QueryKey = FindPetsByTagsQueryKey,
>(
headers: FindPetsByTagsHeaderParams,
params?: FindPetsByTagsQueryParams,
options: {
headers: () => FindPetsByTagsHeaderParams,
params?: () => FindPetsByTagsQueryParams,
options: () => {
query?: Partial<UseBaseQueryOptions<FindPetsByTagsQueryResponse, ResponseErrorConfig<FindPetsByTags400>, TData, TQueryData, TQueryKey>> & {
client?: QueryClient
}
client?: Partial<RequestConfig> & { client?: typeof fetch }
} = {},
} = () => ({}),
) {
const { query: queryConfig = {}, client: config = {} } = options ?? {}
const { query: queryConfig = {}, client: config = {} } = options?.() ?? {}
const { client: queryClient, ...queryOptions } = queryConfig
const queryKey = queryOptions?.queryKey ?? findPetsByTagsQueryKey(params)
const queryKey = queryOptions?.queryKey ?? findPetsByTagsQueryKey(params?.())

const query = useQuery(
() => ({
...(findPetsByTagsQueryOptions(headers, params, config) as unknown as UseBaseQueryOptions),
...(findPetsByTagsQueryOptions(headers?.(), params?.(), config) as unknown as UseBaseQueryOptions),
queryKey,
initialData: null,
...(queryOptions as unknown as Omit<UseBaseQueryOptions, 'queryKey'>),
Expand Down
Loading
Loading