-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib-graphql.cs
More file actions
executable file
·62 lines (55 loc) · 1.99 KB
/
lib-graphql.cs
File metadata and controls
executable file
·62 lines (55 loc) · 1.99 KB
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
// Projekt.csproj
<PackageReference Include="GraphQL.Server.Transports.AspNetCore" Version="3.4.0" />
<PackageReference Include="GraphQL.Server.Ui.Playground" Version="3.4.0" />
// WebApplicationBuilder (builder.Services)
Services.AddScoped<IDependencyResolver>(x => new FuncDependencyResolver(x.GetRequiredService));
Services.AddScoped<CourseSchema>();
Services.AddGraphQL(x => { x.ExposeExceptions = true; }).AddGraphTypes(ServiceLifetime.Scoped);
Services.Configure<KestrelServerOptions>(x => x.AllowSynchronousIO = true); // graphql issue workaround
// WebApplication
app.UseGraphQL<CourseSchema>();
app.UseGraphQLPlayground(new GraphQLPlaygroundOptions());
// Courses
public class CourseType : ObjectGraphType<CourseModel>
{
public CourseType()
{
Field(x => x.Id);
Field(x => x.Title);
//Field(x => x.Date); -> nechci ho v modelu
Field(x => x.Price);
Field(x => x.Attendees);
}
}
public class CourseQuery : ObjectGraphType
{
public CourseQuery(UnitOfWork uow, IMapper mapper)
{
Field<ListGraphType<CourseType>>("courses",
arguments: new QueryArguments(new QueryArgument<StringGraphType> {Name = "orderBy"}),
resolve: x =>
{
string sort = x.GetArgument<string>("orderBy");
var data = uow.Courses.AsQueryable();
if (!string.IsNullOrEmpty(sort))
{
if (sort == "title" || sort == "title:asc")
{
data = data.OrderBy(x => x.Title);
}
if (sort == "title:desc")
{
data = data.OrderByDescending(x => x.Title);
}
}
return mapper.ProjectTo<CourseModel>(data);
});
}
}
public class CourseSchema : Schema
{
public CourseSchema(IDependencyResolver resolver) : base(resolver)
{
Query = resolver.Resolve<CourseQuery>();
}
}