Skip to content

Commit 734d98f

Browse files
committed
feat(compare): Add deprecation changes on input fields
1 parent e434904 commit 734d98f

2 files changed

Lines changed: 97 additions & 18 deletions

File tree

pkg/compare/compare.go

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ const (
8989
InputFieldTypeChanged ChangeType = "INPUT_FIELD_TYPE_CHANGED"
9090
// ObjectTypeInterfaceAdded Object Type Interface Added
9191
ObjectTypeInterfaceAdded ChangeType = "OBJECT_TYPE_INTERFACE_ADDED"
92+
// InputFieldDeprecationAdded Field Deprecation Added
93+
InputFieldDeprecationAdded ChangeType = "INPUT_FIELD_DEPRECATION_ADDED"
94+
// InputFieldDeprecationRemoved Field Deprecation Removed
95+
InputFieldDeprecationRemoved ChangeType = "INPUT_FIELD_DEPRECATION_REMOVED"
96+
// InputFieldDeprecationReasonChanged Field Deprecation Reason Changed
97+
InputFieldDeprecationReasonChanged ChangeType = "INPUT_FIELD_DEPRECATION_REASON_CHANGED"
9298
// ObjectTypeInterfaceRemoved Object Type Interface Removed
9399
ObjectTypeInterfaceRemoved ChangeType = "OBJECT_TYPE_INTERFACE_REMOVED"
94100
// SchemaQueryTypeChanged Schema Query Type Changed
@@ -624,14 +630,18 @@ func checkEnumValueDeprecationChanged(oDef *ast.Definition, nv *ast.EnumValueDef
624630
position: nv.Position,
625631
})
626632
}
627-
if oDep != nil && nDep != nil && oDep.Arguments.ForName("reason") != nDep.Arguments.ForName("reason") {
628-
changes = append(changes, &Change{
629-
changeType: EnumValueDeprecationReasonChanged,
630-
criticalityLevel: NonBreaking,
631-
message: fmt.Sprintf("Enum value '%s' deprecation reason changed in enum '%s' ", ov.Name, oDef.Name),
632-
path: fmt.Sprintf("%s.%s", oDef.Name, ov.Name),
633-
position: nv.Position,
634-
})
633+
if oDep != nil && nDep != nil {
634+
oReason := oDep.Arguments.ForName("reason")
635+
nReason := nDep.Arguments.ForName("reason")
636+
if oReason != nil && nReason != nil && oReason.Value.String() != nReason.Value.String() {
637+
changes = append(changes, &Change{
638+
changeType: EnumValueDeprecationReasonChanged,
639+
criticalityLevel: NonBreaking,
640+
message: fmt.Sprintf("Enum value '%s' deprecation reason changed in enum '%s' ", ov.Name, oDef.Name),
641+
path: fmt.Sprintf("%s.%s", oDef.Name, ov.Name),
642+
position: nv.Position,
643+
})
644+
}
635645
}
636646
return changes
637647
}
@@ -835,31 +845,47 @@ func checkFieldDeprecationChanged(oDef *ast.Definition, nf *ast.FieldDefinition,
835845
oDep := of.Directives.ForName(deprecatedDirective)
836846
nDep := nf.Directives.ForName(deprecatedDirective)
837847
if oDep == nil && nDep != nil {
848+
changeType := FieldDeprecationAdded
849+
if oDef.Kind == ast.InputObject {
850+
changeType = InputFieldDeprecationAdded
851+
}
838852
changes = append(changes, &Change{
839-
changeType: FieldDeprecationAdded,
853+
changeType: changeType,
840854
criticalityLevel: Dangerous,
841855
message: fmt.Sprintf("Field '%s.%s' deprecated in %s ", oDef.Name, of.Name, oDef.Kind),
842856
path: fmt.Sprintf("%s.%s", oDef.Name, of.Name),
843857
position: nf.Position,
844858
})
845859
}
846860
if oDep != nil && nDep == nil {
861+
changeType := FieldDeprecationRemoved
862+
if oDef.Kind == ast.InputObject {
863+
changeType = InputFieldDeprecationRemoved
864+
}
847865
changes = append(changes, &Change{
848-
changeType: FieldDeprecationRemoved,
866+
changeType: changeType,
849867
criticalityLevel: Dangerous,
850868
message: fmt.Sprintf("Field '%s.%s' deprecation removed in %s ", oDef.Name, of.Name, oDef.Kind),
851869
path: fmt.Sprintf("%s.%s", oDef.Name, of.Name),
852870
position: nf.Position,
853871
})
854872
}
855-
if oDep != nil && nDep != nil && oDep.Arguments.ForName("reason") != nDep.Arguments.ForName("reason") {
856-
changes = append(changes, &Change{
857-
changeType: FieldDeprecationReasonChanged,
858-
criticalityLevel: NonBreaking,
859-
message: fmt.Sprintf("Field '%s.%s' deprecation reason changed in %s ", oDef.Name, of.Name, oDef.Kind),
860-
path: fmt.Sprintf("%s.%s", oDef.Name, of.Name),
861-
position: nf.Position,
862-
})
873+
if oDep != nil && nDep != nil {
874+
oReason := oDep.Arguments.ForName("reason")
875+
nReason := nDep.Arguments.ForName("reason")
876+
if oReason != nil && nReason != nil && oReason.Value.String() != nReason.Value.String() {
877+
changeType := FieldDeprecationReasonChanged
878+
if oDef.Kind == ast.InputObject {
879+
changeType = InputFieldDeprecationReasonChanged
880+
}
881+
changes = append(changes, &Change{
882+
changeType: changeType,
883+
criticalityLevel: NonBreaking,
884+
message: fmt.Sprintf("Field '%s.%s' deprecation reason changed in %s ", oDef.Name, of.Name, oDef.Kind),
885+
path: fmt.Sprintf("%s.%s", oDef.Name, of.Name),
886+
position: nf.Position,
887+
})
888+
}
863889
}
864890
return changes
865891
}
@@ -1004,6 +1030,8 @@ func changeInInputFields(oDef *ast.Definition, nDef *ast.Definition) []*Change {
10041030
position: nf.Position,
10051031
})
10061032
}
1033+
//Check deprecation changes
1034+
changes = append(changes, checkFieldDeprecationChanged(oDef, nf, of)...)
10071035
//check change in field directives
10081036
changes = append(changes, changeInTypeFieldDirectives(of.Directives, nf.Directives, fmt.Sprintf("%s.%s", nDef.Name, nf.Name), nf.Position)...)
10091037
}

pkg/compare/compare_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,57 @@ func TestCompareInputObjectFields(t *testing.T) {
820820
criticality: NonBreaking,
821821
ChangeType: InputFieldTypeChanged,
822822
},
823+
{
824+
name: "Input field deprecation added",
825+
oldSchema: `
826+
input UserInput {
827+
name : String
828+
newName: String!
829+
}
830+
`,
831+
newSchema: `
832+
input UserInput {
833+
name : String @deprecated(reason: "use newName")
834+
newName: String!
835+
}
836+
`,
837+
criticality: Dangerous,
838+
ChangeType: InputFieldDeprecationAdded,
839+
},
840+
{
841+
name: "Input field deprecation removed",
842+
oldSchema: `
843+
input UserInput {
844+
name : String @deprecated(reason: "some reason")
845+
newName: String!
846+
}
847+
`,
848+
newSchema: `
849+
input UserInput {
850+
name : String
851+
newName: String!
852+
}
853+
`,
854+
criticality: Dangerous,
855+
ChangeType: InputFieldDeprecationRemoved,
856+
},
857+
{
858+
name: "Input field deprecation reason changed",
859+
oldSchema: `
860+
input UserInput {
861+
name : String @deprecated(reason: "some reason")
862+
newName: String!
863+
}
864+
`,
865+
newSchema: `
866+
input UserInput {
867+
name : String @deprecated(reason: "some reason changed")
868+
newName: String!
869+
}
870+
`,
871+
criticality: NonBreaking,
872+
ChangeType: InputFieldDeprecationReasonChanged,
873+
},
823874
}
824875

825876
for _, tt := range tests {

0 commit comments

Comments
 (0)