forked from SimonCropp/GraphQL.EntityFramework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMultiContextTests.cs
119 lines (99 loc) · 3.16 KB
/
MultiContextTests.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using System.Threading.Tasks;
using EfLocalDb;
using GraphQL;
using GraphQL.EntityFramework;
using GraphQL.Utilities;
using Microsoft.Extensions.DependencyInjection;
using VerifyXunit;
using Xunit;
using Xunit.Abstractions;
public class MultiContextTests :
VerifyBase
{
[Fact]
public async Task Run()
{
GraphTypeTypeRegistry.Register<Entity1, Entity1Graph>();
GraphTypeTypeRegistry.Register<Entity2, Entity2Graph>();
var sqlInstance1 = new SqlInstance<DbContext1>(
constructInstance: builder => new DbContext1(builder.Options));
var sqlInstance2 = new SqlInstance<DbContext2>(
constructInstance: builder => new DbContext2(builder.Options));
var query = @"
{
entity1
{
property
},
entity2
{
property
}
}";
var entity1 = new Entity1
{
Property = "the entity1"
};
var entity2 = new Entity2
{
Property = "the entity2"
};
var sqlDatabase1 = await sqlInstance1.Build();
var sqlDatabase2 = await sqlInstance2.Build();
await using (var dbContext = sqlDatabase1.NewDbContext())
{
dbContext.AddRange(entity1);
await dbContext.SaveChangesAsync();
}
await using (var dbContext = sqlDatabase2.NewDbContext())
{
dbContext.AddRange(entity2);
await dbContext.SaveChangesAsync();
}
await using var dbContext1 = sqlDatabase1.NewDbContext();
await using var dbContext2 = sqlDatabase2.NewDbContext();
var services = new ServiceCollection();
services.AddSingleton<MultiContextQuery>();
services.AddSingleton<Entity1Graph>();
services.AddSingleton<Entity2Graph>();
services.AddSingleton(dbContext1);
services.AddSingleton(dbContext2);
#region RegisterMultipleInContainer
EfGraphQLConventions.RegisterInContainer(
services,
userContext => ((UserContext) userContext).DbContext1);
EfGraphQLConventions.RegisterInContainer(
services,
userContext => ((UserContext) userContext).DbContext2);
#endregion
await using var provider = services.BuildServiceProvider();
using var schema = new MultiContextSchema(new FuncDependencyResolver(provider.GetRequiredService));
var documentExecuter = new EfDocumentExecuter();
#region MultiExecutionOptions
var executionOptions = new ExecutionOptions
{
Schema = schema,
Query = query,
UserContext = new UserContext(dbContext1, dbContext2)
};
#endregion
var executionResult = await documentExecuter.ExecuteWithErrorCheck(executionOptions);
await Verify(executionResult.Data);
}
public MultiContextTests(ITestOutputHelper output) :
base(output)
{
}
}
#region MultiUserContext
public class UserContext
{
public UserContext(DbContext1 context1, DbContext2 context2)
{
DbContext1 = context1;
DbContext2 = context2;
}
public readonly DbContext1 DbContext1;
public readonly DbContext2 DbContext2;
}
#endregion