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

Changes order argument from order:[FooSortInput] to order:[FooSortInput!] #2625

Merged
merged 8 commits into from
Nov 21, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 @@ -240,6 +241,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -147,26 +147,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
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ input ExplicitSortTypeSortInput {
}

input FooSortInput {
barShort: DefaultSortEnumType
barShort: SortEnumType
}

enum DefaultSortEnumType {
enum SortEnumType {
ASC
DESC
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ input BarSortInput {
}

input FooSortInput {
barShort: DefaultSortEnumType
barShort: SortEnumType
}

enum DefaultSortEnumType {
enum SortEnumType {
ASC
DESC
}
Expand Down