Skip to content

Prefix override for query keys/options factories #3790

Description

@farsil

Description

When used with the react-query client, I would like an option in the orval configuration file to configure explicitly what prefix to use for factories.

Current behavior

If I use orval with react-query client, it internally determines whether to use a use or get prefix based on the presence of a custom query options factory:

  const queryOptionsFnName = camel(
    queryKeyMutator || queryOptionsMutator || mutator?.isHook
      ? `use-${name}-queryOptions`
      : `get-${name}-queryOptions`,
  );

However, the use prefix is sometimes undesirable for query options, since the function may have to be used outside react (router loaders for example, with ensureQueryData or prefetchQuery). You can use it anyway of course but:

  1. It would be confusing in any react codebase
  2. If you have certain eslint rules configured, you may have to disable the rule about React functions

Proposed behavior

Orval should let the user decide what prefix to use (if any) for query options factories, and possibly for query keys factories as well.

Proposed solution

The default automatic prefix could detect whether the mutators are hooks or not:

  const queryOptionsFnName = camel(
    queryKeyMutator?.isHook || queryOptionsMutator?.isHook || mutator?.isHook
      ? `use-${name}-queryOptions`
      : `get-${name}-queryOptions`,
  );

Moreover, we could introduce two new settings (queryKeyFactoryPrefix and queryOptionsFactoryPrefix, for example) that would live inside query. An empty prefix is sometimes desirable: my OpenAPI yaml files already prefix get operations with get, so with the default get prefix (assuming no mutators) it would result in a silly getGetActivitiesQueryOptions.

I am willing to put the work myself if you like the idea.

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentationhelp wantedExtra attention is needed

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions