-
-
Notifications
You must be signed in to change notification settings - Fork 725
/
__Type.cs
149 lines (127 loc) · 5.63 KB
/
__Type.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
#pragma warning disable IDE1006 // Naming Styles
using System.Collections.Generic;
using System.Linq;
using HotChocolate.Properties;
#nullable enable
namespace HotChocolate.Types.Introspection
{
[Introspection]
internal sealed class __Type : ObjectType<IType>
{
protected override void Configure(IObjectTypeDescriptor<IType> descriptor)
{
descriptor
.Name(Names.__Type)
.Description(TypeResources.Type_Description)
// Introspection types must always be bound explicitly so that we
// do not get any interference with conventions.
.BindFields(BindingBehavior.Explicit);
descriptor
.Field(t => t.Kind)
.Name(Names.Kind)
.Type<NonNullType<__TypeKind>>();
descriptor
.Field(Names.Name)
.Type<StringType>()
.ResolveWith<Resolvers>(t => t.GetName(default!));
descriptor
.Field(Names.Description)
.Type<StringType>()
.ResolveWith<Resolvers>(t => t.GetDescription(default!));
descriptor
.Field(Names.Fields)
.Argument(Names.IncludeDeprecated, a => a.Type<BooleanType>().DefaultValue(false))
.Type<ListType<NonNullType<__Field>>>()
.ResolveWith<Resolvers>(t => t.GetFields(default!, default));
descriptor
.Field(Names.Interfaces)
.Type<ListType<NonNullType<__Type>>>()
.ResolveWith<Resolvers>(t => t.GetInterfaces(default!));
descriptor
.Field(Names.PossibleTypes)
.Type<ListType<NonNullType<__Type>>>()
.ResolveWith<Resolvers>(t => t.GetPossibleTypes(default!, default!));
descriptor
.Field(Names.EnumValues)
.Argument(Names.IncludeDeprecated, a => a.Type<BooleanType>().DefaultValue(false))
.Type<ListType<NonNullType<__EnumValue>>>()
.ResolveWith<Resolvers>(t => t.GetEnumValues(default!, default!));
descriptor
.Field(Names.InputFields)
.Type<ListType<NonNullType<__InputValue>>>()
.ResolveWith<Resolvers>(t => t.GetInputFields(default!));
descriptor
.Field(Names.OfType)
.Type<__Type>()
.ResolveWith<Resolvers>(t => t.GetOfType(default!));
descriptor
.Field(Names.SpecifiedBy)
.Type<StringType>()
.ResolveWith<Resolvers>(t => t.GetSpecifiedBy(default!));
}
private class Resolvers
{
public string? GetName([Parent] IType type) =>
type is INamedType n ? n.Name.Value : null;
public string? GetDescription([Parent] IType type) =>
type is INamedType n ? n.Description : null;
public IEnumerable<IOutputField>? GetFields([Parent] IType type, bool includeDeprecated)
{
if (type is IComplexOutputType complexType)
{
if (!includeDeprecated)
{
return complexType.Fields
.Where(t => !t.IsIntrospectionField && !t.IsDeprecated);
}
return complexType.Fields.Where(t => !t.IsIntrospectionField);
}
return null;
}
public IEnumerable<IInterfaceType>? GetInterfaces([Parent] IType type) =>
type is IComplexOutputType complexType ? complexType.Interfaces : null;
public IEnumerable<IType>? GetPossibleTypes(ISchema schema, [Parent]INamedType type) =>
type.IsAbstractType() ? schema.GetPossibleTypes(type) : null;
public IEnumerable<IEnumValue>? GetEnumValues(
[Parent] IType type,
bool includeDeprecated)
{
if (type is EnumType et)
{
IReadOnlyCollection<IEnumValue> values = et.Values;
return includeDeprecated ? values : values.Where(t => !t.IsDeprecated);
}
return null;
}
public IEnumerable<InputField>? GetInputFields([Parent] IType type) =>
type is InputObjectType iot ? iot.Fields : null;
public IType? GetOfType([Parent] IType type) =>
type switch
{
ListType lt => lt.ElementType,
NonNullType nnt => nnt.Type,
_ => null
};
public string? GetSpecifiedBy([Parent] IType type) =>
type is ScalarType scalar
? scalar.SpecifiedBy?.ToString()
: null;
}
public static class Names
{
public const string __Type = "__Type";
public const string Kind = "kind";
public const string Name = "name";
public const string Description = "description";
public const string Fields = "fields";
public const string Interfaces = "interfaces";
public const string PossibleTypes = "possibleTypes";
public const string EnumValues = "enumValues";
public const string InputFields = "inputFields";
public const string OfType = "ofType";
public const string SpecifiedBy = "specifiedBy";
public const string IncludeDeprecated = "includeDeprecated";
}
}
}
#pragma warning restore IDE1006 // Naming Styles