@@ -53,6 +53,9 @@ internal override void OnBeforeCreateSchemaInternal(
53
53
schemaBuilder . SetSchema ( d => _schemaContextData = d . Extend ( ) . Definition . ContextData ) ;
54
54
}
55
55
56
+ private ITypeCompletionContext _tc = default ! ;
57
+ private AuthorizeDirectiveType _t = default ! ;
58
+
56
59
public override void OnBeforeCompleteName (
57
60
ITypeCompletionContext completionContext ,
58
61
DefinitionBase definition )
@@ -69,14 +72,33 @@ public override void OnBeforeCompleteName(
69
72
case UnionType when definition is UnionTypeDefinition unionTypeDef :
70
73
_unionTypes . Add ( new UnionTypeInfo ( completionContext , unionTypeDef ) ) ;
71
74
break ;
75
+
76
+ case AuthorizeDirectiveType type :
77
+ _t = type ;
78
+ _tc = completionContext ;
79
+ break ;
72
80
}
73
81
74
82
// note, we do not need to collect interfaces as the object type has a
75
83
// list implements that links to the interfaces that expose an object type.
76
84
}
77
85
78
- public override void OnBeforeCompleteTypes ( )
86
+ public override void OnAfterResolveRootType (
87
+ ITypeCompletionContext completionContext ,
88
+ ObjectTypeDefinition definition ,
89
+ OperationType operationType )
90
+ {
91
+ if ( operationType is OperationType . Query )
92
+ {
93
+ _queryContext = completionContext ;
94
+ }
95
+ }
96
+
97
+ public override void OnBeforeCompleteMetadata ( )
79
98
{
99
+ _t . CompleteMetadata ( _tc ) ;
100
+ ( ( RegisteredType ) _tc ) . Status = TypeStatus . MetadataCompleted ;
101
+
80
102
// at this stage in the type initialization we will create some state that we
81
103
// will use to transform the schema authorization.
82
104
var state = _state = CreateState ( ) ;
@@ -99,18 +121,7 @@ public override void OnBeforeCompleteTypes()
99
121
FindFieldsAndApplyAuthMiddleware ( state ) ;
100
122
}
101
123
102
- public override void OnAfterResolveRootType (
103
- ITypeCompletionContext completionContext ,
104
- ObjectTypeDefinition definition ,
105
- OperationType operationType )
106
- {
107
- if ( operationType is OperationType . Query )
108
- {
109
- _queryContext = completionContext ;
110
- }
111
- }
112
-
113
- public override void OnBeforeCompleteType (
124
+ public override void OnBeforeCompleteMetadata (
114
125
ITypeCompletionContext completionContext ,
115
126
DefinitionBase definition )
116
127
{
@@ -130,101 +141,107 @@ public override void OnAfterMakeExecutable()
130
141
{
131
142
var objectType = ( ObjectType ) type . TypeReg . Type ;
132
143
133
- if ( objectType . ContextData . TryGetValue ( NodeResolver , out var o ) &&
134
- o is NodeResolverInfo nodeResolverInfo )
144
+ if ( ! objectType . ContextData . TryGetValue ( NodeResolver , out var o )
145
+ || o is not NodeResolverInfo nodeResolverInfo )
135
146
{
136
- var pipeline = nodeResolverInfo . Pipeline ;
137
- var directives = ( DirectiveCollection ) objectType . Directives ;
138
- var length = directives . Count ;
139
- ref var start = ref directives . GetReference ( ) ;
147
+ continue ;
148
+ }
140
149
141
- for ( var i = length - 1 ; i >= 0 ; i -- )
142
- {
143
- var directive = Unsafe . Add ( ref start , i ) ;
150
+ var pipeline = nodeResolverInfo . Pipeline ;
151
+ var directives = ( DirectiveCollection ) objectType . Directives ;
152
+ var length = directives . Count ;
153
+ ref var start = ref directives . GetReference ( ) ;
144
154
145
- if ( directive . Type . Name . EqualsOrdinal ( Authorize ) )
146
- {
147
- var authDir = directive . AsValue < AuthorizeDirective > ( ) ;
148
- pipeline = CreateAuthMiddleware ( authDir ) . Middleware . Invoke ( pipeline ) ;
149
- }
150
- }
155
+ for ( var i = length - 1 ; i >= 0 ; i -- )
156
+ {
157
+ var directive = Unsafe . Add ( ref start , i ) ;
151
158
152
- type . TypeDef . ContextData [ NodeResolver ] =
153
- new NodeResolverInfo ( nodeResolverInfo . QueryField , pipeline ) ;
159
+ if ( directive . Type . Name . EqualsOrdinal ( Authorize ) )
160
+ {
161
+ var authDir = directive . AsValue < AuthorizeDirective > ( ) ;
162
+ pipeline = CreateAuthMiddleware ( authDir ) . Middleware . Invoke ( pipeline ) ;
163
+ }
154
164
}
165
+
166
+ type . TypeDef . ContextData [ NodeResolver ] =
167
+ new NodeResolverInfo ( nodeResolverInfo . QueryField , pipeline ) ;
155
168
}
156
169
}
157
170
158
171
private void InspectObjectTypesForAuthDirective ( State state )
159
172
{
160
173
foreach ( var type in _objectTypes )
161
174
{
162
- if ( IsAuthorizedType ( type . TypeDef ) )
175
+ if ( ! IsAuthorizedType ( type . TypeDef ) )
163
176
{
164
- var registration = type . TypeReg ;
165
- var mainTypeRef = registration . TypeReference ;
177
+ continue ;
178
+ }
179
+
180
+ var registration = type . TypeReg ;
181
+ var mainTypeRef = registration . TypeReference ;
166
182
167
- // if this type is a root type we will copy type level auth down to the field.
168
- if ( registration . IsQueryType == true ||
169
- registration . IsMutationType == true ||
170
- registration . IsSubscriptionType == true )
183
+ // if this type is a root type we will copy type level auth down to the field.
184
+ if ( registration . IsQueryType == true ||
185
+ registration . IsMutationType == true ||
186
+ registration . IsSubscriptionType == true )
187
+ {
188
+ foreach ( var fieldDef in type . TypeDef . Fields )
171
189
{
172
- foreach ( var fieldDef in type . TypeDef . Fields )
190
+ // we are not interested in introspection fields or the node fields.
191
+ if ( fieldDef . IsIntrospectionField || fieldDef . IsNodeField ( ) )
173
192
{
174
- // we are not interested in introspection fields or the node fields.
175
- if ( fieldDef . IsIntrospectionField || fieldDef . IsNodeField ( ) )
176
- {
177
- continue ;
178
- }
179
-
180
- // if the field contains the AnonymousAllowed flag we will not
181
- // apply authorization on it.
182
- if ( fieldDef . GetContextData ( ) . ContainsKey ( AllowAnonymous ) )
183
- {
184
- continue ;
185
- }
193
+ continue ;
194
+ }
186
195
187
- ApplyAuthMiddleware ( fieldDef , registration , false ) ;
196
+ // if the field contains the AnonymousAllowed flag we will not
197
+ // apply authorization on it.
198
+ if ( fieldDef . GetContextData ( ) . ContainsKey ( AllowAnonymous ) )
199
+ {
200
+ continue ;
188
201
}
189
- }
190
202
191
- foreach ( var reference in registration . References )
192
- {
193
- state . AuthTypes . Add ( reference ) ;
194
- state . NeedsAuth . Add ( reference ) ;
203
+ ApplyAuthMiddleware ( fieldDef , registration , false ) ;
195
204
}
205
+ }
206
+
207
+ foreach ( var reference in registration . References )
208
+ {
209
+ state . AuthTypes . Add ( reference ) ;
210
+ state . NeedsAuth . Add ( reference ) ;
211
+ }
212
+
213
+ if ( ! type . TypeDef . HasInterfaces )
214
+ {
215
+ continue ;
216
+ }
196
217
197
- if ( type . TypeDef . HasInterfaces )
218
+ CollectInterfaces (
219
+ type . TypeDef . GetInterfaces ( ) ,
220
+ interfaceTypeRef =>
198
221
{
199
- CollectInterfaces (
200
- type . TypeDef . GetInterfaces ( ) ,
201
- interfaceTypeRef =>
222
+ if ( _typeRegistry . TryGetType (
223
+ interfaceTypeRef ,
224
+ out var interfaceTypeReg ) )
225
+ {
226
+ foreach ( var typeRef in interfaceTypeReg . References )
202
227
{
203
- if ( _typeRegistry . TryGetType (
204
- interfaceTypeRef ,
205
- out var interfaceTypeReg ) )
228
+ state . NeedsAuth . Add ( typeRef ) ;
229
+
230
+ if ( ! state . AbstractToConcrete . TryGetValue (
231
+ typeRef ,
232
+ out var authTypeRefs ) )
206
233
{
207
- foreach ( var typeRef in interfaceTypeReg . References )
208
- {
209
- state . NeedsAuth . Add ( typeRef ) ;
210
-
211
- if ( ! state . AbstractToConcrete . TryGetValue (
212
- typeRef ,
213
- out var authTypeRefs ) )
214
- {
215
- authTypeRefs = [ ] ;
216
- state . AbstractToConcrete . Add ( typeRef , authTypeRefs ) ;
217
- }
218
-
219
- authTypeRefs . Add ( mainTypeRef ) ;
220
- }
234
+ authTypeRefs = [ ] ;
235
+ state . AbstractToConcrete . Add ( typeRef , authTypeRefs ) ;
221
236
}
222
- } ,
223
- state ) ;
224
237
225
- state . Completed . Clear ( ) ;
226
- }
227
- }
238
+ authTypeRefs . Add ( mainTypeRef ) ;
239
+ }
240
+ }
241
+ } ,
242
+ state ) ;
243
+
244
+ state . Completed . Clear ( ) ;
228
245
}
229
246
}
230
247
@@ -623,7 +640,7 @@ private State CreateState()
623
640
}
624
641
}
625
642
626
- static file class AuthorizationTypeInterceptorExtensions
643
+ file static class AuthorizationTypeInterceptorExtensions
627
644
{
628
645
public static bool IsNodeField ( this ObjectFieldDefinition fieldDef )
629
646
{
0 commit comments