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 @@ -46,6 +46,11 @@ private static bool Equals(IExtendedType? x, IExtendedType? y)
return false;
}

if (IsKeyValuePair(x) || IsKeyValuePair(y))
{
return x.Equals(y);
}

return ReferenceEquals(x.Type, y.Type) && x.Kind == y.Kind;
}

Expand All @@ -66,6 +71,11 @@ public int GetHashCode(ExtendedTypeReference obj)

private static int GetHashCode(IExtendedType obj)
{
if (IsKeyValuePair(obj))
{
return obj.GetHashCode();
}

unchecked
{
var hashCode = (obj.Type.GetHashCode() * 397)
Expand All @@ -79,4 +89,7 @@ private static int GetHashCode(IExtendedType obj)
return hashCode;
}
}

private static bool IsKeyValuePair(IExtendedType type)
=> type.IsGeneric && type.Definition == typeof(KeyValuePair<,>);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using HotChocolate.Internal;
using HotChocolate.Types.Helpers;
using HotChocolate.Types;
using HotChocolate.Types.Descriptors;
using System.Diagnostics;

namespace HotChocolate.Configuration;

Expand All @@ -18,11 +16,6 @@ public override bool TryInferType(
TypeDiscoveryInfo typeInfo,
[NotNullWhen(true)] out TypeReference[]? schemaTypeRefs)
{
if (TryCreateKeyValuePairTypeRef(typeReference, typeInfo, out schemaTypeRefs))
{
return true;
}

TypeReference? schemaType;

if (typeInfo.IsStatic)
Expand Down Expand Up @@ -67,19 +60,39 @@ public override bool TryInferType(
}
else if (IsObjectType(typeInfo))
{
schemaType =
TypeInspector.CreateTypeRef(
typeof(ObjectType<>),
typeInfo,
typeReference);
if (typeInfo.IsKeyValuePair)
{
schemaType =
TypeReference.Create(
new KeyValuePairObjectType(typeInfo.ExtendedRuntimeType),
scope: typeReference.Scope);
}
else
{
schemaType =
TypeInspector.CreateTypeRef(
typeof(ObjectType<>),
typeInfo,
typeReference);
}
}
else if (IsInputObjectType(typeInfo))
{
schemaType =
TypeInspector.CreateTypeRef(
typeof(InputObjectType<>),
typeInfo,
typeReference);
if (typeInfo.IsKeyValuePair)
{
schemaType =
TypeReference.Create(
new KeyValuePairInputObjectType(typeInfo.ExtendedRuntimeType),
scope: typeReference.Scope);
}
else
{
schemaType =
TypeInspector.CreateTypeRef(
typeof(InputObjectType<>),
typeInfo,
typeReference);
}
}
else if (IsEnumType(typeInfo))
{
Expand Down Expand Up @@ -107,183 +120,6 @@ public override bool TryInferType(
return true;
}

private static bool TryCreateKeyValuePairTypeRef(
TypeReference typeReference,
TypeDiscoveryInfo typeInfo,
[NotNullWhen(true)] out TypeReference[]? schemaTypeRefs)
{
// Only extended type references can represent dictionaries.
if (typeReference is not ExtendedTypeReference { Type: { } extendedType })
{
schemaTypeRefs = null;
return false;
}

// We only handle generic KeyValuePair<TKey, TValue> types here.
if (!extendedType.IsGeneric
|| extendedType.Definition != typeof(KeyValuePair<,>))
{
schemaTypeRefs = null;
return false;
}

// For output types we create an object type to represent the key-value pair.
if (typeInfo.Context is TypeContext.Output or TypeContext.None)
{
var typeName = CreateKeyValuePairTypeName(
extendedType,
TypeKind.Object);

schemaTypeRefs =
[
TypeReference.Create(
typeName,
typeReference,
_ => CreateKeyValuePairObjectType(extendedType, typeName),
typeReference.Context,
typeReference.Scope)
];
return true;
}

// For input types we create an input object type instead.
if (typeInfo.Context is TypeContext.Input)
{
var typeName = CreateKeyValuePairTypeName(
extendedType,
TypeKind.InputObject);

schemaTypeRefs =
[
TypeReference.Create(
typeName,
typeReference,
_ => CreateKeyValuePairInputObjectType(extendedType, typeName),
typeReference.Context,
typeReference.Scope)
];
return true;
}

// We should never get here as all context options are exhausted above.
Debug.Fail("Unexpected TypeContext value.");
schemaTypeRefs = null;
return false;
}

private static ObjectType CreateKeyValuePairObjectType(
IExtendedType keyValuePairType,
string typeName)
{
var runtimeType = keyValuePairType.Type;
var keyType = keyValuePairType.TypeArguments[0];
var valueType = keyValuePairType.TypeArguments[1];
var keyProperty = runtimeType.GetProperty("Key")!;
var valueProperty = runtimeType.GetProperty("Value")!;

return new ObjectType(
descriptor =>
{
descriptor.Name(typeName);

descriptor.Field(keyProperty)
.Name("key")
.Extend()
.OnBeforeCreate(
(_, field) =>
{
field.SetMoreSpecificType(keyType, TypeContext.Output);
field.SourceType = runtimeType;
field.ResolverType = runtimeType;
});

descriptor.Field(valueProperty)
.Name("value")
.Extend()
.OnBeforeCreate(
(_, field) =>
{
field.SetMoreSpecificType(valueType, TypeContext.Output);
field.SourceType = runtimeType;
field.ResolverType = runtimeType;
});

descriptor.Extend()
.OnBeforeCreate(
(_, type) =>
{
type.RuntimeType = runtimeType;
type.FieldBindingType = typeof(object);
});
});
}

private static InputObjectType CreateKeyValuePairInputObjectType(
IExtendedType keyValuePairType,
string typeName)
{
var runtimeType = keyValuePairType.Type;
var keyType = keyValuePairType.TypeArguments[0];
var valueType = keyValuePairType.TypeArguments[1];
var keyProperty = runtimeType.GetProperty("Key")!;
var valueProperty = runtimeType.GetProperty("Value")!;
var keyGetter = keyProperty.GetMethod!;
var valueGetter = valueProperty.GetMethod!;

return new InputObjectType(
descriptor =>
{
descriptor.Name(typeName);

descriptor.Field("key")
.Extend()
.OnBeforeCreate(
(_, field) => field.SetMoreSpecificType(keyType, TypeContext.Input));

descriptor.Field("value")
.Extend()
.OnBeforeCreate(
(_, field) => field.SetMoreSpecificType(valueType, TypeContext.Input));

descriptor.Extend()
.OnBeforeCreate(
(_, type) =>
{
type.RuntimeType = runtimeType;
type.CreateInstance =
values => Activator.CreateInstance(runtimeType, values[0], values[1])!;
type.GetFieldData =
(obj, values) =>
{
values[0] = keyGetter.Invoke(obj, []);
values[1] = valueGetter.Invoke(obj, []);
};
});
});
}

private static string CreateKeyValuePairTypeName(IExtendedType type, TypeKind kind)
{
var keyType = type.TypeArguments[0];
var valueType = type.TypeArguments[1];
var keyName = keyType.Type.Name;
var valueName = valueType.Type.Name;

if (keyType.IsNullable && keyType.Type.IsValueType)
{
keyName = $"Nullable{keyName}";
}

if (valueType.IsNullable && valueType.Type.IsValueType)
{
valueName = $"Nullable{valueName}";
}

return kind is TypeKind.InputObject
? $"KeyValuePairOf{keyName}And{valueName}Input"
: $"KeyValuePairOf{keyName}And{valueName}";
}

public override bool TryInferKind(
TypeReference typeReference,
TypeDiscoveryInfo typeInfo,
Expand Down
10 changes: 10 additions & 0 deletions src/HotChocolate/Core/src/Types/Configuration/TypeDiscoverer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,16 @@ private bool TryInferTypes()
continue;
}

if (unresolvedTypeRef is ExtendedTypeReference fallbackTypeRef
&& _typeRegistry.TryGetNonInferredTypeRef(fallbackTypeRef, out var fallbackReference))
{
inferred = true;
_typeRegistry.TryRegister(fallbackTypeRef, fallbackReference);
_unregistered.Enqueue(fallbackReference, (TypeReferenceStrength.VeryWeak, _nextTypeRefIndex++));
_resolved.Add(unresolvedTypeRef);
continue;
}

// if we do not have a type binding or if we have a directive we will try to infer the type.
if (unresolvedTypeRef is ExtendedTypeReference or ExtendedTypeDirectiveReference
&& _context.TryInferSchemaType(unresolvedTypeRef, out var schemaTypeRefs))
Expand Down
49 changes: 49 additions & 0 deletions src/HotChocolate/Core/src/Types/Configuration/TypeRegistry.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics.CodeAnalysis;
using HotChocolate.Internal;
using HotChocolate.Types;
using HotChocolate.Types.Descriptors;
using HotChocolate.Utilities;
Expand Down Expand Up @@ -78,6 +79,54 @@ public bool TryGetTypeRef(
return _runtimeTypeRefs.TryGetValue(runtimeTypeRef, out typeRef);
}

public bool TryGetNonInferredTypeRef(
ExtendedTypeReference runtimeTypeRef,
[NotNullWhen(true)] out TypeReference? typeRef)
{
ArgumentNullException.ThrowIfNull(runtimeTypeRef);

if (RuntimeTypeBindingHelper.RequiresExactBinding(runtimeTypeRef.Type)
|| !IsKeyValuePair(runtimeTypeRef.Type))
{
typeRef = null;
return false;
}

foreach (var (candidateRef, candidateTypeRef) in _runtimeTypeRefs)
{
if (!candidateRef.Scope.EqualsOrdinal(runtimeTypeRef.Scope))
{
continue;
}

if (candidateRef.Context != runtimeTypeRef.Context
&& candidateRef.Context != TypeContext.None
&& runtimeTypeRef.Context != TypeContext.None)
{
continue;
}

if (candidateRef.Type.Type != runtimeTypeRef.Type.Type
|| candidateRef.Type.Kind != runtimeTypeRef.Type.Kind)
{
continue;
}

if (_typeRegister.TryGetValue(candidateTypeRef, out var registeredType)
&& !registeredType.IsInferred)
{
typeRef = candidateTypeRef;
return true;
}
}

typeRef = null;
return false;
}

private static bool IsKeyValuePair(IExtendedType type)
=> type.IsGeneric && type.Definition == typeof(KeyValuePair<,>);

public bool IsExplicitBinding(ExtendedTypeReference runtimeTypeRef)
{
ArgumentNullException.ThrowIfNull(runtimeTypeRef);
Expand Down
Loading
Loading