graphql.schema.diff.DiffCtx Maven / Gradle / Ivy
package graphql.schema.diff;
import graphql.Internal;
import graphql.language.Document;
import graphql.language.Type;
import graphql.language.TypeDefinition;
import graphql.schema.diff.reporting.DifferenceReporter;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
import java.util.Optional;
import java.util.Stack;
/*
* A helper class that represents diff state (eg visited types) as well as helpers
*/
@Internal
class DiffCtx {
final List examinedTypes = new ArrayList<>();
final Deque currentTypes = new ArrayDeque<>();
private final DifferenceReporter reporter;
final Document oldDoc;
final Document newDoc;
DiffCtx(DifferenceReporter reporter, Document oldDoc, Document newDoc) {
this.reporter = reporter;
this.oldDoc = oldDoc;
this.newDoc = newDoc;
}
void report(DiffEvent differenceEvent) {
reporter.report(differenceEvent);
}
boolean examiningType(String typeName) {
if (examinedTypes.contains(typeName)) {
return true;
}
examinedTypes.add(typeName);
currentTypes.push(typeName);
return false;
}
void exitType() {
currentTypes.pop();
}
Optional getOldTypeDef(Type type, Class typeDefClass) {
return getType(SchemaDiff.getTypeName(type), typeDefClass, oldDoc);
}
Optional getNewTypeDef(Type type, Class typeDefClass) {
return getType(SchemaDiff.getTypeName(type), typeDefClass, newDoc);
}
private Optional getType(String typeName, Class typeDefClass, Document doc) {
if (typeName == null) {
return Optional.empty();
}
return doc.getDefinitions().stream()
.filter(def -> typeDefClass.isAssignableFrom(def.getClass()))
.map(typeDefClass::cast)
.filter(defT -> defT.getName().equals(typeName))
.findFirst();
}
}