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

Proper Filter Type Merging #2619

Merged
merged 7 commits into from
Nov 20, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
using System.Linq;
using HotChocolate.Configuration;
using HotChocolate.Resolvers;
using HotChocolate.Types;
using HotChocolate.Types.Descriptors;
using HotChocolate.Types.Descriptors.Definitions;

namespace HotChocolate.Types
namespace HotChocolate.Internal
{
internal static class TypeExtensionHelper
public static class TypeExtensionHelper
{
public static void MergeObjectFields(
ITypeCompletionContext context,
Expand Down
1 change: 1 addition & 0 deletions src/HotChocolate/Core/src/Types/Types/EnumTypeExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Globalization;
using System.Linq;
using HotChocolate.Configuration;
using HotChocolate.Internal;
using HotChocolate.Properties;
using HotChocolate.Types.Descriptors;
using HotChocolate.Types.Descriptors.Definitions;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using HotChocolate.Configuration;
using HotChocolate.Internal;
using HotChocolate.Properties;
using HotChocolate.Types.Descriptors;
using HotChocolate.Types.Descriptors.Definitions;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using HotChocolate.Configuration;
using HotChocolate.Internal;
using HotChocolate.Properties;
using HotChocolate.Types.Descriptors;
using HotChocolate.Types.Descriptors.Definitions;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using HotChocolate.Configuration;
using HotChocolate.Internal;
using HotChocolate.Properties;
using HotChocolate.Types.Descriptors;
using HotChocolate.Types.Descriptors.Definitions;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Linq;
using HotChocolate.Configuration;
using HotChocolate.Internal;
using HotChocolate.Properties;
using HotChocolate.Types.Descriptors;
using HotChocolate.Types.Descriptors.Definitions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,6 @@ public NameString GetOperationName(int operation)
{
configure(descriptor);
}

if (descriptor is FilterInputTypeDescriptor inputTypeDescriptor)
{
inputTypeDescriptor.CreateDefinition();
}
}
}

Expand All @@ -210,7 +205,7 @@ public NameString GetOperationName(int operation)
_provider.ConfigureField(_argumentName, descriptor);

public bool TryGetHandler(
ITypeDiscoveryContext context,
ITypeCompletionContext context,
IFilterInputTypeDefinition typeDefinition,
IFilterFieldDefinition fieldDefinition,
[NotNullWhen(true)] out IFilterFieldHandler? handler)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using System;
using System.Collections.Generic;
using System.Linq;
using HotChocolate.Configuration;
using HotChocolate.Internal;
using HotChocolate.Types.Descriptors.Definitions;

namespace HotChocolate.Data.Filters
{
internal static class FilterTypeExtensionHelper
{
public static void MergeFilterInputTypeDefinitions(
ITypeCompletionContext context,
FilterInputTypeDefinition extensionDefinition,
FilterInputTypeDefinition typeDefinition)
{
TypeExtensionHelper.MergeContextData(
extensionDefinition,
typeDefinition);

TypeExtensionHelper.MergeDirectives(
context,
extensionDefinition.Directives,
typeDefinition.Directives);

MergeFilterFieldDefinitions(
context,
extensionDefinition.Fields,
typeDefinition.Fields);

TypeExtensionHelper.MergeConfigurations(
extensionDefinition.Configurations,
typeDefinition.Configurations);
}

private static void MergeFilterFieldDefinitions(
ITypeCompletionContext context,
IList<InputFieldDefinition> extensionFields,
IList<InputFieldDefinition> typeFields)
{
MergeFilterFields(
context,
extensionFields,
typeFields,
(fields, extensionField, typeField) =>
{
if (typeField is FilterFieldDefinition filterTypeField &&
extensionField is FilterFieldDefinition filterExtensionField)
{
filterTypeField.Handler ??= filterExtensionField.Handler;
}
typeField.Description ??= extensionField.Description;
typeField.NativeDefaultValue ??= extensionField.NativeDefaultValue;
});
}

private static void MergeFilterFields(
ITypeCompletionContext context,
IList<InputFieldDefinition> extensionFields,
IList<InputFieldDefinition> typeFields,
Action<IList<InputFieldDefinition>, InputFieldDefinition, InputFieldDefinition>
action)
{
foreach (var extensionField in extensionFields)
{
InputFieldDefinition? typeField;
if (extensionField is FilterOperationFieldDefinition operationFieldDefinition)
{
typeField = typeFields.OfType<FilterOperationFieldDefinition>()
.FirstOrDefault(t => t.Id == operationFieldDefinition.Id);
}
else
{
typeField = typeFields.FirstOrDefault(
t => t.Name.Equals(extensionField.Name));
}

if (typeField is null)
{
typeFields.Add(extensionField);
}
else
{
TypeExtensionHelper.MergeDirectives(
context,
extensionField.Directives,
typeField.Directives);

TypeExtensionHelper.MergeContextData(extensionField, typeField);

action(typeFields, extensionField, typeField);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,44 +14,49 @@ public class FilterTypeInterceptor

public override bool CanHandle(ITypeSystemObjectContext context) => true;

public override void OnBeforeRegisterDependencies(
ITypeDiscoveryContext discoveryContext,
public override void OnBeforeCompleteName(
ITypeCompletionContext completionContext,
DefinitionBase? definition,
IDictionary<string, object?> contextData)
{
if (definition is FilterInputTypeDefinition def)
{
IFilterConvention convention = GetConvention(
discoveryContext.DescriptorContext,
completionContext.DescriptorContext,
def.Scope);

var descriptor = FilterInputTypeDescriptor.From(
discoveryContext.DescriptorContext,
def,
var descriptor = FilterInputTypeDescriptor.New(
completionContext.DescriptorContext,
def.EntityType,
def.Scope);

var typeReference = TypeReference.Create(
discoveryContext.Type,
completionContext.Type,
def.Scope);

convention.ApplyConfigurations(typeReference, descriptor);

FilterTypeExtensionHelper.MergeFilterInputTypeDefinitions(
completionContext,
descriptor.CreateDefinition(),
def);

foreach (InputFieldDefinition field in def.Fields)
{
if (field is FilterFieldDefinition filterFieldDefinition)
{
if (discoveryContext.TryPredictTypeKind(
if (completionContext.TryPredictTypeKind(
filterFieldDefinition.Type,
out TypeKind kind) &&
kind != TypeKind.Scalar && kind != TypeKind.Enum)
{
field.Type = field.Type.With(scope: discoveryContext.Scope);
field.Type = field.Type.With(scope: completionContext.Scope);
}

if (filterFieldDefinition.Handler is null)
{
if (convention.TryGetHandler(
discoveryContext,
completionContext,
def,
filterFieldDefinition,
out IFilterFieldHandler? handler))
Expand All @@ -67,20 +72,14 @@ public class FilterTypeInterceptor
}
}
}
}
}

public override void OnBeforeCompleteName(
ITypeCompletionContext completionContext,
DefinitionBase? definition,
IDictionary<string, object?> contextData)
{
if (definition is FilterInputTypeDefinition def &&
def.Scope != null)
{
definition.Name = completionContext.Scope +
"_" +
definition.Name;

if (def.Scope is not null)
{
definition.Name = completionContext.Scope +
"_" +
definition.Name;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public interface IFilterConvention : IConvention
IFilterInputTypeDescriptor descriptor);

bool TryGetHandler(
ITypeDiscoveryContext context,
ITypeCompletionContext context,
IFilterInputTypeDefinition typeDefinition,
IFilterFieldDefinition fieldDefinition,
[NotNullWhen(true)] out IFilterFieldHandler? handler);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public abstract class QueryableBooleanOperationHandler
protected abstract int Operation { get; }

public override bool CanHandle(
ITypeDiscoveryContext context,
ITypeCompletionContext context,
IFilterInputTypeDefinition typeDefinition,
IFilterFieldDefinition fieldDefinition)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public abstract class QueryableComparableOperationHandler
protected ITypeConverter TypeConverter { get; }

public override bool CanHandle(
ITypeDiscoveryContext context,
ITypeCompletionContext context,
IFilterInputTypeDefinition typeDefinition,
IFilterFieldDefinition fieldDefinition)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class QueryableEnumEqualsHandler
}

public override bool CanHandle(
ITypeDiscoveryContext context,
ITypeCompletionContext context,
IFilterInputTypeDefinition typeDefinition,
IFilterFieldDefinition fieldDefinition)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class QueryableEnumInHandler
}

public override bool CanHandle(
ITypeDiscoveryContext context,
ITypeCompletionContext context,
IFilterInputTypeDefinition typeDefinition,
IFilterFieldDefinition fieldDefinition)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class QueryableEnumNotEqualsHandler
}

public override bool CanHandle(
ITypeDiscoveryContext context,
ITypeCompletionContext context,
IFilterInputTypeDefinition typeDefinition,
IFilterFieldDefinition fieldDefinition)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class QueryableEnumNotInHandler
}

public override bool CanHandle(
ITypeDiscoveryContext context,
ITypeCompletionContext context,
IFilterInputTypeDefinition typeDefinition,
IFilterFieldDefinition fieldDefinition)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class QueryableListAnyOperationHandler
protected ITypeConverter TypeConverter { get; }

public override bool CanHandle(
ITypeDiscoveryContext context,
ITypeCompletionContext context,
IFilterInputTypeDefinition typeDefinition,
IFilterFieldDefinition fieldDefinition)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public abstract class QueryableListOperationHandlerBase
protected abstract int Operation { get; }

public override bool CanHandle(
ITypeDiscoveryContext context,
ITypeCompletionContext context,
IFilterInputTypeDefinition typeDefinition,
IFilterFieldDefinition fieldDefinition)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class QueryableDataOperationHandler
protected virtual int Operation => DefaultOperations.Data;

public override bool CanHandle(
ITypeDiscoveryContext context,
ITypeCompletionContext context,
IFilterInputTypeDefinition typeDefinition,
IFilterFieldDefinition fieldDefinition)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class QueryableDefaultFieldHandler
: FilterFieldHandler<QueryableFilterContext, Expression>
{
public override bool CanHandle(
ITypeDiscoveryContext context,
ITypeCompletionContext context,
IFilterInputTypeDefinition typeDefinition,
IFilterFieldDefinition fieldDefinition) =>
!(fieldDefinition is FilterOperationFieldDefinition) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public abstract class QueryableStringOperationHandler : QueryableOperationHandle
protected abstract int Operation { get; }

public override bool CanHandle(
ITypeDiscoveryContext context,
ITypeCompletionContext context,
IFilterInputTypeDefinition typeDefinition,
IFilterFieldDefinition fieldDefinition)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public abstract class FilterFieldHandler<TContext, T>

/// <inheritdoc />
public abstract bool CanHandle(
ITypeDiscoveryContext context,
ITypeCompletionContext context,
IFilterInputTypeDefinition typeDefinition,
IFilterFieldDefinition fieldDefinition);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public interface IFilterFieldHandler
/// <param name="fieldDefinition">The definition of the field</param>
/// <returns>Returns true if the field can be handled</returns>
bool CanHandle(
ITypeDiscoveryContext context,
ITypeCompletionContext context,
IFilterInputTypeDefinition typeDefinition,
IFilterFieldDefinition fieldDefinition);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
For more details look at the `Errors` property.

1. For the field bar of type FooFilterInput was no handler found.
For the field bar of type FooFilterInput was no handler found.
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
For more details look at the `Errors` property.

1. For the field eq of type TestOperationFilterTypeFilterInput was no handler found.
For the field eq of type TestOperationFilterTypeFilterInput was no handler found.