Skip to content

Commit

Permalink
fix(tanstack): incorrect typing for svelte query hooks (#1492)
Browse files Browse the repository at this point in the history
  • Loading branch information
ymc9 authored Jun 8, 2024
1 parent 41880f3 commit ed5133c
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 26 deletions.
2 changes: 1 addition & 1 deletion packages/plugins/tanstack-query/src/runtime-v5/svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export function useModelQuery<TQueryFnData, TData, TError>(
...options,
};
}
return createQuery(mergedOpt);
return createQuery<TQueryFnData, TError, TData>(mergedOpt);
}

/**
Expand Down
97 changes: 72 additions & 25 deletions packages/plugins/tanstack-query/tests/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('Tanstack Query Plugin Tests', () => {

const sharedModel = `
model User {
id String @id
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
email String @unique
Expand All @@ -32,7 +32,7 @@ enum role {
}
model post_Item {
id String @id
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
title String
Expand All @@ -51,13 +51,28 @@ model Foo {
const reactAppSource = {
name: 'main.ts',
content: `
import { useInfiniteFindManypost_Item } from './hooks';
import { useFindFirstpost_Item, useInfiniteFindManypost_Item, useCreatepost_Item } from './hooks';
const { data, fetchNextPage, hasNextPage } = useInfiniteFindManypost_Item();
useInfiniteFindManypost_Item({ where: { published: true } });
useInfiniteFindManypost_Item(undefined, { getNextPageParam: () => null });
console.log(data?.pages[0][0].published);
console.log(data?.pageParams[0]);
function query() {
const { data } = useFindFirstpost_Item({include: { author: true }});
console.log(data?.viewCount);
console.log(data?.author?.email);
}
function infiniteQuery() {
const { data, fetchNextPage, hasNextPage } = useInfiniteFindManypost_Item();
useInfiniteFindManypost_Item({ where: { published: true } });
useInfiniteFindManypost_Item(undefined, { getNextPageParam: () => null });
console.log(data?.pages[0][0].published);
console.log(data?.pageParams[0]);
}
async function mutation() {
const { mutateAsync } = useCreatepost_Item();
const data = await mutateAsync({ data: { title: 'hello' }, include: { author: true } });
console.log(data?.viewCount);
console.log(data?.author?.email);
}
`,
};

Expand Down Expand Up @@ -108,11 +123,13 @@ ${sharedModel}
content: `
import { useSuspenseInfiniteFindManypost_Item } from './hooks';
const { data, fetchNextPage, hasNextPage } = useSuspenseInfiniteFindManypost_Item();
useSuspenseInfiniteFindManypost_Item({ where: { published: true } });
useSuspenseInfiniteFindManypost_Item(undefined, { getNextPageParam: () => null });
console.log(data?.pages[0][0].published);
console.log(data?.pageParams[0]);
function suspenseInfiniteQuery() {
const { data, fetchNextPage, hasNextPage } = useSuspenseInfiniteFindManypost_Item();
useSuspenseInfiniteFindManypost_Item({ where: { published: true } });
useSuspenseInfiniteFindManypost_Item(undefined, { getNextPageParam: () => null });
console.log(data?.pages[0][0].published);
console.log(data?.pageParams[0]);
}
`,
},
],
Expand All @@ -123,13 +140,28 @@ ${sharedModel}
const vueAppSource = {
name: 'main.ts',
content: `
import { useInfiniteFindManypost_Item } from './hooks';
import { useFindFirstpost_Item, useInfiniteFindManypost_Item, useCreatepost_Item } from './hooks';
function query() {
const { data } = useFindFirstpost_Item({include: { author: true }});
console.log(data.value?.viewCount);
console.log(data.value?.author?.email);
}
const { data, fetchNextPage, hasNextPage } = useInfiniteFindManypost_Item();
useInfiniteFindManypost_Item({ where: { published: true } });
useInfiniteFindManypost_Item(undefined, { getNextPageParam: () => null });
console.log(data.value?.pages[0][0].published);
console.log(data.value?.pageParams[0]);
function infiniteQuery() {
const { data, fetchNextPage, hasNextPage } = useInfiniteFindManypost_Item();
useInfiniteFindManypost_Item({ where: { published: true } });
useInfiniteFindManypost_Item(undefined, { getNextPageParam: () => null });
console.log(data.value?.pages[0][0].published);
console.log(data.value?.pageParams[0]);
}
async function mutation() {
const { mutateAsync } = useCreatepost_Item();
const data = await mutateAsync({ data: { title: 'hello' }, include: { author: true } });
console.log(data?.viewCount);
console.log(data?.author?.email);
}
`,
};

Expand Down Expand Up @@ -182,13 +214,28 @@ ${sharedModel}
name: 'main.ts',
content: `
import { get } from 'svelte/store';
import { useInfiniteFindManypost_Item } from './hooks';
import { useFindFirstpost_Item, useInfiniteFindManypost_Item, useCreatepost_Item } from './hooks';
function query() {
const { data } = get(useFindFirstpost_Item({include: { author: true }}));
console.log(data?.viewCount);
console.log(data?.author?.email);
}
function infiniteQuery() {
const { data, fetchNextPage, hasNextPage } = get(useInfiniteFindManypost_Item());
useInfiniteFindManypost_Item({ where: { published: true } });
useInfiniteFindManypost_Item(undefined, { getNextPageParam: () => null });
console.log(data?.pages[0][0].published);
console.log(data?.pageParams[0]);
}
const { data, fetchNextPage, hasNextPage } = get(useInfiniteFindManypost_Item());
useInfiniteFindManypost_Item({ where: { published: true } });
useInfiniteFindManypost_Item(undefined, { getNextPageParam: () => null });
console.log(data?.pages[0][0].published);
console.log(data?.pageParams[0]);
async function mutation() {
const { mutateAsync } = get(useCreatepost_Item());
const data = await mutateAsync({ data: { title: 'hello' }, include: { author: true } });
console.log(data?.viewCount);
console.log(data?.author?.email);
}
`,
};

Expand Down

0 comments on commit ed5133c

Please sign in to comment.