-
-
Notifications
You must be signed in to change notification settings - Fork 722
/
ExtendObjectTypeAttribute.cs
239 lines (207 loc) · 6.57 KB
/
ExtendObjectTypeAttribute.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
using System;
using HotChocolate.Internal;
using HotChocolate.Language;
using HotChocolate.Types.Descriptors;
using HotChocolate.Types.Descriptors.Definitions;
using static HotChocolate.Types.FieldBindingFlags;
#nullable enable
namespace HotChocolate.Types;
/// <summary>
/// Annotate classes which represent extensions to other object types.
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]
public sealed class ExtendObjectTypeAttribute
: ObjectTypeDescriptorAttribute
, ITypeAttribute
{
private string? _name;
public ExtendObjectTypeAttribute(string? name = null)
{
_name = name;
}
public ExtendObjectTypeAttribute(OperationType operationType)
{
_name = operationType.ToString();
}
public ExtendObjectTypeAttribute(Type extendsType)
{
ExtendsType = extendsType;
}
/// <summary>
/// Gets the GraphQL type name to which this extension is bound to.
/// </summary>
public string? Name
{
get => _name;
[Obsolete("Use the new constructor.")]
set => _name = value;
}
/// <summary>
/// Defines if this attribute is inherited. The default is <c>false</c>.
/// </summary>
public bool Inherited { get; set; }
/// <summary>
/// Defines that static members are included.
/// </summary>
public bool IncludeStaticMembers { get; set; }
TypeKind ITypeAttribute.Kind => TypeKind.Object;
bool ITypeAttribute.IsTypeExtension => true;
/// <summary>
/// Gets the .NET type to which this extension is bound to.
/// If this is a base type or an interface the extension will bind to all types
/// inheriting or implementing the type.
/// </summary>
public Type? ExtendsType { get; }
/// <summary>
/// Gets a set of field names that will be removed from the extended type.
/// </summary>
public string[]? IgnoreFields { get; set; }
/// <summary>
/// Gets a set of property names that will be removed from the extended type.
/// </summary>
public string[]? IgnoreProperties { get; set; }
/// <summary>
/// Applies the type extension configuration.
/// </summary>
/// <param name="context">
/// The descriptor context.
/// </param>
/// <param name="descriptor">
/// The object type descriptor.
/// </param>
/// <param name="type">
/// The type to which this instance is annotated to.
/// </param>
protected override void OnConfigure(
IDescriptorContext context,
IObjectTypeDescriptor descriptor,
Type type)
{
if (ExtendsType is not null)
{
descriptor.ExtendsType(ExtendsType);
}
if (!string.IsNullOrEmpty(Name))
{
descriptor.Name(Name);
}
var definition = descriptor.Extend().Definition;
definition.Fields.BindingBehavior = BindingBehavior.Implicit;
if (IncludeStaticMembers)
{
definition.FieldBindingFlags = Instance | Static;
}
if (IgnoreFields is not null)
{
descriptor.Extend().OnBeforeCreate(d =>
{
foreach (var fieldName in IgnoreFields)
{
d.FieldIgnores.Add(new ObjectFieldBinding(
fieldName,
ObjectFieldBindingType.Field));
}
});
}
if (IgnoreProperties is not null)
{
descriptor.Extend().OnBeforeCreate(d =>
{
foreach (var fieldName in IgnoreProperties)
{
d.FieldIgnores.Add(new ObjectFieldBinding(
fieldName,
ObjectFieldBindingType.Property));
}
});
}
}
}
#if NET6_0_OR_GREATER
/// <summary>
/// Annotate classes which represent extensions to other object types.
/// </summary>
public sealed class ExtendObjectTypeAttribute<T>
: ObjectTypeDescriptorAttribute
, ITypeAttribute
{
public ExtendObjectTypeAttribute()
{
ExtendsType = typeof(T);
}
/// <summary>
/// Defines if this attribute is inherited. The default is <c>false</c>.
/// </summary>
public bool Inherited { get; set; }
TypeKind ITypeAttribute.Kind => TypeKind.Object;
bool ITypeAttribute.IsTypeExtension => true;
/// <summary>
/// Gets the .NET type to which this extension is bound to.
/// If this is a base type or an interface the extension will bind to all types
/// inheriting or implementing the type.
/// </summary>
public Type? ExtendsType { get; }
/// <summary>
/// Gets a set of field names that will be removed from the extended type.
/// </summary>
public string[]? IgnoreFields { get; set; }
/// <summary>
/// Defines that static members are included.
/// </summary>
public bool IncludeStaticMembers { get; set; }
/// <summary>
/// Gets a set of property names that will be removed from the extended type.
/// </summary>
public string[]? IgnoreProperties { get; set; }
/// <summary>
/// Applies the type extension configuration.
/// </summary>
/// <param name="context">
/// The descriptor context.
/// </param>
/// <param name="descriptor">
/// The object type descriptor.
/// </param>
/// <param name="type">
/// The type to which this instance is annotated to.
/// </param>
protected override void OnConfigure(
IDescriptorContext context,
IObjectTypeDescriptor descriptor,
Type type)
{
if (ExtendsType is not null)
{
descriptor.ExtendsType(ExtendsType);
}
if (IncludeStaticMembers)
{
descriptor.Extend().Definition.FieldBindingFlags = Instance | Static;
}
if (IgnoreFields is not null)
{
descriptor.Extend().OnBeforeCreate(d =>
{
foreach (var fieldName in IgnoreFields)
{
d.FieldIgnores.Add(new ObjectFieldBinding(
fieldName,
ObjectFieldBindingType.Field));
}
});
}
if (IgnoreProperties is not null)
{
descriptor.Extend().OnBeforeCreate(d =>
{
foreach (var fieldName in IgnoreProperties)
{
d.FieldIgnores.Add(new ObjectFieldBinding(
fieldName,
ObjectFieldBindingType.Property));
}
});
}
}
}
#endif