@@ -36,6 +36,7 @@ import {
36
36
findNamedNode ,
37
37
getChangedNodeOfKind ,
38
38
getDeletedNodeOfKind ,
39
+ getDeletedParentNodeOfKind ,
39
40
getDeprecatedDirectiveNode ,
40
41
parentPath ,
41
42
} from '../utils.js' ;
@@ -370,38 +371,48 @@ export function fieldDeprecationAdded(
370
371
nodeByPath : Map < string , ASTNode > ,
371
372
config : PatchConfig ,
372
373
) {
373
- const fieldNode = getChangedNodeOfKind ( change , nodeByPath , Kind . FIELD_DEFINITION , config ) ;
374
- if ( fieldNode ) {
375
- const hasExistingDeprecationDirective = getDeprecatedDirectiveNode ( fieldNode ) ;
376
- if ( hasExistingDeprecationDirective ) {
377
- handleError (
378
- change ,
379
- new AddedCoordinateAlreadyExistsError ( Kind . DIRECTIVE , '@deprecated' ) ,
380
- config ,
381
- ) ;
382
- } else {
383
- const directiveNode = {
384
- kind : Kind . DIRECTIVE ,
385
- name : nameNode ( GraphQLDeprecatedDirective . name ) ,
386
- ...( change . meta . deprecationReason &&
387
- change . meta . deprecationReason !== DEPRECATION_REASON_DEFAULT
388
- ? {
389
- arguments : [
390
- {
391
- kind : Kind . ARGUMENT ,
392
- name : nameNode ( 'reason' ) ,
393
- value : stringNode ( change . meta . deprecationReason ) ,
394
- } ,
395
- ] ,
396
- }
397
- : { } ) ,
398
- } as DirectiveNode ;
374
+ if ( assertChangeHasPath ( change , config ) ) {
375
+ const fieldNode = nodeByPath . get ( parentPath ( change . path ) ) ;
376
+ if ( fieldNode ) {
377
+ if ( fieldNode . kind !== Kind . FIELD_DEFINITION ) {
378
+ handleError (
379
+ change ,
380
+ new ChangedCoordinateKindMismatchError ( Kind . FIELD_DEFINITION , fieldNode . kind ) ,
381
+ config ,
382
+ ) ;
383
+ return ;
384
+ }
385
+ const hasExistingDeprecationDirective = getDeprecatedDirectiveNode ( fieldNode ) ;
386
+ if ( hasExistingDeprecationDirective ) {
387
+ handleError (
388
+ change ,
389
+ new AddedCoordinateAlreadyExistsError ( Kind . DIRECTIVE , '@deprecated' ) ,
390
+ config ,
391
+ ) ;
392
+ } else {
393
+ const directiveNode = {
394
+ kind : Kind . DIRECTIVE ,
395
+ name : nameNode ( GraphQLDeprecatedDirective . name ) ,
396
+ ...( change . meta . deprecationReason &&
397
+ change . meta . deprecationReason !== DEPRECATION_REASON_DEFAULT
398
+ ? {
399
+ arguments : [
400
+ {
401
+ kind : Kind . ARGUMENT ,
402
+ name : nameNode ( 'reason' ) ,
403
+ value : stringNode ( change . meta . deprecationReason ) ,
404
+ } ,
405
+ ] ,
406
+ }
407
+ : { } ) ,
408
+ } as DirectiveNode ;
399
409
400
- ( fieldNode . directives as DirectiveNode [ ] | undefined ) = [
401
- ...( fieldNode . directives ?? [ ] ) ,
402
- directiveNode ,
403
- ] ;
404
- nodeByPath . set ( [ change . path , `@${ GraphQLDeprecatedDirective . name } ` ] . join ( ',' ) , directiveNode ) ;
410
+ ( fieldNode . directives as DirectiveNode [ ] | undefined ) = [
411
+ ...( fieldNode . directives ?? [ ] ) ,
412
+ directiveNode ,
413
+ ] ;
414
+ nodeByPath . set ( change . path , directiveNode ) ;
415
+ }
405
416
}
406
417
}
407
418
}
@@ -411,16 +422,24 @@ export function fieldDeprecationRemoved(
411
422
nodeByPath : Map < string , ASTNode > ,
412
423
config : PatchConfig ,
413
424
) {
414
- const fieldNode = getChangedNodeOfKind ( change , nodeByPath , Kind . FIELD_DEFINITION , config ) ;
415
- if ( fieldNode ) {
416
- const hasExistingDeprecationDirective = getDeprecatedDirectiveNode ( fieldNode ) ;
417
- if ( hasExistingDeprecationDirective ) {
418
- ( fieldNode . directives as DirectiveNode [ ] | undefined ) = fieldNode . directives ?. filter (
419
- d => d . name . value !== GraphQLDeprecatedDirective . name ,
420
- ) ;
421
- nodeByPath . delete ( [ change . path , `@${ GraphQLDeprecatedDirective . name } ` ] . join ( '.' ) ) ;
422
- } else {
423
- handleError ( change , new DeletedCoordinateNotFound ( Kind . DIRECTIVE , '@deprecated' ) , config ) ;
425
+ if ( assertChangeHasPath ( change , config ) ) {
426
+ const fieldNode = getDeletedParentNodeOfKind (
427
+ change ,
428
+ nodeByPath ,
429
+ Kind . FIELD_DEFINITION ,
430
+ 'directives' ,
431
+ config ,
432
+ ) ;
433
+ if ( fieldNode ) {
434
+ const hasExistingDeprecationDirective = getDeprecatedDirectiveNode ( fieldNode ) ;
435
+ if ( hasExistingDeprecationDirective ) {
436
+ ( fieldNode . directives as DirectiveNode [ ] | undefined ) = fieldNode . directives ?. filter (
437
+ d => d . name . value !== GraphQLDeprecatedDirective . name ,
438
+ ) ;
439
+ nodeByPath . delete ( change . path ) ;
440
+ } else {
441
+ handleError ( change , new DeletedCoordinateNotFound ( Kind . DIRECTIVE , '@deprecated' ) , config ) ;
442
+ }
424
443
}
425
444
}
426
445
}
0 commit comments