-
-
Notifications
You must be signed in to change notification settings - Fork 722
/
CollectionSegmentType~1.cs
134 lines (114 loc) · 4.42 KB
/
CollectionSegmentType~1.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using HotChocolate.Configuration;
using HotChocolate.Resolvers;
using HotChocolate.Types.Descriptors;
using HotChocolate.Types.Descriptors.Definitions;
using static HotChocolate.Properties.TypeResources;
namespace HotChocolate.Types.Pagination;
internal class CollectionSegmentType
: ObjectType
, IPageType
{
internal CollectionSegmentType(
string? collectionSegmentName,
TypeReference nodeType,
bool withTotalCount)
{
if (nodeType is null)
{
throw new ArgumentNullException(nameof(nodeType));
}
Definition = CreateTypeDefinition(withTotalCount);
if (collectionSegmentName is not null)
{
Definition.Name = collectionSegmentName + "CollectionSegment";
}
else
{
Definition.Configurations.Add(
new CompleteConfiguration(
(c, d) =>
{
var type = c.GetType<IType>(nodeType);
var definition = (ObjectTypeDefinition)d;
definition.Name = type.NamedType().Name + "CollectionSegment";
},
Definition,
ApplyConfigurationOn.BeforeNaming,
nodeType,
TypeDependencyFulfilled.Named));
}
Definition.Configurations.Add(
new CompleteConfiguration(
(c, d) =>
{
ItemType = c.GetType<IOutputType>(nodeType);
var definition = (ObjectTypeDefinition)d;
var nodes = definition.Fields.First(IsItemsField);
nodes.Type = TypeReference.Parse($"[{ItemType.Print()}]", TypeContext.Output);
},
Definition,
ApplyConfigurationOn.BeforeNaming,
nodeType,
TypeDependencyFulfilled.Named));
Definition.Dependencies.Add(new(nodeType));
}
/// <summary>
/// Gets the item type of this collection segment.
/// </summary>
public IOutputType ItemType { get; private set; } = default!;
protected override void OnBeforeRegisterDependencies(
ITypeDiscoveryContext context,
DefinitionBase definition)
{
var typeRef = context.TypeInspector.GetOutputTypeRef(typeof(CollectionSegmentInfoType));
context.Dependencies.Add(new(typeRef));
base.OnBeforeRegisterDependencies(context, definition);
}
private static ObjectTypeDefinition CreateTypeDefinition(bool withTotalCount)
{
var definition = new ObjectTypeDefinition
{
Description = CollectionSegmentType_Description,
RuntimeType = typeof(CollectionSegment)
};
definition.Fields.Add(new(
Names.PageInfo,
CollectionSegmentType_PageInfo_Description,
TypeReference.Parse("CollectionSegmentInfo!"),
pureResolver: GetPagingInfo));
definition.Fields.Add(new(
Names.Items,
CollectionSegmentType_Items_Description,
pureResolver: GetItems) {CustomSettings = {ContextDataKeys.Items}});
if (withTotalCount)
{
definition.Fields.Add(new(
Names.TotalCount,
type: TypeReference.Parse($"{ScalarNames.Int}!"),
resolver: GetTotalCountAsync));
}
return definition;
}
private static IPageInfo GetPagingInfo(IPureResolverContext context)
=> context.Parent<CollectionSegment>().Info;
private static IEnumerable<object?> GetItems(IPureResolverContext context)
=> context.Parent<CollectionSegment>().Items;
private static async ValueTask<object?> GetTotalCountAsync(IResolverContext context)
=> await context.Parent<CollectionSegment>().GetTotalCountAsync(context.RequestAborted);
private static bool IsItemsField(ObjectFieldDefinition field)
=> field.CustomSettings.Count > 0 && field.CustomSettings[0].Equals(ContextDataKeys.Items);
internal static class Names
{
public const string PageInfo = "pageInfo";
public const string Items = "items";
public const string TotalCount = "totalCount";
}
private static class ContextDataKeys
{
public const string Items = "HotChocolate.Types.CollectionSegment.Items";
}
}