fabien0102 / openapi-codegen

A tool for generating code base on an OpenAPI schema.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

A way to change component type from murarion to query

xobotyi opened this issue · comments

For several reasons (big query is one of them) - we have couple endpoints that are queries but performed via POST
It would be very good if there would be a way to fix component code generation from mutation to query, since now generator detects endpoint as mutation (basing off request type i guess)

Hey!
You can use the forceReactQueryComponent for this use case.
Here is an example of openapi-codegen.config.ts

import { defineConfig } from "@openapi-codegen/cli";
import { forceReactQueryComponent, generateSchemaTypes, generateReactQueryComponents } from "@openapi-codegen/typescript";

export default defineConfig({
  example: {
    from: {
      /* ... */
    },
    outputDir: "./example"
    to: async (context) => {
      const filenamePrefix = "example";

      context.openAPIDocument = forceReactQueryComponent({
        openAPIDocument: context.openAPIDocument,
        component: "useQuery",
        operationId: "myOperationId"
      })

      const { schemasFiles } = await generateSchemaTypes(context, { filenamePrefix });
      await generateReactQueryComponents(context, {
        filenamePrefix,
        schemasFiles
      });
    }
  },
});

This should do it, if you have more than one, you can do a loop, this is regular javascript 😃

I hope this helps, and sorry for the late answer!