Skip to content

Commit

Permalink
Merge pull request #14 from sonamp/compare-deprecation
Browse files Browse the repository at this point in the history
feat(compare): Add deprecation changes on input fields
  • Loading branch information
sonamp committed Apr 13, 2023
2 parents e434904 + 734d98f commit 60f7202
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 18 deletions.
64 changes: 46 additions & 18 deletions pkg/compare/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ const (
InputFieldTypeChanged ChangeType = "INPUT_FIELD_TYPE_CHANGED"
// ObjectTypeInterfaceAdded Object Type Interface Added
ObjectTypeInterfaceAdded ChangeType = "OBJECT_TYPE_INTERFACE_ADDED"
// InputFieldDeprecationAdded Field Deprecation Added
InputFieldDeprecationAdded ChangeType = "INPUT_FIELD_DEPRECATION_ADDED"
// InputFieldDeprecationRemoved Field Deprecation Removed
InputFieldDeprecationRemoved ChangeType = "INPUT_FIELD_DEPRECATION_REMOVED"
// InputFieldDeprecationReasonChanged Field Deprecation Reason Changed
InputFieldDeprecationReasonChanged ChangeType = "INPUT_FIELD_DEPRECATION_REASON_CHANGED"
// ObjectTypeInterfaceRemoved Object Type Interface Removed
ObjectTypeInterfaceRemoved ChangeType = "OBJECT_TYPE_INTERFACE_REMOVED"
// SchemaQueryTypeChanged Schema Query Type Changed
Expand Down Expand Up @@ -624,14 +630,18 @@ func checkEnumValueDeprecationChanged(oDef *ast.Definition, nv *ast.EnumValueDef
position: nv.Position,
})
}
if oDep != nil && nDep != nil && oDep.Arguments.ForName("reason") != nDep.Arguments.ForName("reason") {
changes = append(changes, &Change{
changeType: EnumValueDeprecationReasonChanged,
criticalityLevel: NonBreaking,
message: fmt.Sprintf("Enum value '%s' deprecation reason changed in enum '%s' ", ov.Name, oDef.Name),
path: fmt.Sprintf("%s.%s", oDef.Name, ov.Name),
position: nv.Position,
})
if oDep != nil && nDep != nil {
oReason := oDep.Arguments.ForName("reason")
nReason := nDep.Arguments.ForName("reason")
if oReason != nil && nReason != nil && oReason.Value.String() != nReason.Value.String() {
changes = append(changes, &Change{
changeType: EnumValueDeprecationReasonChanged,
criticalityLevel: NonBreaking,
message: fmt.Sprintf("Enum value '%s' deprecation reason changed in enum '%s' ", ov.Name, oDef.Name),
path: fmt.Sprintf("%s.%s", oDef.Name, ov.Name),
position: nv.Position,
})
}
}
return changes
}
Expand Down Expand Up @@ -835,31 +845,47 @@ func checkFieldDeprecationChanged(oDef *ast.Definition, nf *ast.FieldDefinition,
oDep := of.Directives.ForName(deprecatedDirective)
nDep := nf.Directives.ForName(deprecatedDirective)
if oDep == nil && nDep != nil {
changeType := FieldDeprecationAdded
if oDef.Kind == ast.InputObject {
changeType = InputFieldDeprecationAdded
}
changes = append(changes, &Change{
changeType: FieldDeprecationAdded,
changeType: changeType,
criticalityLevel: Dangerous,
message: fmt.Sprintf("Field '%s.%s' deprecated in %s ", oDef.Name, of.Name, oDef.Kind),
path: fmt.Sprintf("%s.%s", oDef.Name, of.Name),
position: nf.Position,
})
}
if oDep != nil && nDep == nil {
changeType := FieldDeprecationRemoved
if oDef.Kind == ast.InputObject {
changeType = InputFieldDeprecationRemoved
}
changes = append(changes, &Change{
changeType: FieldDeprecationRemoved,
changeType: changeType,
criticalityLevel: Dangerous,
message: fmt.Sprintf("Field '%s.%s' deprecation removed in %s ", oDef.Name, of.Name, oDef.Kind),
path: fmt.Sprintf("%s.%s", oDef.Name, of.Name),
position: nf.Position,
})
}
if oDep != nil && nDep != nil && oDep.Arguments.ForName("reason") != nDep.Arguments.ForName("reason") {
changes = append(changes, &Change{
changeType: FieldDeprecationReasonChanged,
criticalityLevel: NonBreaking,
message: fmt.Sprintf("Field '%s.%s' deprecation reason changed in %s ", oDef.Name, of.Name, oDef.Kind),
path: fmt.Sprintf("%s.%s", oDef.Name, of.Name),
position: nf.Position,
})
if oDep != nil && nDep != nil {
oReason := oDep.Arguments.ForName("reason")
nReason := nDep.Arguments.ForName("reason")
if oReason != nil && nReason != nil && oReason.Value.String() != nReason.Value.String() {
changeType := FieldDeprecationReasonChanged
if oDef.Kind == ast.InputObject {
changeType = InputFieldDeprecationReasonChanged
}
changes = append(changes, &Change{
changeType: changeType,
criticalityLevel: NonBreaking,
message: fmt.Sprintf("Field '%s.%s' deprecation reason changed in %s ", oDef.Name, of.Name, oDef.Kind),
path: fmt.Sprintf("%s.%s", oDef.Name, of.Name),
position: nf.Position,
})
}
}
return changes
}
Expand Down Expand Up @@ -1004,6 +1030,8 @@ func changeInInputFields(oDef *ast.Definition, nDef *ast.Definition) []*Change {
position: nf.Position,
})
}
//Check deprecation changes
changes = append(changes, checkFieldDeprecationChanged(oDef, nf, of)...)
//check change in field directives
changes = append(changes, changeInTypeFieldDirectives(of.Directives, nf.Directives, fmt.Sprintf("%s.%s", nDef.Name, nf.Name), nf.Position)...)
}
Expand Down
51 changes: 51 additions & 0 deletions pkg/compare/compare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,57 @@ func TestCompareInputObjectFields(t *testing.T) {
criticality: NonBreaking,
ChangeType: InputFieldTypeChanged,
},
{
name: "Input field deprecation added",
oldSchema: `
input UserInput {
name : String
newName: String!
}
`,
newSchema: `
input UserInput {
name : String @deprecated(reason: "use newName")
newName: String!
}
`,
criticality: Dangerous,
ChangeType: InputFieldDeprecationAdded,
},
{
name: "Input field deprecation removed",
oldSchema: `
input UserInput {
name : String @deprecated(reason: "some reason")
newName: String!
}
`,
newSchema: `
input UserInput {
name : String
newName: String!
}
`,
criticality: Dangerous,
ChangeType: InputFieldDeprecationRemoved,
},
{
name: "Input field deprecation reason changed",
oldSchema: `
input UserInput {
name : String @deprecated(reason: "some reason")
newName: String!
}
`,
newSchema: `
input UserInput {
name : String @deprecated(reason: "some reason changed")
newName: String!
}
`,
criticality: NonBreaking,
ChangeType: InputFieldDeprecationReasonChanged,
},
}

for _, tt := range tests {
Expand Down

0 comments on commit 60f7202

Please sign in to comment.