-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
More work on mutation conventions support
- Loading branch information
1 parent
eac8c74
commit 741a70b
Showing
12 changed files
with
372 additions
and
308 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
namespace FairyBread; | ||
|
||
[GraphQLName("ValidationError")] | ||
public class DefaultValidationError | ||
{ | ||
private DefaultValidationError( | ||
string message) | ||
{ | ||
Message = "A validation error occurred"; | ||
// TODO: Complete a nice rich implementation. | ||
} | ||
|
||
public static DefaultValidationError CreateErrorFrom( | ||
ArgumentValidationResult argValRes) | ||
{ | ||
return new(""); | ||
} | ||
|
||
public string Message { get; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
| ||
namespace FairyBread; | ||
|
||
/// <summary> | ||
/// <para> | ||
/// Instructs FairyBread, when using HotChocolate's mutation conventions | ||
/// and with FairyBread configured with UseMutationConventions as true, | ||
/// to report validation errors in the global errors array of the GraphQL | ||
/// response (rather than inline in this mutation fields' local errors array). | ||
/// </para> | ||
/// <para> | ||
/// This attribute is intended to allow a progressive transition from FairyBread's | ||
/// former behavior with mutation conventions (global errors) to the | ||
/// new behavior (local errors). Fields that you aren't ready to modernize can | ||
/// be annotated in the meantime. | ||
/// </para> | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] | ||
public class GlobalValidationErrorsAttribute : ObjectFieldDescriptorAttribute | ||
{ | ||
protected override void OnConfigure( | ||
IDescriptorContext context, | ||
IObjectFieldDescriptor descriptor, | ||
MemberInfo memberInfo) | ||
{ | ||
descriptor.GlobalValidationErrors(); | ||
} | ||
} | ||
|
||
public static class GlobalValidationErrorsObjectFieldDescriptorExtensions | ||
{ | ||
/// <summary> | ||
/// <para> | ||
/// Instructs FairyBread, when using HotChocolate's mutation conventions | ||
/// and with FairyBread configured with UseMutationConventions as true, | ||
/// to report validation errors in the global errors array of the GraphQL | ||
/// response (rather than inline in this mutation fields' local errors array). | ||
/// </para> | ||
/// <para> | ||
/// This is intended to allow a progressive transition from FairyBread's | ||
/// former behavior with mutation conventions (global errors) to the | ||
/// new behavior (local errors). Fields that you aren't ready to modernize can | ||
/// use this in the meantime. | ||
/// </para> | ||
/// </summary> | ||
public static IObjectFieldDescriptor GlobalValidationErrors( | ||
this IObjectFieldDescriptor descriptor) | ||
{ | ||
descriptor.Extend().OnBeforeNaming((completionContext, objFieldDef) => | ||
{ | ||
objFieldDef.ContextData[WellKnownContextData.UsesGlobalErrors] = true; | ||
}); | ||
|
||
return descriptor; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using HotChocolate.Types; | ||
|
||
namespace FairyBread; | ||
|
||
internal class ValidationErrorConfigurer : MutationErrorConfiguration | ||
{ | ||
public override void OnConfigure( | ||
IDescriptorContext context, | ||
ObjectFieldDefinition fieldDef) | ||
{ | ||
var options = context.Services | ||
.GetRequiredService<IFairyBreadOptions>(); | ||
if (!options.UseMutationConventions) | ||
{ | ||
return; | ||
} | ||
|
||
var validatorRegistry = context.Services | ||
.GetRequiredService<IValidatorRegistry>(); | ||
|
||
// TODO: Can I get this? I think I need to PR this as Michael said he'd pass this through but hasn't. | ||
var objTypeDef = new ObjectTypeDefinition(); | ||
|
||
if (ValidationMiddlewareHelpers.NeedsMiddleware(options, validatorRegistry, objTypeDef, fieldDef)) | ||
{ | ||
ValidationMiddlewareHelpers.AddMiddleware(fieldDef); | ||
|
||
if (!fieldDef.ContextData.TryGetValue(WellKnownContextData.UsesGlobalErrors, out var usesGlobalErrorsObj) || | ||
usesGlobalErrorsObj is not bool usesGlobalErrors || | ||
usesGlobalErrors == false) | ||
{ | ||
fieldDef.AddErrorType(context, options.ValidationErrorType); | ||
|
||
// Set a flag for the errors handler | ||
fieldDef.ContextData[WellKnownContextData.UsesInlineErrors] = true; | ||
|
||
// Cleanup | ||
//fieldDef.ContextData.Remove(WellKnownContextData.UsesGlobalErrors); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.