Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 3 additions & 44 deletions src/main/java/graphql/DirectivesUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import graphql.language.Directive;
import graphql.language.DirectiveDefinition;
import graphql.schema.GraphQLArgument;
import graphql.schema.GraphQLDirective;
import graphql.util.FpKit;
Expand All @@ -16,7 +14,6 @@

import static graphql.Assert.assertNotNull;
import static graphql.collect.ImmutableKit.emptyList;
import static java.util.stream.Collectors.toList;

@Internal
public class DirectivesUtil {
Expand All @@ -35,15 +32,6 @@ public static Map<String, ImmutableList<GraphQLDirective>> allDirectivesByName(L
return ImmutableMap.copyOf(FpKit.groupingBy(directives, GraphQLDirective::getName));
}

public static GraphQLDirective nonRepeatedDirectiveByNameWithAssert(Map<String, List<GraphQLDirective>> directives, String directiveName) {
List<GraphQLDirective> directiveList = directives.get(directiveName);
if (directiveList == null || directiveList.isEmpty()) {
return null;
}
Assert.assertTrue(isAllNonRepeatable(directiveList), () -> String.format("'%s' is a repeatable directive and you have used a non repeatable access method", directiveName));
return directiveList.get(0);
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None of this applies any more - it will be done at Schema validation time when we have everything

public static Optional<GraphQLArgument> directiveWithArg(List<GraphQLDirective> directives, String directiveName, String argumentName) {
GraphQLDirective directive = nonRepeatableDirectivesByName(directives).get(directiveName);
GraphQLArgument argument = null;
Expand All @@ -66,40 +54,20 @@ public static boolean isAllNonRepeatable(List<GraphQLDirective> directives) {
return true;
}

public static List<GraphQLDirective> enforceAdd(List<GraphQLDirective> targetList, GraphQLDirective newDirective) {
public static List<GraphQLDirective> add(List<GraphQLDirective> targetList, GraphQLDirective newDirective) {
assertNotNull(targetList, () -> "directive list can't be null");
assertNotNull(newDirective, () -> "directive can't be null");

// check whether the newDirective is repeatable in advance, to avoid needless operations
if (newDirective.isNonRepeatable()) {
Map<String, ImmutableList<GraphQLDirective>> map = allDirectivesByName(targetList);
assertNonRepeatable(newDirective, map);
}
targetList.add(newDirective);
return targetList;
}

public static List<GraphQLDirective> enforceAddAll(List<GraphQLDirective> targetList, List<GraphQLDirective> newDirectives) {
public static List<GraphQLDirective> addAll(List<GraphQLDirective> targetList, List<GraphQLDirective> newDirectives) {
assertNotNull(targetList, () -> "directive list can't be null");
assertNotNull(newDirectives, () -> "directive list can't be null");
Map<String, ImmutableList<GraphQLDirective>> map = allDirectivesByName(targetList);
for (GraphQLDirective newDirective : newDirectives) {
assertNonRepeatable(newDirective, map);
targetList.add(newDirective);
}
targetList.addAll(newDirectives);
return targetList;
}

private static void assertNonRepeatable(GraphQLDirective directive, Map<String, ImmutableList<GraphQLDirective>> mapOfDirectives) {
if (directive.isNonRepeatable()) {
List<GraphQLDirective> currentDirectives = mapOfDirectives.getOrDefault(directive.getName(), emptyList());
int currentSize = currentDirectives.size();
if (currentSize > 0) {
Assert.assertShouldNeverHappen("%s is a non repeatable directive but there is already one present in this list", directive.getName());
}
}
}

public static GraphQLDirective getFirstDirective(String name, Map<String, List<GraphQLDirective>> allDirectivesByName) {
List<GraphQLDirective> directives = allDirectivesByName.getOrDefault(name, emptyList());
if (directives.isEmpty()) {
Expand Down Expand Up @@ -143,7 +111,6 @@ public GraphQLDirective getDirective(String directiveName) {
if (directiveList == null || directiveList.isEmpty()) {
return null;
}
Assert.assertTrue(isAllNonRepeatable(directiveList), () -> String.format("'%s' is a repeatable directive and you have used a non repeatable access method", directiveName));
return directiveList.get(0);

}
Expand All @@ -152,12 +119,4 @@ public List<GraphQLDirective> getDirectives(String directiveName) {
return allDirectivesByName.getOrDefault(directiveName, emptyList());
}
}

public static List<Directive> nonRepeatableDirectivesOnly(Map<String, DirectiveDefinition> directiveDefinitionMap, List<Directive> directives) {
return directives.stream().filter(directive -> {
String directiveName = directive.getName();
DirectiveDefinition directiveDefinition = directiveDefinitionMap.get(directiveName);
return directiveDefinition == null || !directiveDefinition.isRepeatable();
}).collect(toList());
}
}
6 changes: 3 additions & 3 deletions src/main/java/graphql/schema/GraphQLArgument.java
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ public Builder(GraphQLArgument existing) {
this.description = existing.getDescription();
this.definition = existing.getDefinition();
this.deprecationReason = existing.deprecationReason;
DirectivesUtil.enforceAddAll(this.directives, existing.getDirectives());
DirectivesUtil.addAll(this.directives, existing.getDirectives());
}

@Override
Expand Down Expand Up @@ -439,14 +439,14 @@ public Builder withDirectives(GraphQLDirective... directives) {

public Builder withDirective(GraphQLDirective directive) {
assertNotNull(directive, () -> "directive can't be null");
DirectivesUtil.enforceAdd(this.directives, directive);
DirectivesUtil.add(this.directives, directive);
return this;
}

public Builder replaceDirectives(List<GraphQLDirective> directives) {
assertNotNull(directives, () -> "directive can't be null");
this.directives.clear();
DirectivesUtil.enforceAddAll(this.directives, directives);
DirectivesUtil.addAll(this.directives, directives);
return this;
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/graphql/schema/GraphQLEnumType.java
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ public Builder(GraphQLEnumType existing) {
this.definition = existing.getDefinition();
this.extensionDefinitions = existing.getExtensionDefinitions();
this.values.putAll(getByName(existing.getValues(), GraphQLEnumValueDefinition::getName));
DirectivesUtil.enforceAddAll(this.directives, existing.getDirectives());
DirectivesUtil.addAll(this.directives, existing.getDirectives());
}

@Override
Expand Down Expand Up @@ -385,14 +385,14 @@ public Builder withDirectives(GraphQLDirective... directives) {

public Builder withDirective(GraphQLDirective directive) {
assertNotNull(directive, () -> "directive can't be null");
DirectivesUtil.enforceAdd(this.directives, directive);
DirectivesUtil.add(this.directives, directive);
return this;
}

public Builder replaceDirectives(List<GraphQLDirective> directives) {
assertNotNull(directives, () -> "directive can't be null");
this.directives.clear();
DirectivesUtil.enforceAddAll(this.directives, directives);
DirectivesUtil.addAll(this.directives, directives);
return this;
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/graphql/schema/GraphQLEnumValueDefinition.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public Builder(GraphQLEnumValueDefinition existing) {
this.description = existing.getDescription();
this.value = existing.getValue();
this.deprecationReason = existing.getDeprecationReason();
DirectivesUtil.enforceAddAll(this.directives, existing.getDirectives());
DirectivesUtil.addAll(this.directives, existing.getDirectives());
}

@Override
Expand Down Expand Up @@ -231,14 +231,14 @@ public Builder withDirectives(GraphQLDirective... directives) {

public Builder withDirective(GraphQLDirective directive) {
assertNotNull(directive, () -> "directive can't be null");
DirectivesUtil.enforceAdd(this.directives, directive);
DirectivesUtil.add(this.directives, directive);
return this;
}

public Builder replaceDirectives(List<GraphQLDirective> directives) {
assertNotNull(directives, () -> "directive can't be null");
this.directives.clear();
DirectivesUtil.enforceAddAll(this.directives, directives);
DirectivesUtil.addAll(this.directives, directives);
return this;
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/graphql/schema/GraphQLFieldDefinition.java
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public Builder(GraphQLFieldDefinition existing) {
this.deprecationReason = existing.getDeprecationReason();
this.definition = existing.getDefinition();
this.arguments.putAll(getByName(existing.getArguments(), GraphQLArgument::getName));
DirectivesUtil.enforceAddAll(this.directives, existing.getDirectives());
DirectivesUtil.addAll(this.directives, existing.getDirectives());
}


Expand Down Expand Up @@ -442,14 +442,14 @@ public Builder withDirectives(GraphQLDirective... directives) {

public Builder withDirective(GraphQLDirective directive) {
assertNotNull(directive, () -> "directive can't be null");
DirectivesUtil.enforceAdd(this.directives, directive);
DirectivesUtil.add(this.directives, directive);
return this;
}

public Builder replaceDirectives(List<GraphQLDirective> directives) {
assertNotNull(directives, () -> "directive can't be null");
this.directives.clear();
DirectivesUtil.enforceAddAll(this.directives, directives);
DirectivesUtil.addAll(this.directives, directives);
return this;
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/graphql/schema/GraphQLInputObjectField.java
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ public Builder(GraphQLInputObjectField existing) {
this.type = existing.originalType;
this.definition = existing.getDefinition();
this.deprecationReason = existing.deprecationReason;
DirectivesUtil.enforceAddAll(this.directives, existing.getDirectives());
DirectivesUtil.addAll(this.directives, existing.getDirectives());
}

@Override
Expand Down Expand Up @@ -344,14 +344,14 @@ public Builder withDirectives(GraphQLDirective... directives) {

public Builder withDirective(GraphQLDirective directive) {
assertNotNull(directive, () -> "directive can't be null");
DirectivesUtil.enforceAdd(this.directives, directive);
DirectivesUtil.add(this.directives, directive);
return this;
}

public Builder replaceDirectives(List<GraphQLDirective> directives) {
assertNotNull(directives, () -> "directive can't be null");
this.directives.clear();
DirectivesUtil.enforceAddAll(this.directives, directives);
DirectivesUtil.addAll(this.directives, directives);
return this;
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/graphql/schema/GraphQLInputObjectType.java
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public Builder(GraphQLInputObjectType existing) {
this.definition = existing.getDefinition();
this.extensionDefinitions = existing.getExtensionDefinitions();
this.fields.putAll(getByName(existing.getFields(), GraphQLInputObjectField::getName));
DirectivesUtil.enforceAddAll(this.directives, existing.getDirectives());
DirectivesUtil.addAll(this.directives, existing.getDirectives());
}

@Override
Expand Down Expand Up @@ -321,14 +321,14 @@ public Builder withDirectives(GraphQLDirective... directives) {

public Builder withDirective(GraphQLDirective directive) {
assertNotNull(directive, () -> "directive can't be null");
DirectivesUtil.enforceAdd(this.directives, directive);
DirectivesUtil.add(this.directives, directive);
return this;
}

public Builder replaceDirectives(List<GraphQLDirective> directives) {
assertNotNull(directives, () -> "directive can't be null");
this.directives.clear();
DirectivesUtil.enforceAddAll(this.directives, directives);
DirectivesUtil.addAll(this.directives, directives);
return this;
}

Expand Down
7 changes: 3 additions & 4 deletions src/main/java/graphql/schema/GraphQLInterfaceType.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import static graphql.Assert.assertNotNull;
import static graphql.Assert.assertValidName;
import static graphql.schema.GraphqlTypeComparators.asIsOrder;
import static graphql.schema.GraphqlTypeComparators.sortTypes;
import static graphql.util.FpKit.getByName;
import static graphql.util.FpKit.valuesToList;
Expand Down Expand Up @@ -262,7 +261,7 @@ public Builder(GraphQLInterfaceType existing) {
this.extensionDefinitions = existing.getExtensionDefinitions();
this.fields.putAll(getByName(existing.getFieldDefinitions(), GraphQLFieldDefinition::getName));
this.interfaces.putAll(getByName(existing.originalInterfaces, GraphQLNamedType::getName));
DirectivesUtil.enforceAddAll(this.directives,existing.getDirectives());
DirectivesUtil.addAll(this.directives,existing.getDirectives());
}

@Override
Expand Down Expand Up @@ -376,14 +375,14 @@ public Builder withDirectives(GraphQLDirective... directives) {

public Builder withDirective(GraphQLDirective directive) {
assertNotNull(directive, () -> "directive can't be null");
DirectivesUtil.enforceAdd(this.directives, directive);
DirectivesUtil.add(this.directives, directive);
return this;
}

public Builder replaceDirectives(List<GraphQLDirective> directives) {
assertNotNull(directives, () -> "directive can't be null");
this.directives.clear();
DirectivesUtil.enforceAddAll(this.directives, directives);
DirectivesUtil.addAll(this.directives, directives);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename of internal method

return this;
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/graphql/schema/GraphQLObjectType.java
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public Builder(GraphQLObjectType existing) {
extensionDefinitions = existing.getExtensionDefinitions();
fields.putAll(getByName(existing.getFieldDefinitions(), GraphQLFieldDefinition::getName));
interfaces.putAll(getByName(existing.originalInterfaces, GraphQLNamedType::getName));
DirectivesUtil.enforceAddAll(this.directives, existing.getDirectives());
DirectivesUtil.addAll(this.directives, existing.getDirectives());
}

@Override
Expand Down Expand Up @@ -397,7 +397,7 @@ public Builder clearInterfaces() {
public Builder replaceDirectives(List<GraphQLDirective> directives) {
assertNotNull(directives, () -> "directive can't be null");
this.directives.clear();
DirectivesUtil.enforceAddAll(this.directives, directives);
DirectivesUtil.addAll(this.directives, directives);
return this;
}

Expand All @@ -412,7 +412,7 @@ public Builder withDirectives(GraphQLDirective... directives) {

public Builder withDirective(GraphQLDirective directive) {
assertNotNull(directive, () -> "directive can't be null");
DirectivesUtil.enforceAdd(this.directives, directive);
DirectivesUtil.add(this.directives, directive);
return this;
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/graphql/schema/GraphQLScalarType.java
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public Builder(GraphQLScalarType existing) {
definition = existing.getDefinition();
extensionDefinitions = existing.getExtensionDefinitions();
specifiedByUrl = existing.getSpecifiedByUrl();
DirectivesUtil.enforceAddAll(this.directives, existing.getDirectives());
DirectivesUtil.addAll(this.directives, existing.getDirectives());
}

@Override
Expand Down Expand Up @@ -264,14 +264,14 @@ public Builder withDirectives(GraphQLDirective... directives) {

public Builder withDirective(GraphQLDirective directive) {
assertNotNull(directive, () -> "directive can't be null");
DirectivesUtil.enforceAdd(this.directives, directive);
DirectivesUtil.add(this.directives, directive);
return this;
}

public Builder replaceDirectives(List<GraphQLDirective> directives) {
assertNotNull(directives, () -> "directive can't be null");
this.directives.clear();
DirectivesUtil.enforceAddAll(this.directives, directives);
DirectivesUtil.addAll(this.directives, directives);
return this;
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/graphql/schema/GraphQLUnionType.java
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public Builder(GraphQLUnionType existing) {
this.definition = existing.getDefinition();
this.extensionDefinitions = existing.getExtensionDefinitions();
this.types.putAll(getByName(existing.originalTypes, GraphQLNamedType::getName));
DirectivesUtil.enforceAddAll(this.directives,existing.getDirectives());
DirectivesUtil.addAll(this.directives,existing.getDirectives());
}

@Override
Expand Down Expand Up @@ -331,13 +331,13 @@ public Builder withDirectives(GraphQLDirective... directives) {
public Builder replaceDirectives(List<GraphQLDirective> directives) {
assertNotNull(directives, () -> "directive can't be null");
this.directives.clear();
DirectivesUtil.enforceAddAll(this.directives, directives);
DirectivesUtil.addAll(this.directives, directives);
return this;
}

public Builder withDirective(GraphQLDirective directive) {
assertNotNull(directive, () -> "directive can't be null");
DirectivesUtil.enforceAdd(this.directives, directive);
DirectivesUtil.add(this.directives, directive);
return this;
}

Expand Down
7 changes: 1 addition & 6 deletions src/main/java/graphql/schema/idl/SchemaGeneratorHelper.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package graphql.schema.idl;

import graphql.Assert;
import graphql.AssertException;
import graphql.Internal;
import graphql.introspection.Introspection.DirectiveLocation;
Expand Down Expand Up @@ -257,10 +256,6 @@ String buildDeprecationReason(List<Directive> directives) {

private GraphQLDirective buildAppliedDirective(BuildContext buildCtx, Directive directive, DirectiveLocation directiveLocation, Set<GraphQLDirective> runtimeDirectives, GraphqlTypeComparatorRegistry comparatorRegistry, Set<String> previousNames) {
GraphQLDirective gqlDirective = buildAppliedDirective(buildCtx, directive, runtimeDirectives, directiveLocation, comparatorRegistry);
if (previousNames.contains(directive.getName())) {
// other parts of the code protect against duplicate non repeatable directives
Assert.assertTrue(gqlDirective.isRepeatable(), () -> String.format("The directive '%s' MUST be defined as a repeatable directive if its repeated on an SDL element", directive.getName()));
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No longer needed this will be checked later at schema build time

previousNames.add(gqlDirective.getName());
return gqlDirective;
}
Expand Down Expand Up @@ -302,7 +297,7 @@ private GraphQLArgument buildAppliedDArgument(BuildContext buildCtx, Argument ar
builder.name(arg.getName())
.type(inputType)
.definition(buildCtx.isCaptureAstDefinitions() ? directiveDefArgument.getDefinition() : null);

// we know it is a literal because it was created by SchemaGenerator
if (directiveDefArgument.getArgumentDefaultValue().isSet()) {
builder.defaultValueLiteral((Value) directiveDefArgument.getArgumentDefaultValue().getValue());
Expand Down
Loading