Skip to content

Commit

Permalink
add query abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
PascalSenn committed Oct 16, 2020
1 parent 66ced99 commit 0bd2c53
Show file tree
Hide file tree
Showing 27 changed files with 199 additions and 231 deletions.
12 changes: 12 additions & 0 deletions src/HotChocolate/Core/src/Abstractions/IQuery.cs
@@ -0,0 +1,12 @@
using System.Threading;
using System.Threading.Tasks;

namespace HotChocolate
{
public interface IQuery
{
ValueTask<object> ExecuteAsync(CancellationToken cancellationToken);

string Print();
}
}
11 changes: 11 additions & 0 deletions src/HotChocolate/Core/src/Abstractions/IQuery~1.cs
@@ -0,0 +1,11 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace HotChocolate
{
public interface IQuery<T> : IQuery
{
new ValueTask<IReadOnlyList<T>> ExecuteAsync(CancellationToken cancellationToken);
}
}
Expand Up @@ -66,6 +66,10 @@ private async ValueTask ExecuteResolverPipelineAsync()

switch (_context.Result)
{
case HotChocolate.IQuery query:
_context.Result = await query.ExecuteAsync(_context);
break;

case IQueryable queryable:
_context.Result = await Task.Run(() =>
{
Expand Down
Expand Up @@ -166,7 +166,8 @@ public static bool IsSupportedCollectionInterface(Type type)
|| typeDefinition == typeof(ImmutableList<>)
|| typeDefinition == typeof(ImmutableQueue<>)
|| typeDefinition == typeof(ImmutableStack<>)
|| typeDefinition == typeof(ImmutableHashSet<>))
|| typeDefinition == typeof(ImmutableHashSet<>)
|| typeDefinition == typeof(IQuery<>))
{
return true;
}
Expand Down
Expand Up @@ -40,7 +40,8 @@ public static ExtendedMethodInfo FromMethod(MethodInfo method, TypeCache cache)
method,
() => Rewrite(
CreateExtendedType(context, helper.GetFlags(method), method.ReturnType),
method, cache));
method,
cache));

ParameterInfo[] parameters = method.GetParameters();
var parameterTypes = new Dictionary<ParameterInfo, IExtendedType>();
Expand Down Expand Up @@ -203,6 +204,7 @@ public static ExtendedMethodInfo FromMethod(MethodInfo method, TypeCache cache)
state = flags[0];
}
}

return state;
}
}
Expand Down
Expand Up @@ -20,7 +20,7 @@ private static ExtendedType FromTypeInternal(Type type, TypeCache cache)
&& type.GetGenericTypeDefinition() == typeof(Nullable<>))
{
Type inner = type.GetGenericArguments()[0];

return new ExtendedType(
inner,
ExtendedTypeKind.Runtime,
Expand Down Expand Up @@ -50,7 +50,7 @@ private static ExtendedType FromTypeInternal(Type type, TypeCache cache)
}

public static IReadOnlyList<ExtendedType> GetGenericArguments(
Type type,
Type type,
TypeCache cache)
{
if (type.IsGenericType)
Expand Down
66 changes: 60 additions & 6 deletions src/HotChocolate/Core/test/Execution.Tests/CodeFirstTests.cs
@@ -1,4 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using HotChocolate.Execution;
using HotChocolate.Resolvers;
Expand Down Expand Up @@ -43,6 +45,22 @@ public async Task ExecuteOneFieldQueryWithMethod()
result.MatchSnapshot();
}

[Fact]
public async Task ExecuteOneFieldQueryWithQuery()
{
// arrange
var schema = Schema.Create(
c => c.RegisterType<QueryTypeWithMethod>());

// act
IExecutionResult result =
await schema.MakeExecutable().ExecuteAsync("{ query }");

// assert
Assert.Null(result.Errors);
result.MatchSnapshot();
}

[Fact]
public async Task ExecuteWithUnionType()
{
Expand All @@ -51,13 +69,16 @@ public async Task ExecuteWithUnionType()

// act
IExecutionResult result =
await schema.MakeExecutable().ExecuteAsync(
@"{
fooOrBar {
... on Bar { nameBar }
... on Foo { nameFoo }
await schema.MakeExecutable()
.ExecuteAsync(
@"
{
fooOrBar {
... on Bar { nameBar }
... on Foo { nameFoo }
}
}
}");
");

// assert
Assert.Null(result.Errors);
Expand Down Expand Up @@ -263,6 +284,11 @@ public string GetTest()
return "Hello World!";
}

public IQuery<string> GetQuery()
{
return MockQuery<string>.From("foo", "bar");
}

public string TestProp => "Hello World!";
}

Expand All @@ -285,6 +311,7 @@ public class QueryTypeWithMethod
{
descriptor.Name("Query");
descriptor.Field(t => t.GetTest()).Name("test");
descriptor.Field(t => t.GetQuery()).Name("query");
}
}

Expand Down Expand Up @@ -434,5 +461,32 @@ public class DogType
.Type<ListType<StringType>>();
}
}

public class MockQuery<T> : IQuery<T>
{
private readonly IReadOnlyList<T> _list;

private MockQuery(IEnumerable<T> list)
{
_list = list.ToArray();
}

async ValueTask<object> IQuery.ExecuteAsync(CancellationToken cancellationToken)
{
return await ExecuteAsync(cancellationToken);
}

public ValueTask<IReadOnlyList<T>> ExecuteAsync(CancellationToken cancellationToken)
{
return new ValueTask<IReadOnlyList<T>>(_list);
}

public string Print()
{
return _list.ToString();
}

public static MockQuery<T> From(params T[] items) => new MockQuery<T>(items);
}
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit 0bd2c53

Please sign in to comment.