Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More ways of structure keys #19

Closed
TommySorensen opened this issue Oct 7, 2022 · 6 comments · Fixed by #21
Closed

More ways of structure keys #19

TommySorensen opened this issue Oct 7, 2022 · 6 comments · Fixed by #21
Assignees
Labels
enhancement New feature or request

Comments

@TommySorensen
Copy link

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

@lukemorales lukemorales self-assigned this Oct 9, 2022
@lukemorales lukemorales added documentation Improvements or additions to documentation enhancement New feature or request and removed documentation Improvements or additions to documentation labels Oct 9, 2022
@lukemorales
Copy link
Owner

@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 lukemorales linked a pull request Oct 10, 2022 that will close this issue
@TommySorensen
Copy link
Author

@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>();
}

@lukemorales
Copy link
Owner

@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

@TommySorensen
Copy link
Author

TommySorensen commented Oct 12, 2022

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 😀

@lukemorales
Copy link
Owner

@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

@TommySorensen
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants