vishalbalaji / trpc-svelte-query-adapter

A simple adapter to use `@tanstack/svelte-query` with trpc, similar to `@trpc/react-query`.
71 stars 6 forks source link

Allow for naming of queries with `createServerQueries` #21

Closed dihmeetree closed 7 months ago

dihmeetree commented 7 months ago

Thanks for this library first of all, it's great! πŸ˜ƒ

Instead of doing this:

export const load: PageLoad = async (event) => {
  const { queryClient } = await event.parent();
  const api = trpc(event, queryClient);
  return {
    queries: await api.createServerQueries((t) => [
      t.public.todos.list(),
      t.public.hello.get()
    ])
  };
};

I'd like to be able to name the queries like so:

export const load: PageLoad = async (event) => {
  const { queryClient } = await event.parent();
  const api = trpc(event, queryClient);
  return {
    queries: await api.createServerQueries((t) => [
      {
        listTodos: t.public.todos.list(),
        getHello: t.public.hello.get()
      }
    ])
  };
};

Right now i'm doing something like this on the frontend

<script lang="ts">
  import { page } from '$app/stores';
  import { trpc } from '$lib/trpc/client';
  import type { PageData } from './$types';

  export let data: PageData;

  const queries = data.queries();

  $: listTodos = $queries[0];
  $: getHello = $queries[1];

  const api = trpc($page);
  const addTodo = api.public.todos.add.createMutation();
  const deleteTodo = api.public.todos.delete.createMutation();
</script>

I'd love to be able to access the queries directly like so:

$queries.listTodos

and

$queries.getHello
vishalbalaji commented 7 months ago

Hi, glad you liked the library πŸ€—.

This library tries not to stray too far away from how Tanstack query does things. To do this, we might have to significantly deviate from what createQueries returns by default, which is not really desirable since it would mean users having to adapt to non-standard behaviour that exists just for this library.

Personally, I just get over this through just de-structuring the query, like so:

const [fooQuery, barQuery] = $queries;

I don't think this is something I would be adding to this library, but thanks a lot for the suggestion!

dihmeetree commented 7 months ago

Ahhhhh destructuring right! Sometimes I forget the basics haha.

This will work perfect! Thank you for the suggestion πŸ˜ƒ