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

Added support for MutationConventions #80

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

FleloShe
Copy link

@FleloShe FleloShe commented Jun 8, 2022

Added support for MutationConventions. Must use "[Error(typeof(ValidationException))]" on mutations with enabled conventions.

At the current time it is necessary to overwrite the DefaultValidationErrorsHandler to support errors apart from the "ValidationException" from FluentValidation

…tionException))]" on mutations with enabled conventions
@Jonatthu
Copy link

@benmccallum any feedback on this?

@Jonatthu
Copy link

@FleloShe I do not think this is working, I am testing this locally

            if (context.ContextData.ContainsKey(WellKnownContextData.ValidatorDescriptorsParams))
            {
                throw new AggregateException(invalidResults.Select(x => new ValidationException(x.Result.Errors)));
            }

This line is not hitting

@FleloShe
Copy link
Author

It absolutely does....
image

@FleloShe
Copy link
Author

Maybe you did not enable MutationConventions for your endpoint?

Checkout https://chillicream.com/docs/hotchocolate/defining-a-schema/mutations/#errors for further reading.

Some hints

Use this on your endpoint:

        [Error(typeof(ValidationException))]
        [UseMutationConvention()]
        public async Task<YourModel?> YourMethod(...)
        {
           ...
        }

And this in your startup:

                .AddMutationConventions(
                    new MutationConventionOptions
                    {
                        //InputArgumentName = "input",
                        //InputTypeNamePattern = "{MutationName}Input",
                        PayloadTypeNamePattern = "{MutationName}Payload",
                        PayloadErrorTypeNamePattern = "{MutationName}Error",
                        PayloadErrorsFieldName = "errors",
                        ApplyToAllMutations = false
                    })

Also make sure you added a validator for your model.

@aradalvand
Copy link

If I'm not mistaken using Hot Chocolate's mutation conventions means you'll have your mutation input arguments directly as parameters on the corresponding mutation method, so I'm not sure how we could use FluentValidation in that case? What am I missing?

@TheWillard
Copy link

@FleloShe thanks for your work! I tried this branch myself and I couldn't get it to work. I pushed a small example (https://github.com/TheWillard/mutation-convention-fairybread-validation) where only the CreateAuthor3 Mutation Input gets validated. If you got time maybe you could take a look and tell me what I did wrong.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants