Skip to content

Commit

Permalink
Fixed mutation error dependency registration. (#6674)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Staib <michael@chillicream.com>
  • Loading branch information
PascalSenn and michaelstaib authored Dec 14, 2023
1 parent 926ba54 commit c20aec1
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 4,633 deletions.
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

0 comments on commit c20aec1

Please sign in to comment.