-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
executable file
·138 lines (115 loc) · 3.75 KB
/
Program.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using System;
using System.Linq;
using System.Threading.Tasks;
using GraphQL;
using GraphQL.Http;
using GraphQL.Types;
using ResolveGraphQL.DataModel;
using ResolveGraphQL.Schema;
using Unity;
namespace ResolveDataModel
{
public class Program
{
public static void Main(string[] args)
{
using (var db = new StarWarsContext())
{
// insert some testing data into database
if (db.Humans.Count() == 0)
InsertData(db);
var container = new UnityContainer();
container.RegisterInstance(db);
container.RegisterType<StarWarsQuery>();
container.RegisterType<DroidType>();
container.RegisterType<HumanType>();
var schema = new StarWarsSchema((t) => container.Resolve(t) as GraphType);
var query = @"
query AllHumansQuery {
humans {
name
friends {
id
name
...on Droid {
primaryFunction
}
}
}
}";
Console.WriteLine("Run AllHumansQuery");
var result = Execute(schema, null, query);
Console.WriteLine(result.Result);
Console.WriteLine();
query = @"
query AllCharactersQuery {
characters {
name
friends {
name
}
}
}";
Console.WriteLine("Run AllCharactersQuery");
result = Execute(schema, null, query);
Console.WriteLine(result.Result);
}
}
public static async Task<string> Execute(
Schema schema,
object rootObject,
string query,
string operationName = null,
Inputs inputs = null)
{
var executer = new DocumentExecuter();
var writer = new DocumentWriter();
var result = await executer.ExecuteAsync(schema, rootObject, query, operationName, inputs);
return writer.Write(result);
}
private static void InsertData(StarWarsContext db)
{
db.Humans.Add(new Human
{
HumanId = 1,
Name = "Luke",
HomePlanet = "Tatooine"
});
db.Humans.Add(new Human
{
HumanId = 2,
Name = "Vader",
HomePlanet = "Tatooine"
});
db.Droids.Add(new Droid
{
DroidId = 1,
Name = "R2-D2",
PrimaryFunction = "Astromech"
});
db.Droids.Add(new Droid
{
DroidId = 2,
Name = "C-3PO",
PrimaryFunction = "Protocol"
});
db.HumanFriends.Add(new HumanFreind
{
HumanId = 1,
DroidId = 1
});
db.HumanFriends.Add(new HumanFreind
{
HumanId = 1,
DroidId = 2
});
db.HumanFriends.Add(new HumanFreind
{
HumanId = 2,
DroidId = 1
});
var count = db.SaveChanges();
Console.WriteLine("{0} records saved to database", count);
}
}
}