Skip to content

Commit

Permalink
Returned error for unsupported enumerable value types in paging handl…
Browse files Browse the repository at this point in the history
…ers (#7712)
  • Loading branch information
glen-84 authored Nov 17, 2024
1 parent 8004643 commit 5c4bf9e
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ protected override ValueTask<Connection> SliceAsync(
=> source switch
{
IQueryable<TEntity> q => SliceAsyncInternal(context, Executable.From(q), arguments),
IEnumerable<TEntity> e => SliceAsyncInternal(context, Executable.From(e.AsQueryable()), arguments),
IEnumerable<TEntity> e => e.GetType().IsValueType
? throw new GraphQLException("Cannot handle the specified data source.")
: SliceAsyncInternal(context, Executable.From(e.AsQueryable()), arguments),
IQueryableExecutable<TEntity> ex => SliceAsyncInternal(context, ex, arguments),
_ => throw new GraphQLException("Cannot handle the specified data source."),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ protected override ValueTask<CollectionSegment> SliceAsync(
return source switch
{
IQueryable<TEntity> q => ResolveAsync(context, q, arguments, ct),
IEnumerable<TEntity> e => ResolveAsync(context, e.AsQueryable(), arguments, ct),
IEnumerable<TEntity> e => e.GetType().IsValueType
? throw new GraphQLException("Cannot handle the specified data source.")
: ResolveAsync(context, e.AsQueryable(), arguments, ct),
IExecutable<TEntity> ex => SliceAsync(context, ex.Source, arguments),
_ => throw new GraphQLException("Cannot handle the specified data source."),
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Immutable;
using CookieCrumble;
using Microsoft.Extensions.DependencyInjection;
using HotChocolate.Execution;
Expand Down Expand Up @@ -1007,6 +1008,27 @@ public async Task Invalid_Before_Index_Cursor()
result.MatchSnapshot();
}

[Fact]
public async Task Simple_EnumerableValueType_ReturnsError()
{
// arrange
var executor = await new ServiceCollection()
.AddGraphQL()
.AddQueryType<QueryEnumerableValueType>()
.BuildRequestExecutorAsync();

// act
const string query = "{ test { nodes } }";

var result = await executor.ExecuteAsync(query);
var errors = result.ExpectOperationResult().Errors;

// assert
Assert.NotNull(errors);
var error = Assert.Single(errors);
Assert.Equal("Cannot handle the specified data source.", error.Message);
}

public class QueryType : ObjectType<Query>
{
protected override void Configure(IObjectTypeDescriptor<Query> descriptor)
Expand Down Expand Up @@ -1250,4 +1272,13 @@ public Connection<string> GetFoos(int? first, string? after)
new[] {new Edge<string>("abc", "def"), new Edge<string>("abc", "def"), },
new ConnectionPageInfo(false, false, null, null), 2);
}

public class QueryEnumerableValueType
{
[UsePaging]
public ImmutableArray<int> Test()
{
return ImmutableArray<int>.Empty;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections;
using System.Collections.Immutable;
using System.Runtime.CompilerServices;
using CookieCrumble;
using HotChocolate.Execution;
Expand Down Expand Up @@ -641,6 +642,27 @@ public async Task TotalCountWithCustomCollectionSegment()
result.ToJson().MatchSnapshot();
}

[Fact]
public async Task Simple_EnumerableValueType_ReturnsError()
{
// arrange
var executor = await new ServiceCollection()
.AddGraphQL()
.AddQueryType<QueryEnumerableValueType>()
.BuildRequestExecutorAsync();

// act
const string query = "{ test { items } }";

var result = await executor.ExecuteAsync(query);
var errors = result.ExpectOperationResult().Errors;

// assert
Assert.NotNull(errors);
var error = Assert.Single(errors);
Assert.Equal("Cannot handle the specified data source.", error.Message);
}

public class QueryType : ObjectType<Query>
{
protected override void Configure(IObjectTypeDescriptor<Query> descriptor)
Expand Down Expand Up @@ -767,6 +789,15 @@ public interface ISome2
[UseOffsetPaging(typeof(NonNullType<StringType>))]
public string[] ExplicitType();
}

public class QueryEnumerableValueType
{
[UseOffsetPaging]
public ImmutableArray<int> Test()
{
return ImmutableArray<int>.Empty;
}
}
}

public class MockExecutable<T>(IQueryable<T> source) : IExecutable<T>
Expand Down

0 comments on commit 5c4bf9e

Please sign in to comment.