-
-
Notifications
You must be signed in to change notification settings - Fork 725
/
FilterVisitorTestBase.cs
94 lines (85 loc) · 3.37 KB
/
FilterVisitorTestBase.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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();
}
}
}