Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for spatial projections. (#2567)
- Loading branch information
1 parent
7c25702
commit 9905252
Showing
17 changed files
with
508 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
...ial/src/Data/Projections/Extensions/Extensions/QueryableSpatialProjectionScalarHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using HotChocolate.Data.Projections.Expressions.Handlers; | ||
using HotChocolate.Execution.Processing; | ||
using HotChocolate.Utilities; | ||
using NetTopologySuite.Geometries; | ||
|
||
namespace HotChocolate.Data.Projections.Spatial | ||
{ | ||
public class QueryableSpatialProjectionScalarHandler | ||
: QueryableProjectionScalarHandler | ||
{ | ||
public override bool CanHandle(ISelection selection) => | ||
selection.Field.Member is not null && | ||
typeof(Geometry).IsAssignableFrom(selection.Field.Member.GetReturnType()); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...a/Projections/Extensions/Extensions/SpatialFilterProviderDescriptorQueryableExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace HotChocolate.Data.Projections.Spatial | ||
{ | ||
public static class SpatialProjectionProviderDescriptorQueryableExtensions | ||
{ | ||
public static IProjectionProviderDescriptor AddSpatialHandlers( | ||
this IProjectionProviderDescriptor descriptor) => | ||
descriptor.RegisterFieldHandler<QueryableSpatialProjectionScalarHandler>(); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...ial/src/Data/Projections/Extensions/SpatialProjectionsRequestExecutorBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System; | ||
using HotChocolate.Data.Filters; | ||
using HotChocolate.Data.Filters.Spatial; | ||
using HotChocolate.Execution.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace HotChocolate | ||
{ | ||
public static class SpatialProjectionsRequestExecutorBuilderExtensions | ||
{ | ||
public static IRequestExecutorBuilder AddSpatialProjections( | ||
this IRequestExecutorBuilder builder) | ||
{ | ||
if (builder is null) | ||
{ | ||
throw new ArgumentNullException(nameof(builder)); | ||
} | ||
|
||
return builder.ConfigureSchema(x => x.AddSpatialProjections()); | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...late/Spatial/src/Data/Projections/Extensions/SpatialProjectionsSchemaBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System; | ||
using HotChocolate.Data.Projections.Spatial; | ||
using HotChocolate.Data.Projections; | ||
|
||
namespace HotChocolate | ||
{ | ||
public static class SpatialProjectionsSchemaBuilderExtensions | ||
{ | ||
public static ISchemaBuilder AddSpatialProjections(this ISchemaBuilder builder) | ||
{ | ||
if (builder is null) | ||
{ | ||
throw new ArgumentNullException(nameof(builder)); | ||
} | ||
|
||
return builder.AddConvention<IProjectionConvention>( | ||
new ProjectionConventionExtension( | ||
x => x.AddProviderExtension( | ||
new ProjectionProviderExtension(y => y.AddSpatialHandlers())))); | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/HotChocolate/Spatial/test/Data.Projections.SqlServer.Tests/DatabaseContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System.Threading.Tasks; | ||
using Microsoft.EntityFrameworkCore; | ||
using Squadron; | ||
|
||
namespace HotChocolate.Data.Projections.Spatial | ||
{ | ||
public class DatabaseContext<T> : DbContext where T : class | ||
{ | ||
private readonly PostgreSqlResource<PostgisConfig> _resource; | ||
private readonly string _databaseName; | ||
private bool _disposed; | ||
|
||
public DatabaseContext(PostgreSqlResource<PostgisConfig> resource, string databaseName) | ||
{ | ||
_resource = resource; | ||
_databaseName = databaseName; | ||
} | ||
|
||
public DbSet<T> Data { get; set; } = default!; | ||
|
||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | ||
{ | ||
optionsBuilder.UseNpgsql( | ||
_resource.GetConnectionString(_databaseName), | ||
o => o.UseNetTopologySuite()); | ||
} | ||
|
||
public override async ValueTask DisposeAsync() | ||
{ | ||
await base.DisposeAsync(); | ||
|
||
if (!_disposed) | ||
{ | ||
_disposed = true; | ||
} | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/HotChocolate/Spatial/test/Data.Projections.SqlServer.Tests/Extensions/TestExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using HotChocolate.Execution; | ||
using HotChocolate.Tests; | ||
using Snapshooter; | ||
using Snapshooter.Xunit; | ||
|
||
namespace HotChocolate.Data.Projections.Spatial | ||
{ | ||
public static class TestExtensions | ||
{ | ||
public static void MatchSqlSnapshot( | ||
this IExecutionResult? result, | ||
string snapshotName) | ||
{ | ||
if (result is { }) | ||
{ | ||
result.MatchSnapshot(snapshotName); | ||
if (result.ContextData is { } && | ||
result.ContextData.TryGetValue("sql", out object? queryResult)) | ||
{ | ||
queryResult.MatchSnapshot(new SnapshotNameExtension(snapshotName + "_sql")); | ||
} | ||
} | ||
} | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
...olate/Spatial/test/Data.Projections.SqlServer.Tests/Extensions/ToQueryStringExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Collections.Generic; | ||
using Microsoft.EntityFrameworkCore.Query.SqlExpressions; | ||
using Microsoft.EntityFrameworkCore.Query; | ||
using System; | ||
using Microsoft.EntityFrameworkCore.Storage; | ||
|
||
namespace HotChocolate.Data.Projections.Spatial | ||
{ | ||
public static class ToQueryStringExtensions | ||
{ | ||
public static string ToQueryString<TEntity>( | ||
this IQueryable<TEntity> query) | ||
where TEntity : class | ||
{ | ||
IEnumerator<TEntity>? enumerator = query | ||
.Provider | ||
.Execute<IEnumerable<TEntity>>(query.Expression) | ||
.GetEnumerator(); | ||
|
||
var relationalCommandCache = enumerator.Private("_relationalCommandCache"); | ||
|
||
SelectExpression? selectExpression = relationalCommandCache | ||
.Private<SelectExpression>("_selectExpression"); | ||
|
||
IQuerySqlGeneratorFactory? factory = relationalCommandCache | ||
.Private<IQuerySqlGeneratorFactory>("_querySqlGeneratorFactory"); | ||
|
||
QuerySqlGenerator? sqlGenerator = factory.Create(); | ||
IRelationalCommand? command = sqlGenerator.GetCommand(selectExpression); | ||
|
||
return command.CommandText; | ||
} | ||
|
||
private static object Private( | ||
this object? obj, | ||
string privateField) => | ||
obj?.GetType()? | ||
.GetField(privateField, BindingFlags.Instance | BindingFlags.NonPublic)? | ||
.GetValue(obj) ?? throw new InvalidOperationException(); | ||
|
||
private static T Private<T>( | ||
this object obj, | ||
string privateField) | ||
where T : class => | ||
(T?)obj?.GetType()? | ||
.GetField(privateField, BindingFlags.Instance | BindingFlags.NonPublic)? | ||
.GetValue(obj) ?? throw new InvalidOperationException(); | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
src/HotChocolate/Spatial/test/Data.Projections.SqlServer.Tests/FilterVisitorTestBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using HotChocolate.Execution; | ||
using HotChocolate.Resolvers; | ||
using HotChocolate.Types; | ||
using Squadron; | ||
|
||
namespace HotChocolate.Data.Projections.Spatial | ||
{ | ||
public class ProjectionVisitorTestBase | ||
{ | ||
private readonly PostgreSqlResource<PostgisConfig> _resource; | ||
|
||
public ProjectionVisitorTestBase(PostgreSqlResource<PostgisConfig> resource) | ||
{ | ||
_resource = resource; | ||
} | ||
|
||
private async Task<Func<IResolverContext, IEnumerable<T>>> BuildResolverAsync<T>( | ||
params T[] results) | ||
where T : class | ||
{ | ||
var databaseName = Guid.NewGuid().ToString("N"); | ||
var dbContext = new DatabaseContext<T>(_resource, databaseName); | ||
|
||
var sql = dbContext.Database.GenerateCreateScript(); | ||
await _resource.CreateDatabaseAsync(databaseName); | ||
await _resource.RunSqlScriptAsync( | ||
"CREATE EXTENSION postgis;\n" + sql, | ||
databaseName); | ||
dbContext.AddRange(results); | ||
dbContext.SaveChanges(); | ||
|
||
return ctx => dbContext.Data.AsQueryable(); | ||
} | ||
|
||
protected async Task<IRequestExecutor> CreateSchemaAsync<TEntity>( | ||
TEntity[] entities, | ||
ProjectionConvention? convention = null) | ||
where TEntity : class | ||
{ | ||
Func<IResolverContext, IEnumerable<TEntity>> resolver = | ||
await BuildResolverAsync(entities); | ||
|
||
return await new ServiceCollection() | ||
.AddGraphQL() | ||
.AddProjections() | ||
.AddSpatialTypes() | ||
.AddSpatialProjections() | ||
.AddQueryType( | ||
c => c | ||
.Name("Query") | ||
.Field("root") | ||
.Resolver(resolver) | ||
.Use(next => async context => | ||
{ | ||
await next(context); | ||
if (context.Result is IQueryable<TEntity> queryable) | ||
{ | ||
try | ||
{ | ||
context.ContextData["sql"] = queryable.ToQueryString(); | ||
} | ||
catch (Exception) | ||
{ | ||
context.ContextData["sql"] = | ||
"EF Core 3.1 does not support ToQueryString officially"; | ||
} | ||
} | ||
})) | ||
.UseRequest(next => async context => | ||
{ | ||
await next(context); | ||
if (context.Result is IReadOnlyQueryResult result && | ||
context.ContextData.TryGetValue("sql", out var queryString)) | ||
{ | ||
context.Result = | ||
QueryResultBuilder | ||
.FromResult(result) | ||
.SetContextData("sql", queryString) | ||
.Create(); | ||
} | ||
}) | ||
.UseDefaultPipeline() | ||
.BuildRequestExecutorAsync(); | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
....Projections.SqlServer.Tests/HotChocolate.Data.Projections.Spatial.SqlServer.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<AssemblyName>HotChocolate.Data.Projections.Spatial.SqlServer</AssemblyName> | ||
<RootNamespace>HotChocolate.Data.Projections</RootNamespace> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\Data\src\Data\HotChocolate.Data.csproj"/> | ||
<ProjectReference Include="..\..\..\Spatial\src\Types\HotChocolate.Types.Spatial.csproj"/> | ||
<ProjectReference Include="..\..\..\Spatial\src\Data\HotChocolate.Data.Spatial.csproj"/> | ||
<ProjectReference Include="..\..\..\Core\test\Types.Tests\HotChocolate.Types.Tests.csproj"/> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.1.0"/> | ||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.NetTopologySuite" Version="3.1.0"/> | ||
<PackageReference Include="Squadron.PostgreSql" Version="0.8.1"/> | ||
</ItemGroup> | ||
|
||
<!--For Visual Studio for Mac Test Explorer we need this reference here--> | ||
<ItemGroup> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" /> | ||
</ItemGroup> | ||
|
||
</Project> |
17 changes: 17 additions & 0 deletions
17
src/HotChocolate/Spatial/test/Data.Projections.SqlServer.Tests/PostgisConfig.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System; | ||
using Squadron; | ||
|
||
namespace HotChocolate.Data.Projections.Spatial | ||
{ | ||
public class PostgisConfig : PostgreSqlDefaultOptions | ||
{ | ||
public override void Configure(ContainerResourceBuilder builder) => | ||
builder | ||
.WaitTimeout(120) | ||
.Name("postgis") | ||
.Image("postgis/postgis:latest") | ||
.Username("postgis") | ||
.Password(Guid.NewGuid().ToString("N").Substring(12)) | ||
.InternalPort(5432); | ||
} | ||
} |
Oops, something went wrong.