Skip to content

Missing param in mutationOptions #2039

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

Open
jiwanovski87 opened this issue Apr 17, 2025 · 2 comments
Open

Missing param in mutationOptions #2039

jiwanovski87 opened this issue Apr 17, 2025 · 2 comments
Labels
help wanted Extra attention is needed

Comments

@jiwanovski87
Copy link

Introduction

Within the orval.config.ts I have added a transformer for the input to add the api version number.
For the output I have also added a mutationOption. When generating the files we have the result that the url with the version param is added everywhere. But within the hooks we are missing the parameter.

How can I add the missing param to the hooks?

orval.config.ts

{
	"test-file": {
		input: {
			target: "../openapi/v3/openapi.yaml",
			override: {
				transformer: "./src/api/transformer/add-version.cjs"
			}
		},
		output: {
			mode: "tags-split",
			target: "./src/api/endpoints/test.ts",
			schemas: "./src/api/model",
			client: "react-query",
			override: {
				query: {
					useQuery: true,
					signal: true,
					mutationOptions: {
						path: "./src/api/mutator/custom-query-mutator-options.ts",
						name: "useCustomQueryMutatorOptions"
					},
					shouldSplitQueryKey: true
				},
				mutator: {
					path: "./src/api/mutator/custom-client.ts",
					name: "customClient"
				}
			}
		}
	}
}

add-version.cjs

/**
 * Transformer function for orval.
 *
 * @param {OpenAPIObject} schema
 * @return {OpenAPIObject}
 */
module.exports = (inputSchema) => ({
    ...inputSchema,
    paths: Object.entries(inputSchema.paths || {}).reduce(
      (acc, [path, pathItem]) => Object.assign({}, acc, {
        [`api/v{version}${path}`]: Object.entries(pathItem).reduce(
          (pathItemAcc, [verb, operation]) => Object.assign({}, pathItemAcc, {
            [verb]: {
              ...operation,
              parameters: [
                ...(operation.parameters || []),
                {
                  name: 'version',
                  in: 'path',
                  required: true,
                  schema: {
                    type: 'number',
                    default: 1,
                  },
                },
              ],
            },
          }),
          {},
        ),
      }),
      {},
    ),
  });

Generated result with issue

export const useCreateEntityMutationOptions = <
	TError = ErrorType<unknown>,
	TContext = unknown,
>(options?: {
	mutation?: UseMutationOptions<
		Awaited<ReturnType<typeof createEntity>>,
		TError,
		{
			entityId: string | undefined | null;
			data: BodyType<EntitiesEntityRequestBody>;
			version?: number | undefined | null;
		},
		TContext
	>;
	request?: SecondParameter<typeof customClient>;
}): UseMutationOptions<
	Awaited<ReturnType<typeof createEntity>>,
	TError,
	{
		entityId: string | undefined | null;
		data: BodyType<EntitysEntityRequestBody>;
		version?: number | undefined | null;
	},
	TContext
> => {
	const mutationKey = ["createEntity"];
	const { mutation: mutationOptions, request: requestOptions } = options
		? options.mutation &&
			"mutationKey" in options.mutation &&
			options.mutation.mutationKey
			? options
			: { ...options, mutation: { ...options.mutation, mutationKey } }
		: { mutation: { mutationKey }, request: undefined };

	const mutationFn: MutationFunction<
		Awaited<ReturnType<typeof createEntity>>,
		{
			entityId: string | undefined | null;
			data: BodyType<EntitysEntityRequestBody>;
			version?: number | undefined | null;
		}
	> = (props) => {
		const { entityId, data, version } = props ?? {};

		return createEntity(entityId, data, version, requestOptions);
	};

	const customOptions = useCustomQueryMutatorOptions(
		{ ...mutationOptions, mutationFn },
                // Here is the issue
                // version is unknown
		{ url: `/api/v${version}/entity/` },
		{ operationId: "CreateEntity", operationName: "createEntity" },
	);

	return customOptions;
};
@melloware melloware added the question Further information is requested label Apr 17, 2025
@jiwanovski87
Copy link
Author

Still having this issue... Would be happy if someone could help

@melloware melloware added help wanted Extra attention is needed and removed question Further information is requested labels May 6, 2025
@melloware
Copy link
Collaborator

Probably a bug in the generator somewhere. PR is welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants