1
+ using System . Collections . Immutable ;
2
+ using System . Transactions ;
3
+ using HotChocolate . Language ;
1
4
using HotChocolate . Types ;
2
5
using HotChocolate . Types . Mutable ;
3
6
@@ -25,10 +28,111 @@ public static bool IsRootOperationType(
25
28
this MutableSchemaDefinition schema ,
26
29
MutableObjectTypeDefinition type )
27
30
{
28
- return
29
- schema . QueryType == type
30
- || schema . MutationType == type
31
- || schema . SubscriptionType == type ;
31
+ return schema . QueryType == type || schema . MutationType == type || schema . SubscriptionType == type ;
32
+ }
33
+
34
+ public static List < IDirective > GetPossibleFusionLookupDirectives (
35
+ this MutableSchemaDefinition schema ,
36
+ MutableComplexTypeDefinition type ,
37
+ string ? schemaName = null )
38
+ {
39
+ if ( ! string . IsNullOrEmpty ( schemaName ) && ! type . ExistsInSchema ( schemaName ) )
40
+ {
41
+ return [ ] ;
42
+ }
43
+
44
+ // Get the lookups directly on the requested type.
45
+ var lookups = GetFusionLookupDirectives ( type , schemaName ) ;
46
+
47
+ // Get the lookups of interfaces this type implements.
48
+ var implementsDirectives = type . Directives
49
+ . AsEnumerable ( )
50
+ . Where ( d => d . Name == WellKnownDirectiveNames . FusionImplements )
51
+ . ToImmutableArray ( ) ;
52
+
53
+ foreach ( var implementsDirective in implementsDirectives )
54
+ {
55
+ var implementedInSchemaName = ( string ) implementsDirective . Arguments [ WellKnownArgumentNames . Schema ] . Value ! ;
56
+
57
+ if ( ! string . IsNullOrEmpty ( schemaName ) && implementedInSchemaName != schemaName )
58
+ {
59
+ continue ;
60
+ }
61
+
62
+ var interfaceName = ( string ) implementsDirective . Arguments [ WellKnownArgumentNames . Interface ] . Value ! ;
63
+
64
+ if ( ! schema . Types . TryGetType < MutableInterfaceTypeDefinition > ( interfaceName , out var interfaceType ) )
65
+ {
66
+ continue ;
67
+ }
68
+
69
+ var interfaceLookupsInSchema =
70
+ GetFusionLookupDirectives ( interfaceType , implementedInSchemaName ) ;
71
+
72
+ lookups . AddRange ( interfaceLookupsInSchema ) ;
73
+ }
74
+
75
+ // Get the lookups of unions this type is a member of,
76
+ // if it's an object type.
77
+ if ( type . Kind == TypeKind . Object )
78
+ {
79
+ var unionTypes = schema . Types
80
+ . OfType < MutableUnionTypeDefinition > ( )
81
+ . Where ( u => u . Types . Contains ( type ) )
82
+ . ToImmutableArray ( ) ;
83
+
84
+ foreach ( var unionType in unionTypes )
85
+ {
86
+ if ( ! string . IsNullOrEmpty ( schemaName ) && ! unionType . ExistsInSchema ( schemaName ) )
87
+ {
88
+ continue ;
89
+ }
90
+
91
+ var unionMemberDirectives = unionType . Directives
92
+ . AsEnumerable ( )
93
+ . Where ( d => d . Name == WellKnownDirectiveNames . FusionUnionMember
94
+ && ( string ) d . Arguments [ WellKnownArgumentNames . Member ] . Value ! == type . Name )
95
+ . ToImmutableArray ( ) ;
96
+
97
+ foreach ( var unionMemberDirective in unionMemberDirectives )
98
+ {
99
+ var memberInSchemaName =
100
+ ( string ) unionMemberDirective . Arguments [ WellKnownArgumentNames . Schema ] . Value ! ;
101
+
102
+ if ( ! string . IsNullOrEmpty ( schemaName ) && memberInSchemaName != schemaName )
103
+ {
104
+ continue ;
105
+ }
106
+
107
+ var unionLookups = GetFusionLookupDirectives ( unionType , schemaName ) ;
108
+
109
+ lookups . AddRange ( unionLookups ) ;
110
+ }
111
+ }
112
+ }
113
+
114
+ return lookups ;
115
+ }
116
+
117
+ public static List < IDirective > GetPossibleFusionLookupDirectivesById (
118
+ this MutableSchemaDefinition schema ,
119
+ MutableComplexTypeDefinition type ,
120
+ string ? schemaName = null )
121
+ {
122
+ var lookups = GetPossibleFusionLookupDirectives ( schema , type , schemaName ) ;
123
+ var lookupsById = new List < IDirective > ( ) ;
124
+
125
+ foreach ( var lookup in lookups )
126
+ {
127
+ if ( lookup . Arguments [ WellKnownArgumentNames . Map ] is ListValueNode { Items . Count : 1 } mapArg
128
+ && mapArg . Items [ 0 ] . Value ? . Equals ( WellKnownArgumentNames . Id ) == true
129
+ && lookup . Arguments [ WellKnownArgumentNames . Internal ] is not BooleanValueNode { Value : true } )
130
+ {
131
+ lookupsById . Add ( lookup ) ;
132
+ }
133
+ }
134
+
135
+ return lookupsById ;
32
136
}
33
137
34
138
public static void RemoveUnreferencedTypes (
@@ -55,9 +159,7 @@ public static void RemoveUnreferencedTypes(
55
159
56
160
while ( backlog . TryPop ( out var type ) )
57
161
{
58
- if ( ! touchedTypes . Add ( type )
59
- || type . Kind == TypeKind . Scalar
60
- || type . Kind == TypeKind . Enum )
162
+ if ( ! touchedTypes . Add ( type ) || type . Kind == TypeKind . Scalar || type . Kind == TypeKind . Enum )
61
163
{
62
164
continue ;
63
165
}
@@ -98,6 +200,17 @@ public static void RemoveUnreferencedTypes(
98
200
}
99
201
}
100
202
203
+ private static List < IDirective > GetFusionLookupDirectives (
204
+ IDirectivesProvider directivesProvider ,
205
+ string ? schemaName )
206
+ {
207
+ return directivesProvider . Directives
208
+ . Where ( d => d . Name == WellKnownDirectiveNames . FusionLookup
209
+ && ( string . IsNullOrEmpty ( schemaName )
210
+ || ( string ) d . Arguments [ WellKnownArgumentNames . Schema ] . Value ! == schemaName ) )
211
+ . ToList ( ) ;
212
+ }
213
+
101
214
private static void InspectComplexType (
102
215
ISchemaDefinition schema ,
103
216
IComplexTypeDefinition complexType ,
0 commit comments