Skip to content

Commit

Permalink
Added tests that show we can rewrite scalar types (#2444)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib committed Oct 16, 2020
1 parent 66ced99 commit b13a2f4
Show file tree
Hide file tree
Showing 25 changed files with 2,660 additions and 111 deletions.
2 changes: 1 addition & 1 deletion src/HotChocolate/Core/src/Abstractions/Execution/IQuery.cs
Expand Up @@ -6,7 +6,7 @@
namespace HotChocolate.Execution
{
/// <summary>
/// Represent a executable query.
/// Represents an executable query.
/// </summary>
public interface IQuery
{
Expand Down
Expand Up @@ -13,6 +13,7 @@ internal sealed class SyntaxTypeReferenceHandler
: ITypeRegistrarHandler
{
private readonly ITypeInspector _typeInspector;
private readonly HashSet<string> _handled = new HashSet<string>();

public SyntaxTypeReferenceHandler(ITypeInspector typeInspector)
{
Expand All @@ -26,9 +27,9 @@ public SyntaxTypeReferenceHandler(ITypeInspector typeInspector)
foreach (SyntaxTypeReference typeReference in
typeReferences.OfType<SyntaxTypeReference>())
{
if (Scalars.TryGetScalar(
typeReference.Type.NamedType().Name.Value,
out Type? scalarType))
string name = typeReference.Type.NamedType().Name.Value;
if (_handled.Add(name) &&
Scalars.TryGetScalar(name, out Type? scalarType))
{
ExtendedTypeReference namedTypeReference =
_typeInspector.GetTypeRef(scalarType);
Expand Down
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using HotChocolate.Language;
using HotChocolate.Properties;
using HotChocolate.Types;
using HotChocolate.Types.Descriptors;
Expand Down Expand Up @@ -109,7 +110,7 @@ public IReadOnlyList<ISchemaError> DiscoverTypes()

if (_errors.Count == 0)
{
_typeRegistry.CompleteDiscovery();
_typeRegistry.CompleteDiscovery();
}

return _errors;
Expand Down Expand Up @@ -190,7 +191,7 @@ private void CollectErrors()
{
builder.SetTypeSystemObject(types[0].Type);
}
else if(types.Count > 1)
else if (types.Count > 1)
{
builder
.SetTypeSystemObject(types[0].Type)
Expand Down
11 changes: 11 additions & 0 deletions src/HotChocolate/Core/src/Types/Configuration/TypeRegistry.cs
Expand Up @@ -149,6 +149,17 @@ public void Register(RegisteredType registeredType)
}
}

public void Register(NameString typeName, ExtendedTypeReference typeReference)
{
if (typeReference is null)
{
throw new ArgumentNullException(nameof(typeReference));
}

typeName.EnsureNotEmpty(nameof(typeName));
_nameRefs[typeName] = typeReference;
}

public void Register(NameString typeName, RegisteredType registeredType)
{
if (registeredType is null)
Expand Down
4 changes: 3 additions & 1 deletion src/HotChocolate/Core/src/Types/SchemaBuilder.Setup.cs
Expand Up @@ -112,10 +112,12 @@ public static Schema Create(SchemaBuilder builder)
builder,
OperationType.Query,
visitor.QueryTypeName);

RegisterOperationName(
builder,
OperationType.Mutation,
visitor.MutationTypeName);

RegisterOperationName(
builder,
OperationType.Subscription,
Expand Down Expand Up @@ -334,7 +336,7 @@ public static Schema Create(SchemaBuilder builder)

Dictionary<OperationType, ITypeReference> operations =
builder._operations.ToDictionary(
t => t.Key,
t => t.Key,
t => t.Value(context.TypeInspector));

definition.QueryType = ResolveOperation(
Expand Down
Expand Up @@ -7,24 +7,10 @@ public abstract class FieldDefinitionBase
: DefinitionBase
, IHasDirectiveDefinition
{
private ITypeReference type;

/// <summary>
/// Gets the field type.
/// </summary>
public ITypeReference Type
{
get => type;
set
{
if(type is ExtendedTypeReference r &&
r.Type.Kind == ExtendedTypeKind.Extended)
{

}
type = value;
}
}
public ITypeReference Type { get; set; }

/// <summary>
/// Defines if this field is ignored and will
Expand Down
Expand Up @@ -4,6 +4,8 @@
using HotChocolate.Configuration;
using HotChocolate.Language;

#nullable enable

namespace HotChocolate.Types.Factories
{
internal sealed class ObjectTypeFactory
Expand Down Expand Up @@ -34,8 +36,7 @@ internal sealed class ObjectTypeFactory
if (bindingInfo.SourceType != null)
{
d.Extend().OnBeforeCreate(
t => t.RuntimeType = bindingInfo.SourceType);
d.Extend().OnBeforeCreate(t => t.RuntimeType = bindingInfo.SourceType);
}
foreach (DirectiveNode directive in node.Directives)
Expand Down Expand Up @@ -91,10 +92,10 @@ internal sealed class ObjectTypeFactory
}
}

string deprecactionReason = fieldDefinition.DeprecationReason();
if (!string.IsNullOrEmpty(deprecactionReason))
string deprecationReason = fieldDefinition.DeprecationReason();
if (!string.IsNullOrEmpty(deprecationReason))
{
fieldDescriptor.Deprecated(deprecactionReason);
fieldDescriptor.Deprecated(deprecationReason);
}

DeclareFieldArguments(fieldDescriptor, fieldDefinition);
Expand All @@ -112,8 +113,7 @@ internal sealed class ObjectTypeFactory
a =>
{
IArgumentDescriptor descriptor = a
.Description(
inputFieldDefinition.Description?.Value)
.Description(inputFieldDefinition.Description?.Value)
.Type(inputFieldDefinition.Type)
.DefaultValue(inputFieldDefinition.DefaultValue)
.SyntaxNode(inputFieldDefinition);
Expand Down
Expand Up @@ -32,8 +32,7 @@ internal sealed class UnionTypeFactory
if (bindingInfo.SourceType != null)
{
d.Extend().OnBeforeCreate(
t => t.RuntimeType = bindingInfo.SourceType);
d.Extend().OnBeforeCreate(t => t.RuntimeType = bindingInfo.SourceType);
}
foreach (NamedTypeNode namedType in node.Types)
Expand Down
Expand Up @@ -126,7 +126,7 @@ internal static class TypeDependencyHelper
{
RegisterAdditionalDependencies(context, field);

if (field.Type != null)
if (field.Type is not null)
{
context.RegisterDependency(field.Type,
TypeDependencyKind.Default);
Expand Down Expand Up @@ -158,13 +158,13 @@ internal static class TypeDependencyHelper

private static void RegisterFieldDependencies(
this ITypeDiscoveryContext context,
IEnumerable<OutputFieldDefinitionBase> fields)
IReadOnlyList<OutputFieldDefinitionBase> fields)
{
foreach (OutputFieldDefinitionBase field in fields)
{
RegisterAdditionalDependencies(context, field);

if (field.Type != null)
if (field.Type is not null)
{
context.RegisterDependency(field.Type,
TypeDependencyKind.Default);
Expand All @@ -173,10 +173,10 @@ internal static class TypeDependencyHelper
context.RegisterDependencyRange(
field.Directives.Select(t => t.TypeReference),
TypeDependencyKind.Completed);

RegisterFieldDependencies(context,
fields.SelectMany(t => t.Arguments).ToList());
}

RegisterFieldDependencies(context,
fields.SelectMany(t => t.Arguments));
}

private static void RegisterFieldDependencies(
Expand All @@ -187,7 +187,7 @@ internal static class TypeDependencyHelper
{
RegisterAdditionalDependencies(context, field);

if (field.Type != null)
if (field.Type is not null)
{
context.RegisterDependency(field.Type,
TypeDependencyKind.Completed);
Expand Down
17 changes: 2 additions & 15 deletions src/HotChocolate/Core/src/Types/Types/ObjectType.cs
Expand Up @@ -6,6 +6,7 @@
using HotChocolate.Resolvers;
using HotChocolate.Types.Descriptors;
using HotChocolate.Types.Descriptors.Definitions;
using static HotChocolate.Utilities.ErrorHelper;

#nullable enable

Expand Down Expand Up @@ -133,21 +134,7 @@ private void CompleteIsOfType(ITypeCompletionContext context)

foreach (ObjectFieldDefinition field in invalidFields)
{
// TODO : ErrorHelper
context.ReportError(SchemaErrorBuilder.New()
.SetMessage(
"Unable to infer or resolve the type of " +
"field {0}.{1}. Try to explicitly provide the " +
"type like the following: " +
"`descriptor.Field(\"field\")" +
".Type<List<StringType>>()`.",
Name,
field.Name)
.SetCode(ErrorCodes.Schema.NoFieldType)
.SetTypeSystemObject(this)
.SetPath(Path.New(Name).Append(field.Name))
.SetExtension(TypeErrorFields.Definition, field)
.Build());
context.ReportError(ObjectType_UnableToInferOrResolveType(Name, this, field));
}

return invalidFields.Length == 0;
Expand Down
30 changes: 15 additions & 15 deletions src/HotChocolate/Core/src/Types/Types/Scalars/IntType.cs
Expand Up @@ -17,28 +17,28 @@ public sealed class IntType
: IntegerTypeBase<int>
{
public IntType()
: this(int.MinValue, int.MaxValue)
: this(ScalarNames.Int,
TypeResources.IntType_Description,
bind: BindingBehavior.Implicit)
{
}

public IntType(int min, int max)
: this(ScalarNames.Int, min, max)
: this(ScalarNames.Int,
TypeResources.IntType_Description,
min,
max,
BindingBehavior.Implicit)
{
Description = TypeResources.IntType_Description;
}

public IntType(NameString name)
: this(name, int.MinValue, int.MaxValue)
{
}

public IntType(NameString name, int min, int max)
: base(name, min, max, BindingBehavior.Implicit)
{
}

public IntType(NameString name, string description, int min, int max)
: base(name, min, max, BindingBehavior.Implicit)
public IntType(
NameString name,
string? description = null,
int min = int.MinValue,
int max = int.MaxValue,
BindingBehavior bind = BindingBehavior.Explicit)
: base(name, min, max, bind)
{
Description = description;
}
Expand Down
24 changes: 21 additions & 3 deletions src/HotChocolate/Core/src/Types/Utilities/ErrorHelper.cs
@@ -1,6 +1,7 @@
using HotChocolate.Language;
using HotChocolate.Properties;
using HotChocolate.Types;
using HotChocolate.Types.Descriptors.Definitions;

#nullable enable

Expand All @@ -10,7 +11,7 @@ internal static class ErrorHelper
{
public static ISchemaError CompleteInterfacesHelper_UnableToResolveInterface(
ITypeSystemObject interfaceOrObject,
ISyntaxNode? node)=>
ISyntaxNode? node)=>
SchemaErrorBuilder.New()
.SetMessage("COULD NOT RESOLVE INTERFACE")
.SetCode(ErrorCodes.Schema.MissingType)
Expand Down Expand Up @@ -38,7 +39,7 @@ internal static class ErrorHelper
Types.DirectiveLocation location,
ITypeSystemObject type,
DirectiveNode? syntaxNode,
object source) =>
object source) =>
SchemaErrorBuilder.New()
.SetMessage(
TypeResources.DirectiveCollection_LocationNotAllowed,
Expand All @@ -55,7 +56,7 @@ internal static class ErrorHelper
ITypeSystemObject type,
DirectiveNode? syntaxNode,
object source,
string argumentName) =>
string argumentName) =>
SchemaErrorBuilder.New()
.SetMessage(
"The argument `{0}` value type is wrong.",
Expand Down Expand Up @@ -101,5 +102,22 @@ internal static class ErrorHelper
.AddSyntaxNode(syntaxNode)
.SetExtension("Source", source)
.Build();

public static ISchemaError ObjectType_UnableToInferOrResolveType(
NameString typeName, ObjectType type, ObjectFieldDefinition field) =>
SchemaErrorBuilder.New()
.SetMessage(
"Unable to infer or resolve the type of " +
"field {0}.{1}. Try to explicitly provide the " +
"type like the following: " +
"`descriptor.Field(\"field\")" +
".Type<List<StringType>>()`.",
typeName,
field.Name)
.SetCode(ErrorCodes.Schema.NoFieldType)
.SetTypeSystemObject(type)
.SetPath(Path.New(typeName).Append(field.Name))
.SetExtension(TypeErrorFields.Definition, field)
.Build();
}
}

0 comments on commit b13a2f4

Please sign in to comment.