Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,20 @@ namespace HotChocolate.Types;
public abstract class MutationErrorConfiguration
{
/// <summary>
/// Allows to register errors with a mutation.
/// Override to register error dependencies.
/// </summary>
/// <param name="context">
/// The descriptor context.
/// </param>
/// <returns>
/// Returns the dependency references.
/// </returns>
public virtual IEnumerable<TypeReference> OnResolveDependencies(
IDescriptorContext context)
=> Array.Empty<TypeReference>();

/// <summary>
/// Override to register errors with a mutation.
/// </summary>
/// <param name="context">
/// The descriptor context.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
using System.Linq;

namespace HotChocolate.Types;

internal sealed class MutationErrorTypeInterceptor<T>(T errorRegistrar)
: TypeInterceptor
where T : MutationErrorConfiguration
{
internal override void OnBeforeCreateSchemaInternal(
IDescriptorContext context,
ISchemaBuilder schemaBuilder)
{
var b = (SchemaBuilder)schemaBuilder;
foreach (var typeReference in errorRegistrar.OnResolveDependencies(context))
{
b.AddTypeReference(typeReference);
}
}

public override IEnumerable<TypeReference> RegisterMoreTypes(
IReadOnlyCollection<ITypeDiscoveryContext> discoveryContexts)
{
var context = discoveryContexts.First();
return errorRegistrar.OnResolveDependencies(context.DescriptorContext);
}

public override void OnBeforeCompleteMutationField(
ITypeCompletionContext completionContext,
ObjectFieldDefinition mutationField)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,11 @@ public static void AddErrorType(
}

errorFactories.AddRange(definitions);

foreach (var definition in definitions)
{
var typeRef = descriptorContext.TypeInspector.GetTypeRef(definition.SchemaType);
fieldDefinition.Dependencies.Add(new TypeDependency(typeRef));
}
}
}
10 changes: 10 additions & 0 deletions src/HotChocolate/Core/src/Types/SchemaBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,16 @@ public ISchemaBuilder AddType(INamedTypeExtension typeExtension)
_types.Add(_ => TypeReference.Create(typeExtension));
return this;
}

internal void AddTypeReference(TypeReference typeReference)
{
if (typeReference is null)
{
throw new ArgumentNullException(nameof(typeReference));
}

_types.Add(_ => typeReference);
}

/// <inheritdoc />
public ISchemaBuilder AddDirectiveType(DirectiveType type)
Expand Down
Loading