-
-
Notifications
You must be signed in to change notification settings - Fork 722
/
ObjectTypeExtension~1.cs
66 lines (56 loc) · 1.81 KB
/
ObjectTypeExtension~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
using System;
using HotChocolate.Configuration;
using HotChocolate.Types.Descriptors;
using HotChocolate.Types.Descriptors.Definitions;
#nullable enable
namespace HotChocolate.Types;
/// <summary>
/// <para>
/// Object type extensions are used to represent a type which has been extended
/// from some original type.
/// </para>
/// <para>
/// For example, this might be used to represent local data, or by a GraphQL service
/// which is itself an extension of another GraphQL service.
/// </para>
/// </summary>
public class ObjectTypeExtension<T> : ObjectTypeExtension
{
private Action<IObjectTypeDescriptor<T>>? _configure;
/// <summary>
/// Initializes a new instance of <see cref="ObjectType{T}"/>.
/// </summary>
public ObjectTypeExtension()
{
_configure = Configure;
}
/// <summary>
/// Initializes a new instance of <see cref="ObjectTypeExtension{T}"/>.
/// </summary>
public ObjectTypeExtension(Action<IObjectTypeDescriptor<T>> configure)
{
_configure = configure
?? throw new ArgumentNullException(nameof(configure));
}
protected override ObjectTypeDefinition CreateDefinition(
ITypeDiscoveryContext context)
{
var descriptor = ObjectTypeDescriptor.NewExtension<T>(context.DescriptorContext);
_configure!(descriptor);
_configure = null;
return descriptor.CreateDefinition();
}
/// <summary>
/// Override this to configure the type.
/// </summary>
/// <param name="descriptor">
/// The descriptor allows to configure the interface type.
/// </param>
protected virtual void Configure(IObjectTypeDescriptor<T> descriptor)
{
}
protected sealed override void Configure(IObjectTypeDescriptor descriptor)
{
throw new NotSupportedException();
}
}