|
1 | 1 | package graphql.schema.idl; |
2 | 2 |
|
| 3 | +import graphql.Assert; |
3 | 4 | import graphql.GraphQLError; |
4 | 5 | import graphql.PublicApi; |
5 | 6 | import graphql.language.Definition; |
|
10 | 11 | import graphql.language.InterfaceTypeExtensionDefinition; |
11 | 12 | import graphql.language.ObjectTypeDefinition; |
12 | 13 | import graphql.language.ObjectTypeExtensionDefinition; |
| 14 | +import graphql.language.SDLDefinition; |
13 | 15 | import graphql.language.ScalarTypeDefinition; |
14 | 16 | import graphql.language.ScalarTypeExtensionDefinition; |
15 | 17 | import graphql.language.SchemaDefinition; |
|
32 | 34 | import java.util.function.Function; |
33 | 35 | import java.util.stream.Collectors; |
34 | 36 |
|
| 37 | +import static graphql.Assert.assertNotNull; |
35 | 38 | import static java.util.Optional.ofNullable; |
36 | 39 |
|
37 | 40 | /** |
|
41 | 44 | @PublicApi |
42 | 45 | public class TypeDefinitionRegistry { |
43 | 46 |
|
44 | | - private final Map<String, ScalarTypeDefinition> scalarTypes = new LinkedHashMap<>(); |
45 | | - private final Map<String, List<ObjectTypeExtensionDefinition>> typeExtensions = new LinkedHashMap<>(); |
| 47 | + private final Map<String, List<ObjectTypeExtensionDefinition>> objectTypeExtensions = new LinkedHashMap<>(); |
46 | 48 | private final Map<String, List<InterfaceTypeExtensionDefinition>> interfaceTypeExtensions = new LinkedHashMap<>(); |
47 | 49 | private final Map<String, List<UnionTypeExtensionDefinition>> unionTypeExtensions = new LinkedHashMap<>(); |
48 | 50 | private final Map<String, List<EnumTypeExtensionDefinition>> enumTypeExtensions = new LinkedHashMap<>(); |
49 | 51 | private final Map<String, List<ScalarTypeExtensionDefinition>> scalarTypeExtensions = new LinkedHashMap<>(); |
50 | 52 | private final Map<String, List<InputObjectTypeExtensionDefinition>> inputObjectTypeExtensions = new LinkedHashMap<>(); |
| 53 | + |
51 | 54 | private final Map<String, TypeDefinition> types = new LinkedHashMap<>(); |
| 55 | + private final Map<String, ScalarTypeDefinition> scalarTypes = new LinkedHashMap<>(); |
52 | 56 | private final Map<String, DirectiveDefinition> directiveDefinitions = new LinkedHashMap<>(); |
53 | 57 | private SchemaDefinition schema; |
54 | 58 |
|
@@ -98,8 +102,8 @@ public TypeDefinitionRegistry merge(TypeDefinitionRegistry typeRegistry) throws |
98 | 102 | this.directiveDefinitions.putAll(tempDirectiveDefs); |
99 | 103 | // |
100 | 104 | // merge type extensions since they can be redefined by design |
101 | | - typeRegistry.typeExtensions.forEach((key, value) -> { |
102 | | - List<ObjectTypeExtensionDefinition> currentList = this.typeExtensions |
| 105 | + typeRegistry.objectTypeExtensions.forEach((key, value) -> { |
| 106 | + List<ObjectTypeExtensionDefinition> currentList = this.objectTypeExtensions |
103 | 107 | .computeIfAbsent(key, k -> new ArrayList<>()); |
104 | 108 | currentList.addAll(value); |
105 | 109 | }); |
@@ -143,7 +147,7 @@ public Optional<GraphQLError> add(Definition definition) { |
143 | 147 | // extensions |
144 | 148 | if (definition instanceof ObjectTypeExtensionDefinition) { |
145 | 149 | ObjectTypeExtensionDefinition newEntry = (ObjectTypeExtensionDefinition) definition; |
146 | | - return defineExt(typeExtensions, newEntry, ObjectTypeExtensionDefinition::getName); |
| 150 | + return defineExt(objectTypeExtensions, newEntry, ObjectTypeExtensionDefinition::getName); |
147 | 151 | } else if (definition instanceof InterfaceTypeExtensionDefinition) { |
148 | 152 | InterfaceTypeExtensionDefinition newEntry = (InterfaceTypeExtensionDefinition) definition; |
149 | 153 | return defineExt(interfaceTypeExtensions, newEntry, InterfaceTypeExtensionDefinition::getName); |
@@ -181,6 +185,42 @@ public Optional<GraphQLError> add(Definition definition) { |
181 | 185 | return Optional.empty(); |
182 | 186 | } |
183 | 187 |
|
| 188 | + public void remove(SDLDefinition definition) { |
| 189 | + assertNotNull("definition to remove can't be null"); |
| 190 | + if (definition instanceof ObjectTypeExtensionDefinition) { |
| 191 | + removeFromList(objectTypeExtensions, (TypeDefinition) definition); |
| 192 | + } else if (definition instanceof InterfaceTypeExtensionDefinition) { |
| 193 | + removeFromList(interfaceTypeExtensions, (TypeDefinition) definition); |
| 194 | + } else if (definition instanceof UnionTypeExtensionDefinition) { |
| 195 | + removeFromList(unionTypeExtensions, (TypeDefinition) definition); |
| 196 | + } else if (definition instanceof EnumTypeExtensionDefinition) { |
| 197 | + removeFromList(enumTypeExtensions, (TypeDefinition) definition); |
| 198 | + } else if (definition instanceof ScalarTypeExtensionDefinition) { |
| 199 | + removeFromList(scalarTypeExtensions, (TypeDefinition) definition); |
| 200 | + } else if (definition instanceof InputObjectTypeExtensionDefinition) { |
| 201 | + removeFromList(inputObjectTypeExtensions, (TypeDefinition) definition); |
| 202 | + } else if (definition instanceof ScalarTypeDefinition) { |
| 203 | + scalarTypes.remove(((ScalarTypeDefinition) definition).getName()); |
| 204 | + } else if (definition instanceof TypeDefinition) { |
| 205 | + types.remove(((TypeDefinition) definition).getName()); |
| 206 | + } else if (definition instanceof DirectiveDefinition) { |
| 207 | + directiveDefinitions.remove(((DirectiveDefinition) definition).getName()); |
| 208 | + } else if (definition instanceof SchemaDefinition) { |
| 209 | + schema = null; |
| 210 | + } else { |
| 211 | + Assert.assertShouldNeverHappen(); |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + private void removeFromList(Map source, TypeDefinition value) { |
| 216 | + List<TypeDefinition> list = (List<TypeDefinition>) source.get(value.getName()); |
| 217 | + if (list == null) { |
| 218 | + return; |
| 219 | + } |
| 220 | + list.remove(value); |
| 221 | + } |
| 222 | + |
| 223 | + |
184 | 224 | private <T extends TypeDefinition> Optional<GraphQLError> define(Map<String, T> source, Map<String, T> target, T newEntry) { |
185 | 225 | String name = newEntry.getName(); |
186 | 226 |
|
@@ -222,7 +262,7 @@ public Map<String, ScalarTypeDefinition> scalars() { |
222 | 262 | } |
223 | 263 |
|
224 | 264 | public Map<String, List<ObjectTypeExtensionDefinition>> objectTypeExtensions() { |
225 | | - return new LinkedHashMap<>(typeExtensions); |
| 265 | + return new LinkedHashMap<>(objectTypeExtensions); |
226 | 266 | } |
227 | 267 |
|
228 | 268 | public Map<String, List<InterfaceTypeExtensionDefinition>> interfaceTypeExtensions() { |
@@ -267,7 +307,7 @@ public Map<String, DirectiveDefinition> getDirectiveDefinitions() { |
267 | 307 |
|
268 | 308 | public boolean hasType(TypeName typeName) { |
269 | 309 | String name = typeName.getName(); |
270 | | - return types.containsKey(name) || ScalarInfo.STANDARD_SCALAR_DEFINITIONS.containsKey(name) || scalarTypes.containsKey(name) || typeExtensions.containsKey(name); |
| 310 | + return types.containsKey(name) || ScalarInfo.STANDARD_SCALAR_DEFINITIONS.containsKey(name) || scalarTypes.containsKey(name) || objectTypeExtensions.containsKey(name); |
271 | 311 | } |
272 | 312 |
|
273 | 313 | public Optional<TypeDefinition> getType(Type type) { |
|
0 commit comments