Merge pull request #3205 from gnawf/restrict-parent-changes · graphql-java/graphql-java@58bc1c2 · GitHub
Skip to content

Commit 58bc1c2

Browse files
authored
Merge pull request #3205 from gnawf/restrict-parent-changes
Restrict parent changes
2 parents db8e0a6 + 9d56392 commit 58bc1c2

7 files changed

Lines changed: 275 additions & 27 deletions

File tree

src/main/java/graphql/schema/diffing/DiffImpl.java

Lines changed: 25 additions & 6 deletions

src/main/java/graphql/schema/diffing/EditorialCostForMapping.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ public static int baseEditorialCostForMapping(Mapping mapping, // can be a parti
3434
List<EditOperation> editOperationsResult) {
3535
int cost = 0;
3636

37-
3837
for (int i = 0; i < mapping.size(); i++) {
3938
Vertex sourceVertex = mapping.getSource(i);
4039
Vertex targetVertex = mapping.getTarget(i);

src/main/java/graphql/schema/diffing/Mapping.java

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.ArrayList;
88
import java.util.Collections;
99
import java.util.List;
10+
import java.util.Map;
1011
import java.util.function.BiConsumer;
1112
import java.util.function.Consumer;
1213

@@ -20,6 +21,7 @@
2021
public class Mapping {
2122

2223

24+
private final Map<Vertex, Vertex> fixedParentRestrictions;
2325
private final BiMap<Vertex, Vertex> fixedMappings;
2426
private final List<Vertex> fixedSourceList;
2527
private final List<Vertex> fixedTargetList;
@@ -28,12 +30,14 @@ public class Mapping {
2830
private final List<Vertex> sourceList;
2931
private final List<Vertex> targetList;
3032

31-
private Mapping(BiMap<Vertex, Vertex> fixedMappings,
33+
private Mapping(Map<Vertex, Vertex> fixedParentRestrictions,
34+
BiMap<Vertex, Vertex> fixedMappings,
3235
List<Vertex> fixedSourceList,
3336
List<Vertex> fixedTargetList,
3437
BiMap<Vertex, Vertex> map,
3538
List<Vertex> sourceList,
3639
List<Vertex> targetList) {
40+
this.fixedParentRestrictions = fixedParentRestrictions;
3741
this.fixedMappings = fixedMappings;
3842
this.fixedSourceList = fixedSourceList;
3943
this.fixedTargetList = fixedTargetList;
@@ -42,9 +46,26 @@ private Mapping(BiMap<Vertex, Vertex> fixedMappings,
4246
this.targetList = targetList;
4347
}
4448

45-
public static Mapping newMapping(BiMap<Vertex, Vertex> fixedMappings, List<Vertex> fixedSourceList, List<Vertex> fixedTargetList) {
46-
return new Mapping(fixedMappings, fixedSourceList, fixedTargetList, HashBiMap.create(), Collections.emptyList(), Collections.emptyList());
49+
public static Mapping newMapping(Map<Vertex, Vertex> fixedParentRestrictions,
50+
BiMap<Vertex, Vertex> fixedMappings,
51+
List<Vertex> fixedSourceList,
52+
List<Vertex> fixedTargetList) {
53+
return new Mapping(
54+
fixedParentRestrictions,
55+
fixedMappings,
56+
fixedSourceList,
57+
fixedTargetList,
58+
HashBiMap.create(),
59+
Collections.emptyList(),
60+
Collections.emptyList());
61+
}
62+
63+
public boolean hasParentRestriction(Vertex v) {
64+
return fixedParentRestrictions.containsKey(v);
65+
}
4766

67+
public Vertex getParentRestriction(Vertex v) {
68+
return fixedParentRestrictions.get(v);
4869
}
4970

5071
public Vertex getSource(Vertex target) {
@@ -118,14 +139,14 @@ public Mapping copyMappingWithLastElementRemoved() {
118139
newMap.remove(this.sourceList.get(this.sourceList.size() - 1));
119140
List<Vertex> newSourceList = new ArrayList<>(this.sourceList.subList(0, this.sourceList.size() - 1));
120141
List<Vertex> newTargetList = new ArrayList<>(this.targetList.subList(0, this.targetList.size() - 1));
121-
return new Mapping(fixedMappings, fixedSourceList, fixedTargetList, newMap, newSourceList, newTargetList);
142+
return new Mapping(fixedParentRestrictions, fixedMappings, fixedSourceList, fixedTargetList, newMap, newSourceList, newTargetList);
122143
}
123144

124145
public Mapping copy() {
125146
HashBiMap<Vertex, Vertex> newMap = HashBiMap.create(map);
126147
List<Vertex> newSourceList = new ArrayList<>(this.sourceList);
127148
List<Vertex> newTargetList = new ArrayList<>(this.targetList);
128-
return new Mapping(fixedMappings, fixedSourceList, fixedTargetList, newMap, newSourceList, newTargetList);
149+
return new Mapping(fixedParentRestrictions, fixedMappings, fixedSourceList, fixedTargetList, newMap, newSourceList, newTargetList);
129150
}
130151

131152
public Mapping extendMapping(Vertex source, Vertex target) {
@@ -135,7 +156,7 @@ public Mapping extendMapping(Vertex source, Vertex target) {
135156
newSourceList.add(source);
136157
List<Vertex> newTargetList = new ArrayList<>(this.targetList);
137158
newTargetList.add(target);
138-
return new Mapping(fixedMappings, fixedSourceList, fixedTargetList, newMap, newSourceList, newTargetList);
159+
return new Mapping(fixedParentRestrictions, fixedMappings, fixedSourceList, fixedTargetList, newMap, newSourceList, newTargetList);
139160
}
140161

141162
public void forEachTarget(Consumer<? super Vertex> action) {
@@ -168,6 +189,6 @@ public Mapping invert() {
168189
Vertex t = map.get(s);
169190
invertedMap.put(t, s);
170191
}
171-
return new Mapping(invertedFixedMappings, fixedTargetList, fixedSourceList, invertedMap, targetList, sourceList);
192+
return new Mapping(fixedParentRestrictions, invertedFixedMappings, fixedTargetList, fixedSourceList, invertedMap, targetList, sourceList);
172193
}
173194
}

src/main/java/graphql/schema/diffing/PossibleMappingsCalculator.java

Lines changed: 118 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public class PossibleMappingsCalculator {
6262
private final SchemaGraph targetGraph;
6363
private final PossibleMappings possibleMappings;
6464

65-
public final static Map<String, List<VertexContextSegment>> typeContexts = new LinkedHashMap<>();
65+
private static final Map<String, List<VertexContextSegment>> typeContexts = new LinkedHashMap<>();
6666

6767
static {
6868
typeContexts.put(SCHEMA, schemaContext());
@@ -787,13 +787,11 @@ public PossibleMappings calculate() {
787787
// System.out.println("-------------");
788788
}
789789
}
790+
790791
return possibleMappings;
791792
}
792793

793-
794794
public abstract static class VertexContextSegment {
795-
796-
797795
public VertexContextSegment() {
798796
}
799797

@@ -1022,7 +1020,121 @@ private void calcPossibleMappingImpl(
10221020
possibleMappings.putPossibleMappings(contextId, possibleSourceVertices, possibleTargetVertices, typeNameForDebug);
10231021
}
10241022

1025-
public PossibleMappings getIsolatedVertices() {
1026-
return possibleMappings;
1023+
public Map<Vertex, Vertex> getFixedParentRestrictions() {
1024+
return getFixedParentRestrictions(
1025+
sourceGraph,
1026+
possibleMappings.fixedOneToOneSources,
1027+
possibleMappings.fixedOneToOneMappings
1028+
);
1029+
}
1030+
1031+
public Map<Vertex, Vertex> getFixedParentRestrictionsInverse(Map<Vertex, Vertex> fixedOneToOneMappingsInverted) {
1032+
return getFixedParentRestrictions(
1033+
targetGraph,
1034+
possibleMappings.fixedOneToOneTargets,
1035+
fixedOneToOneMappingsInverted
1036+
);
1037+
}
1038+
1039+
/**
1040+
* This computes the initial set of parent restrictions based on the fixed portion of the mapping.
1041+
* <p>
1042+
* See {@link Mapping} for definition of fixed vs non-fixed.
1043+
* <p>
1044+
* If a {@link Vertex} is present in the output {@link Map} then the value is the parent the
1045+
* vertex MUST map to.
1046+
* <p>
1047+
* e.g. for an output {collar: Dog} then the collar vertex must be a child of Dog in the mapping.
1048+
*
1049+
* @return Map where key is any vertex, and the value is the parent that vertex must map to
1050+
*/
1051+
private Map<Vertex, Vertex> getFixedParentRestrictions(SchemaGraph sourceGraph,
1052+
List<Vertex> fixedSourceVertices,
1053+
Map<Vertex, Vertex> fixedOneToOneMappings) {
1054+
Assert.assertFalse(fixedOneToOneMappings.isEmpty());
1055+
1056+
List<Vertex> needsFixing = new ArrayList<>(sourceGraph.getVertices());
1057+
needsFixing.removeAll(fixedSourceVertices);
1058+
1059+
Map<Vertex, Vertex> restrictions = new LinkedHashMap<>();
1060+
1061+
for (Vertex vertex : needsFixing) {
1062+
if (hasParentRestrictions(vertex)) {
1063+
Vertex sourceParent = sourceGraph.getSingleAdjacentInverseVertex(vertex);
1064+
Vertex fixedTargetParent = fixedOneToOneMappings.get(sourceParent);
1065+
1066+
if (fixedTargetParent != null) {
1067+
for (Edge edge : sourceGraph.getAdjacentEdgesNonCopy(sourceParent)) {
1068+
Vertex sibling = edge.getTo();
1069+
1070+
if (hasParentRestrictions(sibling)) {
1071+
restrictions.put(sibling, fixedTargetParent);
1072+
}
1073+
}
1074+
}
1075+
}
1076+
}
1077+
1078+
return restrictions;
1079+
}
1080+
1081+
/**
1082+
* This computes the initial set of parent restrictions based on the given non-fixed mapping.
1083+
* <p>
1084+
* i.e. this introduces restrictions as the {@link Mapping} is being built, as decisions
1085+
* can have knock on effects on other vertices' possible mappings.
1086+
* <p>
1087+
* See {@link Mapping} for definition of fixed vs non-fixed.
1088+
* <p>
1089+
* If a {@link Vertex} is present in the output {@link Map} then the value is the parent the
1090+
* vertex MUST map to.
1091+
* <p>
1092+
* e.g. for an output {collar: Dog} then the collar vertex must be a child of Dog in the mapping.
1093+
*
1094+
* @param mapping the mapping to get non-fixed parent restrictions for
1095+
* @return Map where key is any vertex, and the value is the parent that vertex must map to
1096+
*/
1097+
public Map<Vertex, Vertex> getNonFixedParentRestrictions(SchemaGraph sourceGraph,
1098+
SchemaGraph targetGraph,
1099+
Mapping mapping) {
1100+
Map<Vertex, Vertex> restrictions = new LinkedHashMap<>();
1101+
1102+
mapping.forEachNonFixedSourceAndTarget((source, target) -> {
1103+
if (hasChildrenRestrictions(source) && hasChildrenRestrictions(target)) {
1104+
for (Edge edge : sourceGraph.getAdjacentEdgesNonCopy(source)) {
1105+
Vertex child = edge.getTo();
1106+
1107+
if (hasParentRestrictions(child)) {
1108+
restrictions.put(child, target);
1109+
}
1110+
}
1111+
} else if (hasParentRestrictions(source) && hasParentRestrictions(target)) {
1112+
Vertex sourceParent = sourceGraph.getSingleAdjacentInverseVertex(source);
1113+
Vertex targetParent = targetGraph.getSingleAdjacentInverseVertex(target);
1114+
1115+
for (Edge edge : sourceGraph.getAdjacentEdgesNonCopy(sourceParent)) {
1116+
Vertex sibling = edge.getTo();
1117+
1118+
if (hasParentRestrictions(sibling)) {
1119+
restrictions.put(sibling, targetParent);
1120+
}
1121+
}
1122+
}
1123+
});
1124+
1125+
return restrictions;
1126+
}
1127+
1128+
public static boolean hasParentRestrictions(Vertex vertex) {
1129+
return vertex.isOfType(SchemaGraph.FIELD)
1130+
|| vertex.isOfType(SchemaGraph.INPUT_FIELD)
1131+
|| vertex.isOfType(SchemaGraph.ENUM_VALUE)
1132+
|| vertex.isOfType(SchemaGraph.ARGUMENT);
1133+
}
1134+
1135+
public static boolean hasChildrenRestrictions(Vertex vertex) {
1136+
return vertex.isOfType(SchemaGraph.INPUT_OBJECT)
1137+
|| vertex.isOfType(SchemaGraph.OBJECT)
1138+
|| vertex.isOfType(SchemaGraph.ENUM);
10271139
}
10281140
}

src/main/java/graphql/schema/diffing/SchemaDiffing.java

Lines changed: 4 additions & 5 deletions

0 commit comments

Comments
 (0)