Shaddix / react-query-swagger

Generates react-query hooks based on Swagger API definitions
MIT License
122 stars 4 forks source link

Handle Vue ref parameters #16

Closed FedorSherbakov closed 1 year ago

FedorSherbakov commented 1 year ago

Allows to use refs which is really useful

Consider such example

const { data: todos, isFetched: areTodosFetched } = useGetTodosQuery();

const firstTodoId = computed(() => (areTodosFetched.value && todos.value?.[0]
  ? todos.value[0].id
  : undefined));

const { data: firstTodoNotes } = useGetTodoNotesQuery({ id: firstTodoId }, { enabled: areTodosFetched });

The second request will be triggered only after the first one is finished. So, we will know todo's id later only and using ref it becomes reactive

FedorSherbakov commented 1 year ago

Guess need to update something else to make types to accept ref as well

Should be like

import type { Ref } from 'vue';

export type GetTodoNotesQueryParameters = {
  id: number | Ref<number>;
};

But I'm not sure how to do that yet

Shaddix commented 1 year ago

Thanks for the suggestion! Couple of questions though (I'm not familiar with Vue, so sorry if it's something obvious :)):

  1. Does it make sense to make spread parameters of a Ref type as well? Currently we have 2 overloads:
    export function useGetPetByIdQuery(dto: GetPetByIdQueryParameters, ...)
    export function useGetPetByIdQuery(petId: number,...)

    Should the second one be changed as well to export function useGetPetByIdQuery(petId: Ref<number>,...)?

  2. Your solution with completely 'overriding' ReactQuery.liquid file works, but I hope you don't mind if I'd try to come up with something regex-based so that it's easier in support (if/when ReactQuery.liquid changes)
Shaddix commented 1 year ago

So, version 15.2.9 includes these Ref changes that you did, hopefully it works as expected