-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
68 lines (52 loc) · 2.02 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
using Bogus;
using GraphQL;
using GraphQL.AspNetCore3;
using GraphQL.MicrosoftDI;
using GraphQL.Server.Ui.Playground;
using GraphQL.SystemTextJson;
using GraphQL.Types;
using GraphQLAPI;
using GraphQLAPI.Entities;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseInMemoryDatabase("Helloworld")
);
builder.Services.AddTransient<IMyAwesomeService, MyAwesomeService>();
builder.Services.AddSingleton<ISchema, MyApplicationSchema>(services => new MyApplicationSchema(new SelfActivatingServiceProvider(services)));
builder.Services.AddGraphQL(b => b
.AddGraphTypes(typeof(Program).Assembly)
.AddSchema<MyApplicationSchema>()
.AddSystemTextJson() // serializer
);
var app = builder.Build();
app.UseDeveloperExceptionPage();
app.UseWebSockets();
app.UseGraphQL("/graphql"); // url to host GraphQL endpoint
//app.UseGraphQLGraphiQL();
app.UseGraphQLPlayground(
new PlaygroundOptions {
GraphQLEndPoint = new PathString("/graphql"),
SubscriptionsEndPoint = new PathString("/graphql"),
},
"/"); // url to host Playground at
using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;
var context = services.GetRequiredService<AppDbContext>();
context.Database.EnsureCreated();
var faker = new Faker<Customer>()
.RuleFor(r => r.FirstName, f => f.Name.FirstName())
.RuleFor(r => r.LastName, f => f.Name.LastName())
.RuleFor(r => r.Id, f => Guid.NewGuid())
.RuleFor(r => r.Location, f => f.Address.Country());
var bookFaker = new Faker<VirtualBook>()
.RuleFor(r => r.Id, f => Guid.NewGuid())
.RuleFor(x => x.Title, f => f.Lorem.Sentence())
.RuleFor(x => x.Description, f => f.Lorem.Text())
.RuleFor(x => x.PurchasedBy, f => faker.Generate(new Random().Next(0, 10)));
var fakeData = bookFaker.Generate(25);
context.VirtualBooks.AddRange(fakeData);
context.SaveChanges();
}
await app.RunAsync();