query-key-factory icon indicating copy to clipboard operation
query-key-factory copied to clipboard

More ways of structure keys

Open TommySorensen opened this issue 1 year ago • 4 comments

It would be nice to have a third level in the factory. For example if you have 5 different api urls with 30 endpoints underneed and not only a single one

export const cmsKeys = createQueryKeys('cms', {
  content: {
    page: (locale: string, path: string) => [{ locale, path }],
    breadcrumb: (locale: string, path: string) => [{ locale, path }],
  }
});

So i can have a query like ['cms', 'content', 'page', { locale, path }].

Basically i would have multiple swagger interfaces like this one. So in this case i would like ['bff', 'basket', 'baskets', { basketReference }] image

TommySorensen avatar Oct 07 '22 12:10 TommySorensen

@TommySorensen thanks for the suggestion and for sharing more information about your use cases. FYI v1.0.0 brings support for nested keys and the new API should be available soon, probably next week if the last mile goes smoothly.

lukemorales avatar Oct 10 '22 00:10 lukemorales

@lukemorales Sounds great 👍 One thing i was considering was to also always put the url(without query params) for the fetch function inside the key factory so the react query key, fetch function and the url always go hand in hand. Something like this will work.

export const cmsQueries = createQueryKeys('cms', {
  content: (locale: string, path: string) => ({ locale, path, url: '/some/url' }),
});

export const queries = mergeQueryKeys(cmsQueries);
export type Queries = inferQueryKeyStore<typeof queries>;

function getPage(context: QueryFunctionContext<Queries['cms']['content']>) {
  const { signal, queryKey } = context;
  const [, , { locale, path, url }] = queryKey;

  const searchParams = new URLSearchParams();
  searchParams.set('locale', locale);
  searchParams.set('path', path);

  return ky
    .get(url, { signal })
    .json<Page>();
}

TommySorensen avatar Oct 11 '22 06:10 TommySorensen

@lukemorales Sounds great 👍 One thing i was considering was to also always put the url(without query params) for the fetch function inside the key factory so the react query key, fetch function and the url always go hand in hand. Something like this will work.

export const cmsQueries = createQueryKeys('cms', {
  content: (locale: string, path: string) => ({ locale, path, url: '/some/url' }),
});

export const queries = mergeQueryKeys(cmsQueries);
export type Queries = inferQueryKeyStore<typeof queries>;

function getPage(context: QueryFunctionContext<Queries['cms']['content']>) {
  const { signal, queryKey } = context;
  const [, , { locale, path, url }] = queryKey;

  const searchParams = new URLSearchParams();
  searchParams.set('locale', locale);
  searchParams.set('path', path);

  return ky
    .get(url, { signal })
    .json<Page>();
}

You should already be able to do that? Not sure you're suggesting some feature or just giving an example of an idea you're considering 😅, but I don't see why you wouldn't already be able to do

lukemorales avatar Oct 11 '22 15:10 lukemorales

You should already be able to do that? Not sure you're suggesting some feature or just giving an example of an idea you're considering 😅, but I don't see why you wouldn't already be able to do

I guess it's me trying to give more context to the talk 😀

TommySorensen avatar Oct 12 '22 06:10 TommySorensen

@TommySorensen just letting you know, v1.0 has been released with support for nested keys: https://github.com/lukemorales/query-key-factory/releases/tag/v1.0.0

lukemorales avatar Oct 25 '22 03:10 lukemorales

@lukemorales Yeah thx 👍 I will test it out tomorrow in my project.

TommySorensen avatar Oct 25 '22 09:10 TommySorensen