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

No import statements for HTTP exceptions (react-query) #1267

Open
ANTOSzbk opened this issue Mar 17, 2024 · 5 comments
Open

No import statements for HTTP exceptions (react-query) #1267

ANTOSzbk opened this issue Mar 17, 2024 · 5 comments
Assignees
Labels
bug Something isn't working tanstack-query TanStack Query related issue

Comments

@ANTOSzbk
Copy link

ANTOSzbk commented Mar 17, 2024

What are the steps to reproduce this issue?

  1. Have Exception defined in your Swagger API like this:
    image
  2. Generate react-query client code from Swagger docs using npx orval and config:
export default defineConfig({
  myproject: {
    input: './api/docs.json',
    output: {
      mode: 'tags',
      target: './src/queries',
      schemas: './api/schemas',
      client: 'react-query',
      mock: true,
      override: {
        mutator: {
          path: './src/lib/api/client.ts',
          name: 'axiosClient',
        },
        query: {
          queryOptions: {
            path: './src/lib/api/queries.ts',
            name: 'paginationQueryOptions',
          },
          signal: true,
          useQuery: true,
          useInfinite: true,
          useInfiniteQueryParam: 'page',
        },
      },
      tsconfig: './tsconfig.json',
      clean: true,
    },
  },
});

What happens?

Orval understands there are exceptions schemas and creates them createGroupChannel401.ts, createGroupChannel409.ts, uses them within react-query code but does not import them from schemas directory.

export type CreateGroupChannelMutationResult = NonNullable<Awaited<ReturnType<typeof createGroupChannel>>>
export type CreateGroupChannelMutationBody = BodyType<CreateGroupChannelDto>
/* no import statements for exceptions below */
export type CreateGroupChannelMutationError = ErrorType<CreateGroupChannel400 | CreateGroupChannel401 | CreateGroupChannel409>

What were you expecting to happen?

Create import statements for exceptions.

Any logs, error output, etc?

No specific errors.

What versions are you using?

Operating System: MacOS Sonoma 14.3.1 (M3 Pro)
Package Version: 6.25.0
Browser Version: Chrome 122.0.6261.129

@melloware melloware added enhancement New feature or request bug Something isn't working and removed enhancement New feature or request labels Mar 17, 2024
@ANTOSzbk
Copy link
Author

I managed to create temporary workaround using npm library patch-package on @orval/query, but I suppose correct patch should be applied to @orval/core package.

diff --git a/node_modules/@orval/query/dist/index.js b/node_modules/@orval/query/dist/index.js
index c8621a2..66842f9 100644
--- a/node_modules/@orval/query/dist/index.js
+++ b/node_modules/@orval/query/dist/index.js
@@ -2520,6 +2520,10 @@ var generateQueryHook = async ({
       ...queryKeyMutator ? [queryKeyMutator] : []
     ] : void 0;
   }
+  const exceptionImports = response.definition.errors.split('|').map(value => value.trim()).filter(value => value !== 'void' && value !== 'unknown' && value !== 'null' && value !== 'undefined');
+  if(exceptionImports.length > 0) {
+    implementation += `import { ${exceptionImports.join(', ') } } from '../../api/schemas';\n`;
+  }
   let isMutation = verb !== import_core2.Verbs.GET && override.query.useMutation;
   if ((operationQueryOptions == null ? void 0 : operationQueryOptions.useMutation) !== void 0) {
     isMutation = operationQueryOptions.useMutation;

@melloware
Copy link
Collaborator

Please submit a PR!

@ANTOSzbk
Copy link
Author

@melloware Wouldn't that be an issue on other clients? I cannot find code responsible for generating imports inside @orval/query and compiled @orval/core is too hard to read

@melloware
Copy link
Collaborator

If I had to guess yes it's a problem for all clients if the code is in core.

@melloware
Copy link
Collaborator

Adding @soartec-lab for thoughts

@soartec-lab soartec-lab self-assigned this Apr 3, 2024
@melloware melloware added the tanstack-query TanStack Query related issue label Apr 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working tanstack-query TanStack Query related issue
Projects
None yet
Development

No branches or pull requests

3 participants