Skip to content

Commit aff7c04

Browse files
committed
support_repeatable_directives
1 parent d2b66ab commit aff7c04

File tree

14 files changed

+145
-54
lines changed

14 files changed

+145
-54
lines changed

src/main/java/graphql/execution/ConditionalNodes.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
import graphql.Internal;
44
import graphql.VisibleForTesting;
55
import graphql.language.Directive;
6+
import graphql.util.FpKit;
67

78
import java.util.List;
89
import java.util.Map;
910

1011
import static graphql.Directives.IncludeDirective;
1112
import static graphql.Directives.SkipDirective;
12-
import static graphql.language.NodeUtil.directivesByName;
1313

1414

1515
@Internal
@@ -25,15 +25,9 @@ public boolean shouldInclude(Map<String, Object> variables, List<Directive> dire
2525
return !skip && include;
2626
}
2727

28-
private Directive getDirectiveByName(List<Directive> directives, String name) {
29-
if (directives.isEmpty()) {
30-
return null;
31-
}
32-
return directivesByName(directives).get(name);
33-
}
34-
3528
private boolean getDirectiveResult(Map<String, Object> variables, List<Directive> directives, String directiveName, boolean defaultValue) {
36-
Directive directive = getDirectiveByName(directives, directiveName);
29+
Directive directive = FpKit.findOneOrNull(directives, d -> d.getName().equals(directiveName));
30+
3731
if (directive != null) {
3832
Map<String, Object> argumentValues = valuesResolver.getArgumentValues(SkipDirective.getArguments(), directive.getArguments(), variables);
3933
return (Boolean) argumentValues.get("if");

src/main/java/graphql/execution/defer/DeferSupport.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import graphql.execution.reactive.SingleSubscriberPublisher;
1010
import graphql.language.Directive;
1111
import graphql.language.Field;
12+
import graphql.util.FpKit;
1213
import org.reactivestreams.Publisher;
1314

1415
import java.util.Deque;
@@ -34,7 +35,12 @@ public class DeferSupport {
3435

3536
public boolean checkForDeferDirective(MergedField currentField, Map<String,Object> variables) {
3637
for (Field field : currentField.getFields()) {
37-
Directive directive = field.getDirective(DeferDirective.getName());
38+
List<Directive> directives = field.getDirectives();
39+
if (directives == null || directives.isEmpty()) {
40+
return false;
41+
}
42+
43+
Directive directive = FpKit.findOneOrNull(directives, d -> d.getName().equals(DeferDirective.getName()));
3844
if (directive != null) {
3945
Map<String, Object> argumentValues = valuesResolver.getArgumentValues(DeferDirective.getArguments(), directive.getArguments(), variables);
4046
return (Boolean) argumentValues.get("if");

src/main/java/graphql/introspection/Introspection.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,7 @@ public enum DirectiveLocation {
416416
@SuppressWarnings("deprecation") // because graphql spec still has the deprecated fields
417417
public static final GraphQLObjectType __Directive = newObject()
418418
.name("__Directive")
419+
.description("The __Directive type represents a Directive that a server supports.")
419420
.field(newFieldDefinition()
420421
.name("name")
421422
.type(nonNull(GraphQLString)))

src/main/java/graphql/language/DirectivesContainer.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,20 @@ public interface DirectivesContainer<T extends DirectivesContainer> extends Node
2020
List<Directive> getDirectives();
2121

2222
/**
23-
* @return a a map of directives by directive name
23+
* @return a map of directives by directive name
2424
*/
25-
default Map<String, Directive> getDirectivesByName() {
25+
default Map<String, List<Directive>> getDirectivesByName() {
2626
return directivesByName(getDirectives());
2727
}
2828

2929
/**
30-
* Returns a directive with the provided name
30+
* Returns a directive list with the provided name
3131
*
3232
* @param directiveName the name of the directive to retrieve
3333
*
34-
* @return the directive or null if there is one one with that name
34+
* @return the directive list or null if there is one one with that name
3535
*/
36-
default Directive getDirective(String directiveName) {
36+
default List<Directive> getDirective(String directiveName) {
3737
return getDirectivesByName().get(directiveName);
3838
}
3939
}

src/main/java/graphql/language/InlineFragment.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ public List<Directive> getDirectives() {
6868
return new ArrayList<>(directives);
6969
}
7070

71-
public Map<String, Directive> getDirectivesByName() {
71+
public Map<String, List<Directive>> getDirectivesByName() {
7272
return directivesByName(directives);
7373
}
7474

75-
public Directive getDirective(String directiveName) {
75+
public List<Directive> getDirective(String directiveName) {
7676
return getDirectivesByName().get(directiveName);
7777
}
7878

src/main/java/graphql/language/NodeUtil.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ public static boolean isEqualTo(String thisStr, String thatStr) {
2929
}
3030

3131

32-
public static Map<String, Directive> directivesByName(List<Directive> directives) {
33-
return FpKit.getByName(directives, Directive::getName, mergeFirst());
32+
public static Map<String, List<Directive>> directivesByName(List<Directive> directives) {
33+
return FpKit.groupingBy(directives, Directive::getName);
3434
}
3535

3636
public static Map<String, Argument> argumentsByName(List<Argument> arguments) {

src/main/java/graphql/language/SchemaDefinition.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ public List<Directive> getDirectives() {
4141
return new ArrayList<>(directives);
4242
}
4343

44-
public Map<String, Directive> getDirectivesByName() {
44+
public Map<String, List<Directive>> getDirectivesByName() {
4545
return directivesByName(directives);
4646
}
4747

48-
public Directive getDirective(String directiveName) {
48+
public List<Directive> getDirective(String directiveName) {
4949
return getDirectivesByName().get(directiveName);
5050
}
5151

src/main/java/graphql/schema/GraphQLDirective.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public class GraphQLDirective implements GraphQLNamedSchemaElement {
3131

3232
private final String name;
3333
private final String description;
34+
private final boolean isRepeatable;
3435
private final EnumSet<DirectiveLocation> locations;
3536
private final List<GraphQLArgument> arguments = new ArrayList<>();
3637
private final DirectiveDefinition definition;
@@ -44,20 +45,23 @@ public class GraphQLDirective implements GraphQLNamedSchemaElement {
4445
@Deprecated
4546
public GraphQLDirective(String name,
4647
String description,
48+
boolean isRepeatable,
4749
EnumSet<DirectiveLocation> locations,
4850
List<GraphQLArgument> arguments) {
49-
this(name, description, locations, arguments, null);
51+
this(name, description, isRepeatable, locations, arguments, null);
5052
}
5153

5254
private GraphQLDirective(String name,
5355
String description,
56+
boolean isRepeatable,
5457
EnumSet<DirectiveLocation> locations,
5558
List<GraphQLArgument> arguments,
5659
DirectiveDefinition definition) {
5760
assertValidName(name);
5861
assertNotNull(arguments, () -> "arguments can't be null");
5962
this.name = name;
6063
this.description = description;
64+
this.isRepeatable = isRepeatable;
6165
this.locations = locations;
6266
this.arguments.addAll(arguments);
6367
this.definition = definition;
@@ -89,6 +93,10 @@ public String getDescription() {
8993
return description;
9094
}
9195

96+
public boolean isRepeatable() {
97+
return isRepeatable;
98+
}
99+
92100
public DirectiveDefinition getDefinition() {
93101
return definition;
94102
}
@@ -97,6 +105,7 @@ public DirectiveDefinition getDefinition() {
97105
public String toString() {
98106
return "GraphQLDirective{" +
99107
"name='" + name + '\'' +
108+
", isRepeatable=" + isRepeatable +
100109
", arguments=" + arguments +
101110
", locations=" + locations +
102111
'}';
@@ -150,6 +159,7 @@ public static Builder newDirective(GraphQLDirective existing) {
150159

151160
public static class Builder extends GraphqlTypeBuilder {
152161

162+
private boolean isRepeatable;
153163
private EnumSet<DirectiveLocation> locations = EnumSet.noneOf(DirectiveLocation.class);
154164
private final Map<String, GraphQLArgument> arguments = new LinkedHashMap<>();
155165
private DirectiveDefinition definition;
@@ -161,6 +171,7 @@ public Builder() {
161171
public Builder(GraphQLDirective existing) {
162172
this.name = existing.getName();
163173
this.description = existing.getDescription();
174+
this.isRepeatable = existing.isRepeatable();
164175
this.locations = existing.validLocations();
165176
this.arguments.putAll(getByName(existing.getArguments(), GraphQLArgument::getName));
166177
}
@@ -183,6 +194,11 @@ public Builder comparatorRegistry(GraphqlTypeComparatorRegistry comparatorRegist
183194
return this;
184195
}
185196

197+
public Builder isRepeatable(boolean isRepeatable) {
198+
this.isRepeatable = isRepeatable;
199+
return this;
200+
}
201+
186202
public Builder validLocations(DirectiveLocation... validLocations) {
187203
Collections.addAll(locations, validLocations);
188204
return this;
@@ -264,11 +280,12 @@ public GraphQLDirective build() {
264280
return new GraphQLDirective(
265281
name,
266282
description,
283+
isRepeatable,
267284
locations,
268285
sort(arguments, GraphQLDirective.class, GraphQLArgument.class),
269286
definition);
270287
}
271288

272289

273290
}
274-
}
291+
}

src/main/java/graphql/schema/idl/SchemaGeneratorHelper.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ public GraphQLDirective buildDirective(Directive directive, Set<GraphQLDirective
172172
GraphQLDirective directiveDefinition = FpKit.findOne(directiveDefinitions, dd -> dd.getName().equals(directive.getName())).get();
173173
GraphQLDirective.Builder builder = GraphQLDirective.newDirective()
174174
.name(directive.getName())
175+
.isRepeatable(directiveDefinition.isRepeatable())
175176
.description(buildDescription(directive, null))
176177
.comparatorRegistry(comparatorRegistry)
177178
.validLocations(directiveLocation);
@@ -229,6 +230,7 @@ public GraphQLDirective buildDirectiveFromDefinition(DirectiveDefinition directi
229230

230231
GraphQLDirective.Builder builder = GraphQLDirective.newDirective()
231232
.name(directiveDefinition.getName())
233+
.isRepeatable(directiveDefinition.isRepeatable())
232234
.definition(directiveDefinition)
233235
.description(buildDescription(directiveDefinition, directiveDefinition.getDescription()));
234236

0 commit comments

Comments
 (0)