No adapter (Svelte, Solid, Angular)
There is no Svelte, Solid or Angular adapter (for Vue 3 there is @getenginex/sdk-vue, which does everything this page describes). The factories in @getenginex/sdk/query (and in the /query entries of the product packages) return plain objects, compatible with TanStack Query v5 in any framework:
import { createMutations, createQueries, enginexKeys } from '@getenginex/sdk/query'
const q = createQueries(enginex) // once per client
const m = createMutations(enginex)
q.wallet.getBalance() // { queryKey, queryFn } → useQuery / createQuery / prefetchQuery
m.deposits.createPix() // { mutationKey, mutationFn, invalidates } → useMutation / createMutationKeys, invalidation and idempotency are those of the queries layer.
What the adapters (React and Vue) do and you do by hand
- Call the factory again when the session changes. The key of user queries carries the
userIdat call time (with no session,'anonymous', and thequeryFnrejects withUNAUTHENTICATEDwithout a request). Build the options inside something reactive to the session (a getter,computed,$derived, signal) so the key follows login and logout. - Remove the previous user's data. When the user changes, remove (not just invalidate)
enginexKeys.user()from theQueryClient. - Invalidate after success. In
onSuccess, invalidate each key frominvalidates(input, result)and return the promise, so the mutation only finishes with the data updated. - Do not retry
UNAUTHENTICATED. This error made no request; retrying it only delays the error.
Steps 2 and 4 fit in a single module, shared by the examples below:
// enginex.ts
import { createEnginexClient, isEnginexError, localStorageTokenStore, type Session } from '@getenginex/sdk'
import { createMutations, createQueries, enginexKeys } from '@getenginex/sdk/query'
// The framework package's QueryClient.
import { QueryClient } from '@tanstack/svelte-query' // or '@tanstack/solid-query'
export const enginex = createEnginexClient({ locale: 'pt-BR', tokenStore: localStorageTokenStore() })
export const q = createQueries(enginex)
export const m = createMutations(enginex)
export const queryClient = new QueryClient({
defaultOptions: {
queries: {
staleTime: 60_000,
retry: (failures, error) => !isEnginexError(error, 'UNAUTHENTICATED') && failures < 3,
},
},
})
// User switch (logout, login, another `sub`): nothing from the previous player stays in the cache.
const identity = (s: Session) => `${s.status}:${s.userId ?? ''}`
let last = identity(enginex.session.get())
enginex.session.subscribe((session) => {
const next = identity(session)
if (next !== last) queryClient.removeQueries({ queryKey: enginexKeys.user() })
last = next
})
// Step 3: the onSuccess of every SDK mutation.
export function invalidateAfter<I, R>(options: { invalidates: (input: I, result: R) => readonly (readonly unknown[])[] }) {
return (result: R, input: I) =>
Promise.all(options.invalidates(input, result).map((queryKey) => queryClient.invalidateQueries({ queryKey })))
}Svelte 5 (@tanstack/svelte-query 6)
<!-- App.svelte -->
<script lang="ts">
import { QueryClientProvider } from '@tanstack/svelte-query'
import { queryClient } from './enginex'
let { children } = $props()
</script>
<QueryClientProvider client={queryClient}>{@render children()}</QueryClientProvider>// session.svelte.ts
import { enginex } from './enginex'
class SessionState {
current = $state.raw(enginex.session.get())
}
export const session = new SessionState()
enginex.session.subscribe((s) => {
session.current = s
})<script lang="ts">
import { createMutation, createQuery } from '@tanstack/svelte-query'
import { invalidateAfter, m, q } from './enginex'
import { session } from './session.svelte'
// The function is re-evaluated when `session.current` changes: the key follows the login.
const balance = createQuery(() => ({
...q.wallet.getBalance(),
enabled: session.current.status === 'authenticated',
}))
const createPix = m.deposits.createPix()
const pix = createMutation(() => ({ ...createPix, onSuccess: invalidateAfter(createPix) }))
</script>
{#if balance.data}<p>{balance.data.realBalance}</p>{/if}
<button disabled={pix.isPending} onclick={() => pix.mutate({ amount: 5000 })}>Deposit</button>This page has no Svelte 4 example. There, with @tanstack/svelte-query 5, the options go in a store (a derived of the session store) instead of a function; the rules above are the same.
Solid (@tanstack/solid-query)
import { QueryClientProvider, createMutation, createQuery } from '@tanstack/solid-query'
import { createSignal } from 'solid-js'
import { enginex, invalidateAfter, m, q, queryClient } from './enginex'
const [session, setSession] = createSignal(enginex.session.get())
enginex.session.subscribe(setSession)
function Wallet() {
// The function is re-run when `session()` changes: the key follows the login.
const balance = createQuery(() => ({
...q.wallet.getBalance(),
enabled: session().status === 'authenticated',
}))
const createPix = m.deposits.createPix()
const pix = createMutation(() => ({ ...createPix, onSuccess: invalidateAfter(createPix) }))
return (
<>
<p>{balance.data?.realBalance}</p>
<button disabled={pix.isPending} onClick={() => pix.mutate({ amount: 5000 })}>
Deposit
</button>
</>
)
}
export const App = () => (
<QueryClientProvider client={queryClient}>
<Wallet />
</QueryClientProvider>
)No framework (@tanstack/query-core)
The same factories work with the bare QueryClient, in a script or on a server:
const balance = await queryClient.fetchQuery(q.wallet.getBalance())
await queryClient.prefetchQuery(q.config.get())On the server, create an SDK client and a QueryClient per request (see Getting started), never at module scope.
Realtime
The core's realtime works the same in any framework. For the useRealtimeSync pattern from React and Vue (invalidating the balance on every balance_update instead of writing to the cache), subscribe to the topic and invalidate the factory's key:
import { coreTopics, createRealtime } from '@getenginex/sdk/realtime'
const realtime = createRealtime(enginex)
const invalidateBalance = () => queryClient.invalidateQueries({ queryKey: q.wallet.getBalance().queryKey })
const off = realtime.subscribe(coreTopics.balance(), invalidateBalance)
const offResync = realtime.onResync(coreTopics.balance(), invalidateBalance)Subscribe only with a user in the session and subscribe again when the user changes (the balance key carries the userId). See Realtime.