Skip to content

Commit 0bc2fd5

Browse files
authored
Merge pull request #3518 from gnawf/empty-directives-holder
Return empty singleton DirectivesHolder if directives are empty
2 parents 78d3278 + 9d41e98 commit 0bc2fd5

11 files changed

Lines changed: 19 additions & 12 deletions

src/main/java/graphql/DirectivesUtil.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import graphql.util.FpKit;
1010

1111
import java.util.Collection;
12+
import java.util.Collections;
1213
import java.util.List;
1314
import java.util.Map;
1415
import java.util.Optional;
@@ -91,7 +92,6 @@ public static GraphQLDirective getFirstDirective(String name, Map<String, List<G
9192
* directives collection takes precedence.
9293
*
9394
* @param directiveContainer the schema element holding applied directives
94-
*
9595
* @return a combined list unique by name
9696
*/
9797
public static List<GraphQLAppliedDirective> toAppliedDirectives(GraphQLDirectiveContainer directiveContainer) {
@@ -104,7 +104,6 @@ public static List<GraphQLAppliedDirective> toAppliedDirectives(GraphQLDirective
104104
*
105105
* @param appliedDirectives the applied directives to use
106106
* @param directives the legacy directives to use
107-
*
108107
* @return a combined list unique by name
109108
*/
110109
public static List<GraphQLAppliedDirective> toAppliedDirectives(Collection<GraphQLAppliedDirective> appliedDirectives, Collection<GraphQLDirective> directives) {
@@ -127,6 +126,7 @@ public static List<GraphQLAppliedDirective> toAppliedDirectives(Collection<Graph
127126
* A holder class that breaks a list of directives into maps to be more easily accessible in using classes
128127
*/
129128
public static class DirectivesHolder {
129+
private static final DirectivesHolder EMPTY_HOLDER = new DirectivesHolder(Collections.emptyList(), Collections.emptyList());
130130

131131
private final ImmutableMap<String, List<GraphQLDirective>> allDirectivesByName;
132132
private final ImmutableMap<String, GraphQLDirective> nonRepeatableDirectivesByName;
@@ -145,7 +145,14 @@ public DirectivesHolder(Collection<GraphQLDirective> allDirectives, Collection<G
145145

146146
this.allAppliedDirectives = ImmutableList.copyOf(allAppliedDirectives);
147147
this.allAppliedDirectivesByName = ImmutableMap.copyOf(FpKit.groupingBy(allAppliedDirectives, GraphQLAppliedDirective::getName));
148+
}
149+
150+
public static DirectivesHolder create(List<GraphQLDirective> directives, List<GraphQLAppliedDirective> appliedDirectives) {
151+
if (directives.isEmpty() && appliedDirectives.isEmpty()) {
152+
return EMPTY_HOLDER;
153+
}
148154

155+
return new DirectivesHolder(directives, appliedDirectives);
149156
}
150157

151158
public ImmutableMap<String, List<GraphQLDirective>> getAllDirectivesByName() {

src/main/java/graphql/schema/GraphQLArgument.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ private GraphQLArgument(String name,
8282
this.value = value;
8383
this.definition = definition;
8484
this.deprecationReason = deprecationReason;
85-
this.directivesHolder = new DirectivesUtil.DirectivesHolder(directives, appliedDirectives);
85+
this.directivesHolder = DirectivesUtil.DirectivesHolder.create(directives, appliedDirectives);
8686
}
8787

8888

src/main/java/graphql/schema/GraphQLEnumType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ private GraphQLEnumType(String name,
6666
this.description = description;
6767
this.definition = definition;
6868
this.extensionDefinitions = ImmutableList.copyOf(extensionDefinitions);
69-
this.directivesHolder = new DirectivesUtil.DirectivesHolder(directives, appliedDirectives);
69+
this.directivesHolder = DirectivesUtil.DirectivesHolder.create(directives, appliedDirectives);
7070
this.valueDefinitionMap = buildMap(values);
7171
}
7272

src/main/java/graphql/schema/GraphQLEnumValueDefinition.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ private GraphQLEnumValueDefinition(String name,
4848
this.description = description;
4949
this.value = value;
5050
this.deprecationReason = deprecationReason;
51-
this.directivesHolder = new DirectivesUtil.DirectivesHolder(directives, appliedDirectives);
51+
this.directivesHolder = DirectivesUtil.DirectivesHolder.create(directives, appliedDirectives);
5252
this.definition = definition;
5353
}
5454

src/main/java/graphql/schema/GraphQLFieldDefinition.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ private GraphQLFieldDefinition(String name,
6666
this.originalType = type;
6767
this.dataFetcherFactory = dataFetcherFactory;
6868
this.arguments = ImmutableList.copyOf(arguments);
69-
this.directivesHolder = new DirectivesUtil.DirectivesHolder(directives, appliedDirectives);
69+
this.directivesHolder = DirectivesUtil.DirectivesHolder.create(directives, appliedDirectives);
7070
this.deprecationReason = deprecationReason;
7171
this.definition = definition;
7272
}

src/main/java/graphql/schema/GraphQLInputObjectField.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ private GraphQLInputObjectField(
6262
this.originalType = type;
6363
this.defaultValue = defaultValue;
6464
this.description = description;
65-
this.directivesHolder = new DirectivesUtil.DirectivesHolder(directives, appliedDirectives);
65+
this.directivesHolder = DirectivesUtil.DirectivesHolder.create(directives, appliedDirectives);
6666
this.definition = definition;
6767
this.deprecationReason = deprecationReason;
6868
}

src/main/java/graphql/schema/GraphQLInputObjectType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ private GraphQLInputObjectType(String name,
6161
this.description = description;
6262
this.definition = definition;
6363
this.extensionDefinitions = ImmutableList.copyOf(extensionDefinitions);
64-
this.directives = new DirectivesUtil.DirectivesHolder(directives, appliedDirectives);
64+
this.directives = DirectivesUtil.DirectivesHolder.create(directives, appliedDirectives);
6565
this.fieldMap = buildDefinitionMap(fields);
6666
this.isOneOf = hasOneOf(directives, appliedDirectives);
6767
}

src/main/java/graphql/schema/GraphQLInterfaceType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ private GraphQLInterfaceType(String name,
7777
this.interfaceComparator = interfaceComparator;
7878
this.originalInterfaces = ImmutableList.copyOf(sortTypes(interfaceComparator, interfaces));
7979
this.extensionDefinitions = ImmutableList.copyOf(extensionDefinitions);
80-
this.directivesHolder = new DirectivesUtil.DirectivesHolder(directives, appliedDirectives);
80+
this.directivesHolder = DirectivesUtil.DirectivesHolder.create(directives, appliedDirectives);
8181
this.fieldDefinitionsByName = buildDefinitionMap(fieldDefinitions);
8282
}
8383

src/main/java/graphql/schema/GraphQLObjectType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ private GraphQLObjectType(String name,
7474
this.originalInterfaces = ImmutableList.copyOf(sortTypes(interfaceComparator, interfaces));
7575
this.definition = definition;
7676
this.extensionDefinitions = ImmutableList.copyOf(extensionDefinitions);
77-
this.directivesHolder = new DirectivesUtil.DirectivesHolder(directives, appliedDirectives);
77+
this.directivesHolder = DirectivesUtil.DirectivesHolder.create(directives, appliedDirectives);
7878
this.fieldDefinitionsByName = buildDefinitionMap(fieldDefinitions);
7979
}
8080

src/main/java/graphql/schema/GraphQLScalarType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ private GraphQLScalarType(String name,
6464
this.description = description;
6565
this.coercing = coercing;
6666
this.definition = definition;
67-
this.directivesHolder = new DirectivesUtil.DirectivesHolder(directives, appliedDirectives);
67+
this.directivesHolder = DirectivesUtil.DirectivesHolder.create(directives, appliedDirectives);
6868
this.extensionDefinitions = ImmutableList.copyOf(extensionDefinitions);
6969
this.specifiedByUrl = specifiedByUrl;
7070
}

0 commit comments

Comments
 (0)