Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix description changes causing renames #3182

Merged
merged 1 commit into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
72 changes: 57 additions & 15 deletions src/main/java/graphql/schema/diffing/ana/EditOperationAnalyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -651,15 +651,30 @@ private void handleEnumValuesChanges(List<EditOperation> editOperations, Mapping
private void handleInputFieldChange(EditOperation editOperation) {
Vertex inputField = editOperation.getTargetVertex();
Vertex inputObject = newSchemaGraph.getInputObjectForInputField(inputField);

String oldName = editOperation.getSourceVertex().getName();
String newName = inputObject.getName();
getInputObjectModification(newName).getDetails().add(new InputObjectFieldRename(oldName, inputField.getName()));
String newName = inputField.getName();

if (oldName.equals(newName)) {
// Something else like description could have changed
return;
}

getInputObjectModification(inputObject.getName()).getDetails().add(new InputObjectFieldRename(oldName, newName));
}

private void handleArgumentChange(EditOperation editOperation, Mapping mapping) {
Vertex oldArgument = editOperation.getSourceVertex();
Vertex argument = editOperation.getTargetVertex();

String oldName = oldArgument.getName();
String newName = argument.getName();

if (oldName.equals(newName)) {
// Something else like description could have changed
return;
}

if (!doesArgumentChangeMakeSense(oldArgument, argument, mapping)) {
return;
}
Expand All @@ -668,8 +683,6 @@ private void handleArgumentChange(EditOperation editOperation, Mapping mapping)
if (fieldOrDirective.isOfType(SchemaGraph.DIRECTIVE)) {
Vertex directive = fieldOrDirective;
DirectiveModification directiveModification = getDirectiveModification(directive.getName());
String oldName = oldArgument.getName();
String newName = argument.getName();
directiveModification.getDetails().add(new DirectiveArgumentRename(oldName, newName));
} else {
assertTrue(fieldOrDirective.isOfType(SchemaGraph.FIELD));
Expand All @@ -679,15 +692,11 @@ private void handleArgumentChange(EditOperation editOperation, Mapping mapping)
if (fieldsContainerForField.isOfType(SchemaGraph.OBJECT)) {
Vertex object = fieldsContainerForField;
ObjectModification objectModification = getObjectModification(object.getName());
String oldName = oldArgument.getName();
String newName = argument.getName();
objectModification.getDetails().add(new ObjectFieldArgumentRename(fieldName, oldName, newName));
} else {
assertTrue(fieldsContainerForField.isOfType(SchemaGraph.INTERFACE));
Vertex interfaze = fieldsContainerForField;
InterfaceModification interfaceModification = getInterfaceModification(interfaze.getName());
String oldName = oldArgument.getName();
String newName = argument.getName();
interfaceModification.getDetails().add(new InterfaceFieldArgumentRename(fieldName, oldName, newName));
}
}
Expand Down Expand Up @@ -946,7 +955,6 @@ private void changedTypeVertex(EditOperation editOperation) {
changedDirective(editOperation);
break;
}

}


Expand Down Expand Up @@ -1856,6 +1864,11 @@ private void changedEnum(EditOperation editOperation) {
String oldName = editOperation.getSourceVertex().getName();
String newName = editOperation.getTargetVertex().getName();

if (oldName.equals(newName)) {
// Something else like description could have changed
return;
}

EnumModification modification = new EnumModification(oldName, newName);
enumDifferences.put(oldName, modification);
enumDifferences.put(newName, modification);
Expand All @@ -1865,6 +1878,11 @@ private void changedScalar(EditOperation editOperation) {
String oldName = editOperation.getSourceVertex().getName();
String newName = editOperation.getTargetVertex().getName();

if (oldName.equals(newName)) {
// Something else like description could have changed
return;
}

ScalarModification modification = new ScalarModification(oldName, newName);
scalarDifferences.put(oldName, modification);
scalarDifferences.put(newName, modification);
Expand All @@ -1874,6 +1892,11 @@ private void changedInputObject(EditOperation editOperation) {
String oldName = editOperation.getSourceVertex().getName();
String newName = editOperation.getTargetVertex().getName();

if (oldName.equals(newName)) {
// Something else like description could have changed
return;
}

InputObjectModification modification = new InputObjectModification(oldName, newName);
inputObjectDifferences.put(oldName, modification);
inputObjectDifferences.put(newName, modification);
Expand All @@ -1883,6 +1906,11 @@ private void changedDirective(EditOperation editOperation) {
String oldName = editOperation.getSourceVertex().getName();
String newName = editOperation.getTargetVertex().getName();

if (oldName.equals(newName)) {
// Something else like description could have changed
return;
}

DirectiveModification modification = new DirectiveModification(oldName, newName);
directiveDifferences.put(oldName, modification);
directiveDifferences.put(newName, modification);
Expand All @@ -1892,6 +1920,11 @@ private void changedObject(EditOperation editOperation) {
String oldName = editOperation.getSourceVertex().getName();
String newName = editOperation.getTargetVertex().getName();

if (oldName.equals(newName)) {
// Something else like description could have changed
return;
}

ObjectModification objectModification = new ObjectModification(oldName, newName);
objectDifferences.put(oldName, objectModification);
objectDifferences.put(newName, objectModification);
Expand All @@ -1901,18 +1934,27 @@ private void changedInterface(EditOperation editOperation) {
String oldName = editOperation.getSourceVertex().getName();
String newName = editOperation.getTargetVertex().getName();

if (oldName.equals(newName)) {
// Something else like description could have changed
return;
}

InterfaceModification interfaceModification = new InterfaceModification(oldName, newName);
interfaceDifferences.put(oldName, interfaceModification);
interfaceDifferences.put(newName, interfaceModification);
}

private void changedUnion(EditOperation editOperation) {
String newUnionName = editOperation.getTargetVertex().getName();
String oldUnionName = editOperation.getSourceVertex().getName();
String newName = editOperation.getTargetVertex().getName();
String oldName = editOperation.getSourceVertex().getName();

UnionModification objectModification = new UnionModification(oldUnionName, newUnionName);
unionDifferences.put(oldUnionName, objectModification);
unionDifferences.put(newUnionName, objectModification);
}
if (oldName.equals(newName)) {
// Something else like description could have changed
return;
}

UnionModification objectModification = new UnionModification(oldName, newName);
unionDifferences.put(oldName, objectModification);
unionDifferences.put(newName, objectModification);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2681,6 +2681,219 @@ class EditOperationAnalyzerTest extends Specification {
argumentAddition[0].name == "names"
}

def "change object description"() {
given:
def oldSdl = '''
"HELLO"
type Query {
pet: String
}
'''
def newSdl = '''
type Query {
pet: String
}
'''

when:
def changes = calcDiff(oldSdl, newSdl)

then:
changes.objectDifferences.isEmpty()
}

def "change object field argument description"() {
given:
def oldSdl = '''
type Query {
pet(
age: Int
): String
}
'''
def newSdl = '''
type Query {
pet(
"The age of the pet"
age: Int
): String
}
'''

when:
def changes = calcDiff(oldSdl, newSdl)

then:
changes.objectDifferences.isEmpty()
}

def "change interface description"() {
given:
def oldSdl = '''
type Query {
pet: Pet
}
interface Pet {
name: String
}
'''
def newSdl = '''
type Query {
pet: Pet
}
"Hello World"
interface Pet {
name: String
}
'''

when:
def changes = calcDiff(oldSdl, newSdl)

then:
changes.interfaceDifferences.isEmpty()
}

def "change union description"() {
given:
def oldSdl = '''
type Query {
pet: Pet
}
union Pet = Dog | Cat
type Dog {
name: String
}
type Cat {
name: String
}
'''
def newSdl = '''
type Query {
pet: Pet
}
"----------------"
union Pet = Dog | Cat
type Dog {
name: String
}
type Cat {
name: String
}
'''

when:
def changes = calcDiff(oldSdl, newSdl)

then:
changes.unionDifferences.isEmpty()
}

def "change input object and field description"() {
given:
def oldSdl = '''
type Query {
pets(filter: PetFilter): [ID]
}
"Pet"
input PetFilter {
age: Int
}
'''
def newSdl = '''
type Query {
pets(filter: PetFilter): [ID]
}
"Only pets matching the filter will be returned"
input PetFilter {
"The age in years"
age: Int
}
'''

when:
def changes = calcDiff(oldSdl, newSdl)

then:
changes.inputObjectDifferences.isEmpty()
}

def "change enum type and value description"() {
given:
def oldSdl = '''
type Query {
pet(kind: PetKind): ID
}
enum PetKind {
"doggo"
DOG,
CAT,
}
'''
def newSdl = '''
type Query {
pet(kind: PetKind): ID
}
"The kind of pet"
enum PetKind {
DOG,
CAT,
}
'''

when:
def changes = calcDiff(oldSdl, newSdl)

then:
changes.enumDifferences.isEmpty()
}

def "change scalar description"() {
given:
def oldSdl = '''
scalar Age
type Query {
pet(age: Age): ID
}
'''
def newSdl = '''
"Represents age in years"
scalar Age
type Query {
pet(age: Age): ID
}
'''

when:
def changes = calcDiff(oldSdl, newSdl)

then:
changes.scalarDifferences.isEmpty()
}

def "change directive description"() {
given:
def oldSdl = '''
directive @cat on FIELD_DEFINITION
type Query {
pet: String @cat
}
'''
def newSdl = '''
"A cat or something"
directive @cat on FIELD_DEFINITION
type Query {
pet: String @cat
}
'''

when:
def changes = calcDiff(oldSdl, newSdl)

then:
changes.directiveDifferences.isEmpty()
}

EditOperationAnalysisResult calcDiff(
String oldSdl,
String newSdl
Expand Down