graphql.execution.ConditionalNodes Maven / Gradle / Ivy
package graphql.execution;
import graphql.language.Directive;
import java.util.List;
import java.util.Map;
import static graphql.Directives.IncludeDirective;
import static graphql.Directives.SkipDirective;
import static graphql.language.NodeUtil.directivesByName;
public class ConditionalNodes {
private final ValuesResolver valuesResolver;
public ConditionalNodes() {
valuesResolver = new ValuesResolver();
}
public boolean
shouldInclude(Map variables, List directives) {
boolean skip = getDirectiveResult(variables, directives, SkipDirective.getName(), false);
boolean include = getDirectiveResult(variables, directives, IncludeDirective.getName(), true);
return !skip && include;
}
private Directive getDirectiveByName(List directives, String name) {
if (directives.isEmpty()) {
return null;
}
return directivesByName(directives).get(name);
}
private boolean getDirectiveResult(Map variables, List directives, String directiveName, boolean defaultValue) {
Directive directive = getDirectiveByName(directives, directiveName);
if (directive != null) {
Map argumentValues = valuesResolver.getArgumentValues(SkipDirective.getArguments(), directive.getArguments(), variables);
return (Boolean) argumentValues.get("if");
}
return defaultValue;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy