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

Publicly expose ability to register an error type on a mutation field #6463

Merged
merged 3 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 32 additions & 2 deletions src/HotChocolate/Core/HotChocolate.Core.sln
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HotChocolate.Transport.Sock
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HotChocolate.Transport.Sockets", "..\AspNetCore\src\Transport.Sockets\HotChocolate.Transport.Sockets.csproj", "{AD65B505-8EDE-4B54-9298-6B71D0283EB1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HotChocolate.Subscriptions.Postgres.Tests", "test\Subscriptions.Postgres.Tests\HotChocolate.Subscriptions.Postgres.Tests.csproj", "{AA04790E-58AC-40CC-9DEC-570D78D252AD}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HotChocolate.Subscriptions.Postgres.Tests", "test\Subscriptions.Postgres.Tests\HotChocolate.Subscriptions.Postgres.Tests.csproj", "{AA04790E-58AC-40CC-9DEC-570D78D252AD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HotChocolate.Subscriptions.Postgres", "src\Subscriptions.Postgres\HotChocolate.Subscriptions.Postgres.csproj", "{B98BC881-DDDD-4988-AC43-187C733895CD}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HotChocolate.Subscriptions.Postgres", "src\Subscriptions.Postgres\HotChocolate.Subscriptions.Postgres.csproj", "{B98BC881-DDDD-4988-AC43-187C733895CD}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HotChocolate.Transport.Http", "..\AspNetCore\src\Transport.Http\HotChocolate.Transport.Http.csproj", "{D1B67116-DB1C-4CE7-954F-AD9E17C06061}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HotChocolate.Transport.Abstractions", "..\AspNetCore\src\Transport.Abstractions\HotChocolate.Transport.Abstractions.csproj", "{08D371ED-04DE-4CE3-AD1B-F6CC6D341706}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -931,6 +935,30 @@ Global
{B98BC881-DDDD-4988-AC43-187C733895CD}.Release|x64.Build.0 = Release|Any CPU
{B98BC881-DDDD-4988-AC43-187C733895CD}.Release|x86.ActiveCfg = Release|Any CPU
{B98BC881-DDDD-4988-AC43-187C733895CD}.Release|x86.Build.0 = Release|Any CPU
{D1B67116-DB1C-4CE7-954F-AD9E17C06061}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D1B67116-DB1C-4CE7-954F-AD9E17C06061}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D1B67116-DB1C-4CE7-954F-AD9E17C06061}.Debug|x64.ActiveCfg = Debug|Any CPU
{D1B67116-DB1C-4CE7-954F-AD9E17C06061}.Debug|x64.Build.0 = Debug|Any CPU
{D1B67116-DB1C-4CE7-954F-AD9E17C06061}.Debug|x86.ActiveCfg = Debug|Any CPU
{D1B67116-DB1C-4CE7-954F-AD9E17C06061}.Debug|x86.Build.0 = Debug|Any CPU
{D1B67116-DB1C-4CE7-954F-AD9E17C06061}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D1B67116-DB1C-4CE7-954F-AD9E17C06061}.Release|Any CPU.Build.0 = Release|Any CPU
{D1B67116-DB1C-4CE7-954F-AD9E17C06061}.Release|x64.ActiveCfg = Release|Any CPU
{D1B67116-DB1C-4CE7-954F-AD9E17C06061}.Release|x64.Build.0 = Release|Any CPU
{D1B67116-DB1C-4CE7-954F-AD9E17C06061}.Release|x86.ActiveCfg = Release|Any CPU
{D1B67116-DB1C-4CE7-954F-AD9E17C06061}.Release|x86.Build.0 = Release|Any CPU
{08D371ED-04DE-4CE3-AD1B-F6CC6D341706}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{08D371ED-04DE-4CE3-AD1B-F6CC6D341706}.Debug|Any CPU.Build.0 = Debug|Any CPU
{08D371ED-04DE-4CE3-AD1B-F6CC6D341706}.Debug|x64.ActiveCfg = Debug|Any CPU
{08D371ED-04DE-4CE3-AD1B-F6CC6D341706}.Debug|x64.Build.0 = Debug|Any CPU
{08D371ED-04DE-4CE3-AD1B-F6CC6D341706}.Debug|x86.ActiveCfg = Debug|Any CPU
{08D371ED-04DE-4CE3-AD1B-F6CC6D341706}.Debug|x86.Build.0 = Debug|Any CPU
{08D371ED-04DE-4CE3-AD1B-F6CC6D341706}.Release|Any CPU.ActiveCfg = Release|Any CPU
{08D371ED-04DE-4CE3-AD1B-F6CC6D341706}.Release|Any CPU.Build.0 = Release|Any CPU
{08D371ED-04DE-4CE3-AD1B-F6CC6D341706}.Release|x64.ActiveCfg = Release|Any CPU
{08D371ED-04DE-4CE3-AD1B-F6CC6D341706}.Release|x64.Build.0 = Release|Any CPU
{08D371ED-04DE-4CE3-AD1B-F6CC6D341706}.Release|x86.ActiveCfg = Release|Any CPU
{08D371ED-04DE-4CE3-AD1B-F6CC6D341706}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -1001,6 +1029,8 @@ Global
{AD65B505-8EDE-4B54-9298-6B71D0283EB1} = {7637D30E-7339-4D4E-9424-87CF2394D234}
{AA04790E-58AC-40CC-9DEC-570D78D252AD} = {7462D089-D350-44D6-8131-896D949A65B7}
{B98BC881-DDDD-4988-AC43-187C733895CD} = {37B9D3B1-CA34-4720-9A0B-CFF1E64F52C2}
{D1B67116-DB1C-4CE7-954F-AD9E17C06061} = {7637D30E-7339-4D4E-9424-87CF2394D234}
{08D371ED-04DE-4CE3-AD1B-F6CC6D341706} = {7637D30E-7339-4D4E-9424-87CF2394D234}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {E4D94C77-6657-4630-9D42-0A9AC5153A1B}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,28 +406,8 @@ public static IObjectFieldDescriptor Error(
throw new ArgumentNullException(nameof(errorType));
}

descriptor.Extend().OnBeforeCreate(ConfigureField);
descriptor.Extend().OnBeforeCreate((ctx, d) => d.AddErrorType(ctx, errorType));

return descriptor;

void ConfigureField(IDescriptorContext c, ObjectFieldDefinition d)
{
var definitions = ErrorFactoryCompiler.Compile(errorType);

if (!d.ContextData.TryGetValue(ErrorDefinitions, out var value) ||
value is not List<ErrorDefinition> errorFactories)
{
errorFactories = new List<ErrorDefinition>();
d.ContextData[ErrorDefinitions] = errorFactories;
}

errorFactories.AddRange(definitions);

foreach (var definition in definitions)
{
var typeRef = c.TypeInspector.GetTypeRef(definition.SchemaType);
d.Dependencies.Add(new TypeDependency(typeRef));
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using static HotChocolate.Properties.TypeResources;
using static HotChocolate.Types.ErrorContextDataKeys;

#nullable enable

namespace HotChocolate.Types;

/// <summary>
/// Provides extensions to the <see cref="ObjectFieldDefinition"/> for the mutation convention.
/// </summary>
public static class ObjectFieldDefinitionExtensions
{
/// <summary>
/// Registers an error type with this field for the mutation convention
/// to pick up.
/// </summary>
/// <param name="fieldDefinition">The object field definition.</param>
/// <param name="descriptorContext">The descriptor context.</param>
/// <param name="errorType">
/// The type of the exception, the class with factory methods or the error with an exception
/// as the argument. See the examples in <see cref="Error"/>.
/// </param>
public static void AddErrorType(
this ObjectFieldDefinition fieldDefinition,
IDescriptorContext descriptorContext,
Type errorType)
{
if (fieldDefinition is null)
{
throw new ArgumentNullException(nameof(fieldDefinition));
}

if (descriptorContext is null)
{
throw new ArgumentNullException(nameof(descriptorContext));
}

if (errorType is null)
{
throw new ArgumentNullException(nameof(errorType));
}

if (descriptorContext.ContextData[MutationContextDataKeys.Options] == null)
{
var richMessage = string.Format(
MutationConvention_ShouldBeEnabled_WhenAddingErrorType,
errorType.Name,
fieldDefinition.Name);

var schemaError = SchemaErrorBuilder.New()
.SetMessage(richMessage)
.Build();

throw new SchemaException(schemaError);
}

var definitions = ErrorFactoryCompiler.Compile(errorType);

if (!fieldDefinition.ContextData.TryGetValue(ErrorDefinitions, out var value) ||
value is not List<ErrorDefinition> errorFactories)
{
errorFactories = new List<ErrorDefinition>();
fieldDefinition.ContextData[ErrorDefinitions] = errorFactories;
}

errorFactories.AddRange(definitions);

foreach (var definition in definitions)
{
var typeRef = descriptorContext.TypeInspector.GetTypeRef(definition.SchemaType);
fieldDefinition.Dependencies.Add(new TypeDependency(typeRef));
}
}
}
Loading