-
-
Notifications
You must be signed in to change notification settings - Fork 326
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
feat: make all params optional #858 #894
Conversation
Someone is attempting to deploy a commit to a Personal Account owned by @anymaniax on Vercel. @anymaniax first needs to authorize it. |
Hello @Maxim-Mazurok, could it cause breaking chance for others? |
commented on the issue, before seeing the PR. so re-posting here to keep closer to the code: why make them optional? shouldn't you follow whatever the schema says? if you want them optional, you can wrap the generated composable in your own which handles this, no? |
Yes, I will make this configurable
Because otherwise, Orval has a feature that doesn't make sense. const getPetsQuery = useGetPetsQuery();
const firstPetId = computed(() => getPetsQuery.data.value?.data?.[0]);
const petInfoQuery = useGetPetInfoQuery({ petId: firstPetId }); This will magically fetch pets, and then as soon as they are fetched - it'll fetch the first pet info.
Well, that's a good point, I guess you might end up with invalid queries this way. const firstPetId = computed(() => undefined);
const petInfoQuery = useGetPetInfoQuery({ petId: firstPetId }); In that example However, there's another interesting thing: if you make
Yeah, I guess that could be an option. However, this kinda defeats the purpose of using Orval... I don't want to manually wrap generated composables, I want to reduce manual to a minimum by generating composables that work for me. |
this is actually my opinion on the matter - i don't think the feature does makes sense! i personally think that adding for generated composables, i don't want them to try to be smart about enabling the query or not. i'd rather a TS error if i have passed the incorrect type. it would be a pain having to dig into generated files to work out why a query is silently not running. as a developer if i want to disable a query until some conditions are met, it's much easier for me to code this and get it right for my use case, than for orval to take a guess at it, and potentially get it very wrong. things get even more confusing if e.g. there are boolean props. i can legitimately pass of course i can override orval's own by passing
i don't see why this is less preferable? const getPetsQuery = useGetPetsQuery();
const firstPetId = computed(() => getPetsQuery.data.value?.data?.[0]);
const enable = computed(() => firstPetId != null)
const petInfoQuery = useGetPetInfoQuery({ petId: firstPetId, enable }); sure it's one more line of code, but it's clear and self explanatory and does not rely on any magic 🧙
good point. i think this is a bug in orval and the type should be
whenever i've written query composables by hand, most of them do not use the enable functionality. it's been the exception not the rule. so the generated composables would be usable as-is most of the time. i don't want a tool like orval to eliminate writing any composables, just 95% of them. writing a wrapper for the 5% case is a fine trade off for greater correctness in general. |
I think most of the arguments are fair, I found it a bit unexpected that some queries were disabled due to falsy values. However, we've been using this approach for a while now, including this PR as a patch in our project. So I would like to keep it for now. Perhaps you could create a PR that adds a non-default configurable option to disable the "only enable when values are truthy" feature? This way it won't break existing users. And I would be interested in giving that PR a try. Also, I just remembered that this feature and this PR are only about route/url params. I'd say it's quite unlikely you'll have boolean type in the route, like My plan for this PR:
Would you guys agree to this plan, or would you still insist on removing this feature altogether? |
Looks like there are conflicts with this PR. |
Hello, yes, I'm back from the holidays and going to continue work on my PRs for Orval this or next week, will also connect with @anymaniax on discord, cheers :) |
@@ -83,6 +90,11 @@ export const getParams = ({ | |||
}, | |||
}); | |||
|
|||
let paramType = resolvedValue.value; | |||
if (output.allParamsOptional) { | |||
paramType = `${paramType} | undefined | null`; // TODO: maybe check that `paramType` isn't already undefined or null |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the core change directly related to the issue
@WickyNilliams this is ready for review now if you'd like to have a look |
It looks like @WickyNilliams is more involved in other projects, so I'll merge it for now to avoid conflicts, but still open to feedback, cheers! |
Status
READY
Description
Makes all query params optional to resolve #858
Todos
Steps to Test or Reproduce
See tests https://github.com/Maxim-Mazurok/orval/blob/858/samples/vue-query/src/tests/all-params-optional.test-d.ts