TanStackT
TanStack3y ago
2 replies
progressive-amaranth

Is it possible to create queries outside of context?

I have a load function in a +layout.ts

I'm running a adapter-static, so all this is purely on the browser.

import { queries } from '$lib/api';
import { createQuery } from '@tanstack/svelte-query';
import type { LayoutLoad } from './$types';

export const load: LayoutLoad = async ({ params: { accountId }, parent }) => {
    const { queryClient } = await parent();
    const accountQueryDefinition = queries.accounts.id(accountId);
    return {
        accountId,
        accountQueryDefinition,
        accountQuery: createQuery(accountQueryDefinition, queryClient),
    };
};


I'd like to send the query through as a store to the pages.

Unfortunately, it looks like @tanstack/svelte-query/context.ts has a call getIsRestoringContext = (): Readable<boolean> which results in a call to getContext in svelte - which throws.

app.js:24 Error: Function called outside component initialization
    at get_current_component (lifecycle.js:11:32)
    at getContext (lifecycle.js:141:9)
    at getIsRestoringContext (context.js:19:25)
    at useIsRestoring (useIsRestoring.js:3:12)
    at createBaseQuery (createBaseQuery.js:9:25)
    at createQuery (createQuery.js:4:12)
    at load (+layout.ts:13:17)
    at async load_node (client.js?v=e2855009:530:14)


Is this a bug in tanstacks context.ts, or just not possible with the design?
Was this page helpful?