@@ -161,9 +161,13 @@ protected virtual IObjectGraphType ToObjectGraphType(GraphQLObjectTypeDefinition
161
161
var type = new ObjectGraphType ( ) ;
162
162
type . Name = astType . Name . Value ;
163
163
type . Description = typeConfig . Description ;
164
- type . DeprecationReason = typeConfig . DeprecationReason ;
165
164
type . IsTypeOf = typeConfig . IsTypeOfFunc ;
166
165
166
+ ApplyDeprecatedDirective ( astType . Directives , reason =>
167
+ {
168
+ type . DeprecationReason = typeConfig . DeprecationReason ?? reason ;
169
+ } ) ;
170
+
167
171
CopyMetadata ( type , typeConfig ) ;
168
172
169
173
var fields = astType . Fields . Select ( f => ToFieldType ( type . Name , f ) ) ;
@@ -189,7 +193,6 @@ protected virtual FieldType ToFieldType(string parentTypeName, GraphQLFieldDefin
189
193
var field = new FieldType ( ) ;
190
194
field . Name = fieldDef . Name . Value ;
191
195
field . Description = fieldConfig . Description ;
192
- field . DeprecationReason = fieldConfig . DeprecationReason ;
193
196
field . ResolvedType = ToGraphType ( fieldDef . Type ) ;
194
197
field . Resolver = fieldConfig . Resolver ;
195
198
@@ -198,16 +201,50 @@ protected virtual FieldType ToFieldType(string parentTypeName, GraphQLFieldDefin
198
201
var args = fieldDef . Arguments . Select ( ToArguments ) ;
199
202
field . Arguments = new QueryArguments ( args ) ;
200
203
204
+ ApplyDeprecatedDirective ( fieldDef . Directives , reason =>
205
+ {
206
+ field . DeprecationReason = fieldConfig . DeprecationReason ?? reason ;
207
+ } ) ;
208
+
201
209
return field ;
202
210
}
203
211
204
- protected virtual FieldType ToFieldType ( GraphQLInputValueDefinition inputDef )
212
+ private static string DeprecatedDefaultValue = DirectiveGraphType . Deprecated . Arguments . Find ( "reason" ) . DefaultValue . ToString ( ) ;
213
+ private void ApplyDeprecatedDirective ( IEnumerable < GraphQLDirective > directives , Action < string > apply )
214
+ {
215
+ var deprecated = directives . Directive ( "deprecated" ) ;
216
+
217
+ if ( deprecated != null )
218
+ {
219
+ var arg = deprecated . Arguments . Argument ( "reason" ) ;
220
+ var value = "" ;
221
+
222
+ if ( arg != null )
223
+ {
224
+ value = ToValue ( arg . Value ) . ToString ( ) ;
225
+ }
226
+
227
+ if ( string . IsNullOrWhiteSpace ( value ) )
228
+ {
229
+ value = DeprecatedDefaultValue ;
230
+ }
231
+
232
+ apply ( value ) ;
233
+ }
234
+ }
235
+
236
+ protected virtual FieldType ToFieldType ( string parentTypeName , GraphQLInputValueDefinition inputDef )
205
237
{
206
238
var field = new FieldType ( ) ;
207
239
field . Name = inputDef . Name . Value ;
208
240
field . ResolvedType = ToGraphType ( inputDef . Type ) ;
209
241
field . DefaultValue = ToValue ( inputDef . DefaultValue ) ;
210
242
243
+ ApplyDeprecatedDirective ( inputDef . Directives , reason =>
244
+ {
245
+ field . DeprecationReason = reason ;
246
+ } ) ;
247
+
211
248
return field ;
212
249
}
213
250
@@ -220,6 +257,11 @@ protected virtual InterfaceGraphType ToInterfaceType(GraphQLInterfaceTypeDefinit
220
257
type . Description = typeConfig . Description ;
221
258
type . ResolveType = typeConfig . ResolveType ;
222
259
260
+ ApplyDeprecatedDirective ( interfaceDef . Directives , reason =>
261
+ {
262
+ type . DeprecationReason = typeConfig . DeprecationReason ?? reason ;
263
+ } ) ;
264
+
223
265
CopyMetadata ( type , typeConfig ) ;
224
266
225
267
var fields = interfaceDef . Fields . Select ( f => ToFieldType ( type . Name , f ) ) ;
@@ -237,6 +279,11 @@ protected virtual UnionGraphType ToUnionType(GraphQLUnionTypeDefinition unionDef
237
279
type . Description = typeConfig . Description ;
238
280
type . ResolveType = typeConfig . ResolveType ;
239
281
282
+ ApplyDeprecatedDirective ( unionDef . Directives , reason =>
283
+ {
284
+ type . DeprecationReason = typeConfig . DeprecationReason ?? reason ;
285
+ } ) ;
286
+
240
287
CopyMetadata ( type , typeConfig ) ;
241
288
242
289
var possibleTypes = unionDef . Types . Select ( x => GetType ( x . Name . Value ) ) ;
@@ -248,8 +295,13 @@ protected virtual InputObjectGraphType ToInputObjectType(GraphQLInputObjectTypeD
248
295
{
249
296
var type = new InputObjectGraphType ( ) ;
250
297
type . Name = inputDef . Name . Value ;
251
- var fields = inputDef . Fields . Select ( ToFieldType ) ;
252
298
299
+ ApplyDeprecatedDirective ( inputDef . Directives , reason =>
300
+ {
301
+ type . DeprecationReason = reason ;
302
+ } ) ;
303
+
304
+ var fields = inputDef . Fields . Select ( x => ToFieldType ( type . Name , x ) ) ;
253
305
fields . Apply ( f => type . AddField ( f ) ) ;
254
306
255
307
return type ;
@@ -259,6 +311,12 @@ protected virtual EnumerationGraphType ToEnumerationType(GraphQLEnumTypeDefiniti
259
311
{
260
312
var type = new EnumerationGraphType ( ) ;
261
313
type . Name = enumDef . Name . Value ;
314
+
315
+ ApplyDeprecatedDirective ( enumDef . Directives , reason =>
316
+ {
317
+ type . DeprecationReason = reason ;
318
+ } ) ;
319
+
262
320
var values = enumDef . Values . Select ( ToEnumValue ) ;
263
321
values . Apply ( type . AddValue ) ;
264
322
return type ;
@@ -292,6 +350,12 @@ private EnumValueDefinition ToEnumValue(GraphQLEnumValueDefinition valDef)
292
350
var val = new EnumValueDefinition ( ) ;
293
351
val . Value = valDef . Name . Value ;
294
352
val . Name = valDef . Name . Value ;
353
+
354
+ ApplyDeprecatedDirective ( valDef . Directives , reason =>
355
+ {
356
+ val . DeprecationReason = reason ;
357
+ } ) ;
358
+
295
359
return val ;
296
360
}
297
361
@@ -413,4 +477,19 @@ protected virtual void CopyMetadata(IProvideMetadata target, IProvideMetadata so
413
477
} ) ;
414
478
}
415
479
}
480
+
481
+ internal static class SchemaExtensions
482
+ {
483
+ public static GraphQLDirective Directive ( this IEnumerable < GraphQLDirective > directives , string name )
484
+ {
485
+ return directives ? . FirstOrDefault (
486
+ x => string . Equals ( x . Name . Value , name , StringComparison . OrdinalIgnoreCase ) ) ;
487
+ }
488
+
489
+ public static GraphQLArgument Argument ( this IEnumerable < GraphQLArgument > arguments , string name )
490
+ {
491
+ return arguments ? . FirstOrDefault (
492
+ x => string . Equals ( x . Name . Value , name , StringComparison . OrdinalIgnoreCase ) ) ;
493
+ }
494
+ }
416
495
}
0 commit comments