Skip to content

Commit

Permalink
Changes order argument from order:[FooSortInput] to order:[FooSortInp…
Browse files Browse the repository at this point in the history
…ut!] (#2625)
  • Loading branch information
PascalSenn committed Nov 21, 2020
1 parent 9905252 commit 106a37a
Show file tree
Hide file tree
Showing 40 changed files with 135 additions and 159 deletions.
6 changes: 6 additions & 0 deletions src/HotChocolate/Data/src/Data/DataResources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/HotChocolate/Data/src/Data/DataResources.resx
Expand Up @@ -53,6 +53,9 @@
<data name="SortField_SortField_TypeUnknown" xml:space="preserve">
<value>Unable to resolve the field runtime type for `{0}.{1}`.</value>
</data>
<data name="Sorting_TypeOfInvalidFormat" xml:space="preserve">
<value>Unable to resolve sorting type. {0} is of invalid format.</value>
</data>
<data name="SortConventionDescriptor_MustInheritFromSortInputOrEnumType" xml:space="preserve">
<value>The sorting type must inherit from `SortInputType` or `SortEnumType`.</value>
</data>
Expand Down
1 change: 1 addition & 0 deletions src/HotChocolate/Data/src/Data/HotChocolate.Data.csproj
Expand Up @@ -4,6 +4,7 @@
<PackageId>HotChocolate.Data</PackageId>
<AssemblyName>HotChocolate.Data</AssemblyName>
<RootNamespace>HotChocolate.Data</RootNamespace>
<Nullable>enable</Nullable>
<NoWarn>$(NoWarn);CA1062</NoWarn>
</PropertyGroup>

Expand Down
Expand Up @@ -39,7 +39,8 @@ public class QueryableSortInterceptor
out IReadOnlyDictionary<NameString, ArgumentValue>? coercedArgs) &&
coercedArgs.TryGetValue(argumentName, out var argumentValue) &&
argumentValue.Argument.Type is ListType lt &&
lt.ElementType is ISortInputType sortInputType &&
lt.ElementType is NonNullType nn &&
nn.NamedType() is ISortInputType sortInputType &&
argumentValue.ValueLiteral is {} valueNode)
{
QueryableSortContext sortContext =
Expand Down
Expand Up @@ -81,7 +81,7 @@ public ISortConventionDescriptor BindRuntimeType(Type runtimeType, Type sortType
nameof(sortType));
}

Definition.Bindings.Add(runtimeType, sortType);
Definition.Bindings[runtimeType] = sortType;
return this;
}

Expand Down
Expand Up @@ -55,7 +55,8 @@ public override FieldMiddleware CreateExecutor<TEntityType>(NameString argumentN
}

if (argument.Type is ListType lt &&
lt.ElementType is ISortInputType sortInput &&
lt.ElementType is NonNullType nn &&
nn.NamedType() is ISortInputType sortInput &&
context.Field.ContextData.TryGetValue(
ContextVisitSortArgumentKey,
out object? executorObj) &&
Expand Down
Expand Up @@ -2,6 +2,7 @@
using System.Globalization;
using System.Reflection;
using HotChocolate.Configuration;
using HotChocolate.Data;
using HotChocolate.Data.Sorting;
using HotChocolate.Internal;
using HotChocolate.Resolvers;
Expand Down Expand Up @@ -34,7 +35,7 @@ public static class SortObjectFieldDescriptorExtensions
throw new ArgumentNullException(nameof(descriptor));
}

return UseSorting(descriptor, null, null, scope);
return UseSortingInternal(descriptor, null, scope);
}

/// <summary>
Expand All @@ -59,35 +60,7 @@ public static class SortObjectFieldDescriptorExtensions
? typeof(T)
: typeof(SortInputType<>).MakeGenericType(typeof(T));

return UseSorting(descriptor, sortType, null, scope);
}

/// <summary>
/// Registers the middleware and adds the arguments for sorting
/// </summary>
/// <param name="descriptor">The field descriptor where the arguments and middleware are
/// applied to</param>
/// <param name="configure">Configures the filter input types that is used by the field
/// </param>
/// <param name="scope">Specifies what scope should be used for the
/// <see cref="SortConvention" /></param>
public static IObjectFieldDescriptor UseSorting<T>(
this IObjectFieldDescriptor descriptor,
Action<ISortInputTypeDescriptor<T>> configure,
string? scope = null)
{
if (descriptor is null)
{
throw new ArgumentNullException(nameof(descriptor));
}

if (configure is null)
{
throw new ArgumentNullException(nameof(configure));
}

var sortType = new SortInputType<T>(configure);
return UseSorting(descriptor, sortType.GetType(), sortType, scope);
return UseSorting(descriptor, sortType, scope);
}

/// <summary>
Expand Down Expand Up @@ -118,13 +91,12 @@ public static class SortObjectFieldDescriptorExtensions
? type
: typeof(SortInputType<>).MakeGenericType(type);

return UseSorting(descriptor, sortType, null, scope);
return UseSortingInternal(descriptor, sortType, scope);
}

private static IObjectFieldDescriptor UseSorting(
private static IObjectFieldDescriptor UseSortingInternal(
IObjectFieldDescriptor descriptor,
Type? sortType,
ITypeSystemMember? sortTypeInstance,
string? scope)
{
FieldMiddleware placeholder = next => context => default;
Expand All @@ -137,9 +109,9 @@ public static class SortObjectFieldDescriptorExtensions
.OnBeforeCreate(
(c, definition) =>
{
Type? argumentType = sortType;
if (argumentType is null)
ISortConvention convention = c.GetSortConvention(scope);
Type argumentType;
if (sortType is null)
{
if (definition.ResultType is null ||
definition.ResultType == typeof(object) ||
Expand All @@ -152,32 +124,28 @@ public static class SortObjectFieldDescriptorExtensions
nameof(descriptor));
}
argumentType = typeof(SortInputType<>)
.MakeGenericType(typeInfo.NamedType);
}
ITypeReference argumentTypeReference = sortTypeInstance is null
? (ITypeReference)c.TypeInspector.GetTypeRef(
argumentType,
TypeContext.Input,
scope)
: TypeReference.Create(sortTypeInstance, scope);
if (argumentType == typeof(object))
ExtendedTypeReference fieldType = convention
.GetFieldType(typeInfo.NamedType);
argumentType = fieldType.Type.Type;
}
else
{
throw SortObjectFieldDescriptorExtensions_CannotInfer();
argumentType = sortType;
}
argumentType = typeof(ListType<>).MakeGenericType(argumentType);
ExtendedTypeReference argumentTypeReference = c.TypeInspector.GetTypeRef(
typeof(ListType<>).MakeGenericType(
typeof(NonNullType<>).MakeGenericType(argumentType)),
TypeContext.Input,
scope);
var argumentDefinition = new ArgumentDefinition
{
Name = argumentPlaceholder,
Type = c.TypeInspector.GetTypeRef(
argumentType,
TypeContext.Input,
scope)
Name = argumentPlaceholder, Type = argumentTypeReference
};
definition.Arguments.Add(argumentDefinition);
definition.Configurations.Add(
Expand Down Expand Up @@ -218,7 +186,12 @@ public static class SortObjectFieldDescriptorExtensions
FieldMiddleware placeholder,
string? scope)
{
ISortInputType type = context.GetType<ISortInputType>(argumentTypeReference);
IType resolvedType = context.GetType<IType>(argumentTypeReference);
if (!(resolvedType.ElementType().NamedType() is ISortInputType type))
{
throw Sorting_TypeOfInvalidFormat(resolvedType);
}

ISortConvention convention = context.DescriptorContext.GetSortConvention(scope);

var fieldDescriptor = ObjectFieldDescriptor.From(context.DescriptorContext, definition);
Expand Down
Expand Up @@ -4,6 +4,7 @@ public class DefaultSortEnumType : SortEnumType
{
protected override void Configure(ISortEnumTypeDescriptor descriptor)
{
descriptor.Name("SortEnumType");
descriptor.Operation(DefaultSortOperations.Ascending);
descriptor.Operation(DefaultSortOperations.Descending);
}
Expand Down
10 changes: 10 additions & 0 deletions src/HotChocolate/Data/src/Data/ThrowHelper.cs
Expand Up @@ -4,6 +4,7 @@
using HotChocolate.Data.Projections;
using HotChocolate.Data.Sorting;
using HotChocolate.Language;
using HotChocolate.Types;
using HotChocolate.Types.Descriptors.Definitions;

namespace HotChocolate.Data
Expand Down Expand Up @@ -257,6 +258,15 @@ internal static class ThrowHelper
.SetExtension(nameof(sortOperation), sortOperation)
.Build());

public static SchemaException Sorting_TypeOfInvalidFormat(
IType type) =>
new SchemaException(
SchemaErrorBuilder.New()
.SetMessage(
DataResources.Sorting_TypeOfInvalidFormat,
type.Print())
.Build());

public static SchemaException ProjectionProvider_NoHandlersConfigured(
IProjectionProvider projectionConvention) =>
new SchemaException(
Expand Down
Expand Up @@ -449,7 +449,7 @@ public async Task Create_ObjectString_OrderBy_TwoProperties_Variables()
QueryRequestBuilder.New()
.SetQuery(
@"
query testSort($order: [BarSortInput]) {
query testSort($order: [BarSortInput!]) {
root(order: $order) {
foo {
barBool
Expand Down Expand Up @@ -480,7 +480,7 @@ public async Task Create_ObjectString_OrderBy_TwoProperties_Variables()
QueryRequestBuilder.New()
.SetQuery(
@"
query testSort($order: [BarSortInput]) {
query testSort($order: [BarSortInput!]) {
root(order: $order) {
foo {
barBool
Expand Down Expand Up @@ -514,7 +514,7 @@ public async Task Create_ObjectString_OrderBy_TwoProperties_Variables()
QueryRequestBuilder.New()
.SetQuery(
@"
query testSort($order: [BarSortInput]) {
query testSort($order: [BarSortInput!]) {
root(order: $order) {
foo {
barBool
Expand Down Expand Up @@ -545,7 +545,7 @@ public async Task Create_ObjectString_OrderBy_TwoProperties_Variables()
QueryRequestBuilder.New()
.SetQuery(
@"
query testSort($order: [BarSortInput]) {
query testSort($order: [BarSortInput!]) {
root(order: $order) {
foo {
barBool
Expand Down
Expand Up @@ -7,24 +7,24 @@ type Foo {
}

type Query1 {
foos(order: [FooSortInput]): [Foo!]!
foosBar(order: [Bar_FooSortInput]): [Foo!]!
foos(order: [FooSortInput!]): [Foo!]!
foosBar(order: [Bar_FooSortInput!]): [Foo!]!
}

input Bar_FooSortInput {
bar: Bar_DefaultSortEnumType
bar: Bar_SortEnumType
}

input FooSortInput {
bar: DefaultSortEnumType
bar: SortEnumType
}

enum Bar_DefaultSortEnumType {
enum Bar_SortEnumType {
Different
DESC
}

enum DefaultSortEnumType {
enum SortEnumType {
ASC
DESC
}
Expand Down
Expand Up @@ -7,24 +7,24 @@ type Foo {
}

type QueryType {
foos(order: [FooSortInput]): [Foo]
foosBar(order: [Bar_FooSortInput]): [Foo]
foos(order: [FooSortInput!]): [Foo]
foosBar(order: [Bar_FooSortInput!]): [Foo]
}

input Bar_FooSortInput {
bar: Bar_DefaultSortEnumType
bar: Bar_SortEnumType
}

input FooSortInput {
bar: DefaultSortEnumType
bar: SortEnumType
}

enum Bar_DefaultSortEnumType {
enum Bar_SortEnumType {
Different
DESC
}

enum DefaultSortEnumType {
enum SortEnumType {
ASC
DESC
}
Expand Down
20 changes: 0 additions & 20 deletions src/HotChocolate/Data/test/Data.Sorting.Tests/ExtensionsTest.cs
Expand Up @@ -200,26 +200,6 @@ public void ObjectField_UseSorting_Type_SchemaType()
schema.ToString().MatchSnapshot();
}

[Fact]
public void ObjectField_UseSorting_Descriptor()
{
// arrange
// act
ISchemaBuilder builder = SchemaBuilder.New()
.AddSorting()
.AddQueryType<Query>(
c =>
c.Field(x => x.GetFoos())
.UseSorting<Bar>(
x => x.Name("foo").Field(x => x.Foo)));

ISchema schema = builder.Create();

// assert
schema.ToString().MatchSnapshot();
IObjectFieldDescriptor f = default;
}

private class TestSort : SortInputType
{
protected override void Configure(ISortInputTypeDescriptor descriptor)
Expand Down
Expand Up @@ -16,10 +16,10 @@ input ExplicitSortTypeSortInput {
}

input FooSortInput {
barShort: DefaultSortEnumType
barShort: SortEnumType
}

enum DefaultSortEnumType {
enum SortEnumType {
ASC
DESC
}
Expand Down
Expand Up @@ -12,10 +12,10 @@ input BarSortInput {
}

input FooSortInput {
barShort: DefaultSortEnumType
barShort: SortEnumType
}

enum DefaultSortEnumType {
enum SortEnumType {
ASC
DESC
}
Expand Down

0 comments on commit 106a37a

Please sign in to comment.