Skip to content

Commit

Permalink
Added support for IQueryable Projections (#1446)
Browse files Browse the repository at this point in the history
  • Loading branch information
PascalSenn authored Mar 7, 2020
1 parent 6dbe61e commit 8302ae7
Show file tree
Hide file tree
Showing 57 changed files with 3,512 additions and 885 deletions.
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"sdk": {
"version": "3.0.100"
"version": "3.1.101"
}
}
2 changes: 1 addition & 1 deletion src/Core/Abstractions/ParentAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;

namespace HotChocolate
{
Expand Down
70 changes: 70 additions & 0 deletions src/Core/Core.sln
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Types.Sorting.Tests", "Type
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Types.Sorting.Mongo.Tests", "Types.Sorting.Mongo.Tests\Types.Sorting.Mongo.Tests.csproj", "{E8843255-77EB-471F-B682-528BB4B5D1D5}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Types.Selection", "Types.Selection\Types.Selection.csproj", "{0FD6B6D1-00AB-42CF-800F-4039A976A75A}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Types.Selection.Abstractions.Tests", "Types.Selection.Abstractions.Tests\Types.Selection.Abstractions.Tests.csproj", "{726D5295-3F4E-4A1A-8F3B-0EA8EC456B48}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Types.Selection.InMemory.Tests", "Types.Selection.InMemory.Tests\Types.Selection.InMemory.Tests.csproj", "{3DB2E3F5-0DBE-4B91-A335-D82F8794324B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Types.Selection.Mongo.Tests", "Types.Selection.Mongo.Tests\Types.Selection.Mongo.Tests.csproj", "{21AA751A-4C29-414C-B476-EE9204A48DDD}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Types.Selection.SqlServer.Tests", "Types.Selection.SqlServer.Tests\Types.Selection.SqlServer.Tests.csproj", "{07674D84-4A5C-4608-BECE-03630D38275F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -449,6 +459,66 @@ Global
{E8843255-77EB-471F-B682-528BB4B5D1D5}.Release|x64.Build.0 = Release|Any CPU
{E8843255-77EB-471F-B682-528BB4B5D1D5}.Release|x86.ActiveCfg = Release|Any CPU
{E8843255-77EB-471F-B682-528BB4B5D1D5}.Release|x86.Build.0 = Release|Any CPU
{0FD6B6D1-00AB-42CF-800F-4039A976A75A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0FD6B6D1-00AB-42CF-800F-4039A976A75A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0FD6B6D1-00AB-42CF-800F-4039A976A75A}.Debug|x64.ActiveCfg = Debug|Any CPU
{0FD6B6D1-00AB-42CF-800F-4039A976A75A}.Debug|x64.Build.0 = Debug|Any CPU
{0FD6B6D1-00AB-42CF-800F-4039A976A75A}.Debug|x86.ActiveCfg = Debug|Any CPU
{0FD6B6D1-00AB-42CF-800F-4039A976A75A}.Debug|x86.Build.0 = Debug|Any CPU
{0FD6B6D1-00AB-42CF-800F-4039A976A75A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0FD6B6D1-00AB-42CF-800F-4039A976A75A}.Release|Any CPU.Build.0 = Release|Any CPU
{0FD6B6D1-00AB-42CF-800F-4039A976A75A}.Release|x64.ActiveCfg = Release|Any CPU
{0FD6B6D1-00AB-42CF-800F-4039A976A75A}.Release|x64.Build.0 = Release|Any CPU
{0FD6B6D1-00AB-42CF-800F-4039A976A75A}.Release|x86.ActiveCfg = Release|Any CPU
{0FD6B6D1-00AB-42CF-800F-4039A976A75A}.Release|x86.Build.0 = Release|Any CPU
{726D5295-3F4E-4A1A-8F3B-0EA8EC456B48}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{726D5295-3F4E-4A1A-8F3B-0EA8EC456B48}.Debug|Any CPU.Build.0 = Debug|Any CPU
{726D5295-3F4E-4A1A-8F3B-0EA8EC456B48}.Debug|x64.ActiveCfg = Debug|Any CPU
{726D5295-3F4E-4A1A-8F3B-0EA8EC456B48}.Debug|x64.Build.0 = Debug|Any CPU
{726D5295-3F4E-4A1A-8F3B-0EA8EC456B48}.Debug|x86.ActiveCfg = Debug|Any CPU
{726D5295-3F4E-4A1A-8F3B-0EA8EC456B48}.Debug|x86.Build.0 = Debug|Any CPU
{726D5295-3F4E-4A1A-8F3B-0EA8EC456B48}.Release|Any CPU.ActiveCfg = Release|Any CPU
{726D5295-3F4E-4A1A-8F3B-0EA8EC456B48}.Release|Any CPU.Build.0 = Release|Any CPU
{726D5295-3F4E-4A1A-8F3B-0EA8EC456B48}.Release|x64.ActiveCfg = Release|Any CPU
{726D5295-3F4E-4A1A-8F3B-0EA8EC456B48}.Release|x64.Build.0 = Release|Any CPU
{726D5295-3F4E-4A1A-8F3B-0EA8EC456B48}.Release|x86.ActiveCfg = Release|Any CPU
{726D5295-3F4E-4A1A-8F3B-0EA8EC456B48}.Release|x86.Build.0 = Release|Any CPU
{3DB2E3F5-0DBE-4B91-A335-D82F8794324B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3DB2E3F5-0DBE-4B91-A335-D82F8794324B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3DB2E3F5-0DBE-4B91-A335-D82F8794324B}.Debug|x64.ActiveCfg = Debug|Any CPU
{3DB2E3F5-0DBE-4B91-A335-D82F8794324B}.Debug|x64.Build.0 = Debug|Any CPU
{3DB2E3F5-0DBE-4B91-A335-D82F8794324B}.Debug|x86.ActiveCfg = Debug|Any CPU
{3DB2E3F5-0DBE-4B91-A335-D82F8794324B}.Debug|x86.Build.0 = Debug|Any CPU
{3DB2E3F5-0DBE-4B91-A335-D82F8794324B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3DB2E3F5-0DBE-4B91-A335-D82F8794324B}.Release|Any CPU.Build.0 = Release|Any CPU
{3DB2E3F5-0DBE-4B91-A335-D82F8794324B}.Release|x64.ActiveCfg = Release|Any CPU
{3DB2E3F5-0DBE-4B91-A335-D82F8794324B}.Release|x64.Build.0 = Release|Any CPU
{3DB2E3F5-0DBE-4B91-A335-D82F8794324B}.Release|x86.ActiveCfg = Release|Any CPU
{3DB2E3F5-0DBE-4B91-A335-D82F8794324B}.Release|x86.Build.0 = Release|Any CPU
{21AA751A-4C29-414C-B476-EE9204A48DDD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{21AA751A-4C29-414C-B476-EE9204A48DDD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{21AA751A-4C29-414C-B476-EE9204A48DDD}.Debug|x64.ActiveCfg = Debug|Any CPU
{21AA751A-4C29-414C-B476-EE9204A48DDD}.Debug|x64.Build.0 = Debug|Any CPU
{21AA751A-4C29-414C-B476-EE9204A48DDD}.Debug|x86.ActiveCfg = Debug|Any CPU
{21AA751A-4C29-414C-B476-EE9204A48DDD}.Debug|x86.Build.0 = Debug|Any CPU
{21AA751A-4C29-414C-B476-EE9204A48DDD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{21AA751A-4C29-414C-B476-EE9204A48DDD}.Release|Any CPU.Build.0 = Release|Any CPU
{21AA751A-4C29-414C-B476-EE9204A48DDD}.Release|x64.ActiveCfg = Release|Any CPU
{21AA751A-4C29-414C-B476-EE9204A48DDD}.Release|x64.Build.0 = Release|Any CPU
{21AA751A-4C29-414C-B476-EE9204A48DDD}.Release|x86.ActiveCfg = Release|Any CPU
{21AA751A-4C29-414C-B476-EE9204A48DDD}.Release|x86.Build.0 = Release|Any CPU
{07674D84-4A5C-4608-BECE-03630D38275F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{07674D84-4A5C-4608-BECE-03630D38275F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{07674D84-4A5C-4608-BECE-03630D38275F}.Debug|x64.ActiveCfg = Debug|Any CPU
{07674D84-4A5C-4608-BECE-03630D38275F}.Debug|x64.Build.0 = Debug|Any CPU
{07674D84-4A5C-4608-BECE-03630D38275F}.Debug|x86.ActiveCfg = Debug|Any CPU
{07674D84-4A5C-4608-BECE-03630D38275F}.Debug|x86.Build.0 = Debug|Any CPU
{07674D84-4A5C-4608-BECE-03630D38275F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{07674D84-4A5C-4608-BECE-03630D38275F}.Release|Any CPU.Build.0 = Release|Any CPU
{07674D84-4A5C-4608-BECE-03630D38275F}.Release|x64.ActiveCfg = Release|Any CPU
{07674D84-4A5C-4608-BECE-03630D38275F}.Release|x64.Build.0 = Release|Any CPU
{07674D84-4A5C-4608-BECE-03630D38275F}.Release|x86.ActiveCfg = Release|Any CPU
{07674D84-4A5C-4608-BECE-03630D38275F}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
3 changes: 3 additions & 0 deletions src/Core/Core/InternalsVisibleTo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("HotChocolate.Types.Selections")]
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Squadron.Redis" Version="0.3.0" />
<PackageReference Include="Squadron.Redis" Version="0.5.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Squadron.Redis" Version="0.3.0" />
<PackageReference Include="Squadron.Redis" Version="0.5.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Squadron.Mongo" Version="0.3.0" />
<PackageReference Include="Squadron.Mongo" Version="0.5.0" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions src/Core/Types.Filters/QueryableFilterMiddleware.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Expand Down Expand Up @@ -26,7 +26,7 @@ public async Task InvokeAsync(IMiddlewareContext context)
{
await _next(context).ConfigureAwait(false);

var filter = context.Argument<IValueNode>("where");
IValueNode filter = context.Argument<IValueNode>("where");

if (filter is null || filter is NullValueNode)
{
Expand Down
5 changes: 5 additions & 0 deletions src/Core/Types.Filters/QueryableFilterVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ public Expression<Func<TSource, bool>> CreateFilter<TSource>()
_parameter);
}

public Expression CreateFilter()
{
return Expression.Lambda(Level.Peek().Peek(), _parameter);
}

protected Stack<Queue<Expression>> Level { get; } =
new Stack<Queue<Expression>>();

Expand Down
14 changes: 14 additions & 0 deletions src/Core/Types.Selection.Abstractions.Tests/IResolverProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using HotChocolate.Resolvers;
using Microsoft.Extensions.DependencyInjection;

namespace HotChocolate.Types.Selections
{
public interface IResolverProvider
{
public (IServiceCollection, Func<IResolverContext, IEnumerable<TResult>>)
CreateResolver<TResult>(params TResult[] results)
where TResult : class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using HotChocolate.Execution;
using HotChocolate.Resolvers;
using Microsoft.Extensions.DependencyInjection;
using Xunit;

namespace HotChocolate.Types.Selections
{
public abstract class SelectionAttributeTestsBase
{

private readonly IResolverProvider _provider;
private readonly bool _setId;

protected SelectionAttributeTestsBase(IResolverProvider provider, bool setId = false)
{
_provider = provider;
_setId = setId;
}

[Fact]
public void Execute_Selection_MultipleScalar()
{
// arrange
IServiceCollection services;
Func<IResolverContext, IEnumerable<Foo>> resolver;
(services, resolver) = _provider.CreateResolver(
Foo.Create("aa", 1, _setId),
Foo.Create("bb", 2, _setId));

IQueryable<Foo> resultCtx = null;
ISchema schema = SchemaBuilder.New()
.AddServices(services.BuildServiceProvider())
.AddQueryType<Query>(d =>
d.Field(t => t.Foos)
.Resolver(resolver)
.Use(next => async ctx =>
{
await next(ctx).ConfigureAwait(false);
resultCtx = ctx.Result as IQueryable<Foo>;
}))
.Create();
IQueryExecutor executor = schema.MakeExecutable();

// act
executor.Execute(
"{ foos { bar baz } }");

// assert
Assert.NotNull(resultCtx);
Assert.Collection(resultCtx.ToArray(),
x =>
{
Assert.Equal("aa", x.Bar);
Assert.Equal(1, x.Baz);
Assert.Null(x.Nested);
Assert.Null(x.NestedCollection);
},
x =>
{
Assert.Equal("bb", x.Bar);
Assert.Equal(2, x.Baz);
Assert.Null(x.Nested);
Assert.Null(x.NestedCollection);
});
}

public class Query
{
[UseSelection]
public IQueryable<Foo> Foos { get; }
}

public class Foo
{
private static int idCounter = 1;

[Key]
public int Id { get; set; }

public string Bar { get; set; }

public int Baz { get; set; }

public NestedFoo Nested { get; set; }

public List<NestedFoo> NestedCollection { get; set; }

public static Foo Create(string bar, int baz, bool setId)
{
var value = new Foo
{
Bar = bar,
Baz = baz,
Nested = new NestedFoo()
{
Bar = "nested" + bar,
Baz = baz * 2
},
NestedCollection = new List<NestedFoo>()
{
new NestedFoo()
{
Bar = "nestedcollection" + bar,
Baz = baz * 3
},
}
};
if (setId)
{
value.Id = ++idCounter;
}
return value;
}
}

public class NestedFoo
{
[Key]
public int Id { get; set; }

public string Bar { get; set; }

public int Baz { get; set; }
}
}
}
Loading

0 comments on commit 8302ae7

Please sign in to comment.