graphql.execution.ConditionalNodes Maven / Gradle / Ivy
package graphql.execution;
import graphql.Assert;
import graphql.GraphQLContext;
import graphql.Internal;
import graphql.language.Directive;
import graphql.language.NodeUtil;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import static graphql.Directives.IncludeDirective;
import static graphql.Directives.SkipDirective;
@Internal
public class ConditionalNodes {
public boolean shouldInclude(Map variables, List directives) {
// shortcut on no directives
if (directives.isEmpty()) {
return true;
}
boolean skip = getDirectiveResult(variables, directives, SkipDirective.getName(), false);
if (skip) {
return false;
}
return getDirectiveResult(variables, directives, IncludeDirective.getName(), true);
}
private boolean getDirectiveResult(Map variables, List directives, String directiveName, boolean defaultValue) {
Directive foundDirective = NodeUtil.findNodeByName(directives, directiveName);
if (foundDirective != null) {
Map argumentValues = ValuesResolver.getArgumentValues(SkipDirective.getArguments(), foundDirective.getArguments(), CoercedVariables.of(variables), GraphQLContext.getDefault(), Locale.getDefault());
Object flag = argumentValues.get("if");
Assert.assertTrue(flag instanceof Boolean, () -> String.format("The '%s' directive MUST have a value for the 'if' argument", directiveName));
return (Boolean) flag;
}
return defaultValue;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy