use of graphql.schema.GraphQLDirectiveContainer in project graphql-java by graphql-java.
the class IntrospectionWithDirectivesSupport method addAppliedDirectives.
private GraphQLObjectType addAppliedDirectives(GraphQLObjectType originalType, GraphQLCodeRegistry.Builder codeRegistry, GraphQLObjectType appliedDirectiveType, GraphQLObjectType directiveArgumentType) {
GraphQLObjectType objectType = originalType.transform(bld -> bld.field(fld -> fld.name("appliedDirectives").type(nonNull(list(nonNull(appliedDirectiveType))))));
DataFetcher<?> df = env -> {
Object source = env.getSource();
GraphQLSchema schema = env.getGraphQLSchema();
if (source instanceof GraphQLDirectiveContainer) {
GraphQLDirectiveContainer type = env.getSource();
List<GraphQLAppliedDirective> appliedDirectives = DirectivesUtil.toAppliedDirectives(type);
return filterAppliedDirectives(schema, false, type, appliedDirectives);
}
if (source instanceof GraphQLSchema) {
List<GraphQLAppliedDirective> appliedDirectives = DirectivesUtil.toAppliedDirectives(schema.getSchemaAppliedDirectives(), schema.getSchemaDirectives());
return filterAppliedDirectives(schema, true, null, appliedDirectives);
}
return assertShouldNeverHappen("What directive containing element have we not considered? - %s", originalType);
};
DataFetcher<?> argsDF = env -> {
final GraphQLAppliedDirective directive = env.getSource();
// we only show directive arguments that have values set on them
return directive.getArguments().stream().filter(arg -> arg.getArgumentValue().isSet());
};
DataFetcher<?> argValueDF = env -> {
final GraphQLAppliedDirectiveArgument argument = env.getSource();
InputValueWithState value = argument.getArgumentValue();
Node<?> literal = ValuesResolver.valueToLiteral(value, argument.getType());
return AstPrinter.printAst(literal);
};
codeRegistry.dataFetcher(coordinates(objectType, "appliedDirectives"), df);
codeRegistry.dataFetcher(coordinates(appliedDirectiveType, "args"), argsDF);
codeRegistry.dataFetcher(coordinates(directiveArgumentType, "value"), argValueDF);
return objectType;
}
use of graphql.schema.GraphQLDirectiveContainer in project graphql-java-extended-validation by graphql-java.
the class TargetedValidationRules method walkListArg.
private List<GraphQLError> walkListArg(ValidationRule rule, ValidationEnvironment validationEnvironment, GraphQLList argumentType, List<Object> objectList) {
List<GraphQLError> errors = new ArrayList<>();
GraphQLInputType listItemType = Util.unwrapOneAndAllNonNull(argumentType);
List<GraphQLDirective> directives;
if (!(listItemType instanceof GraphQLDirectiveContainer)) {
directives = Collections.emptyList();
} else {
directives = ((GraphQLDirectiveContainer) listItemType).getDirectives();
}
int ix = 0;
for (Object value : objectList) {
ResultPath newPath = validationEnvironment.getValidatedPath().segment(ix);
ValidationEnvironment newValidationEnvironment = validationEnvironment.transform(builder -> builder.validatedPath(newPath).validatedValue(value).validatedType(listItemType).directives(directives));
List<GraphQLError> ruleErrors = runValidationImpl(rule, newValidationEnvironment, listItemType, value);
errors.addAll(ruleErrors);
ix++;
}
return errors;
}
use of graphql.schema.GraphQLDirectiveContainer in project graphql-java by graphql-java.
the class AppliedDirectivesAreValid method visitGraphQLType.
@Override
protected TraversalControl visitGraphQLType(GraphQLSchemaElement node, TraverserContext<GraphQLSchemaElement> context) {
SchemaValidationErrorCollector collector = context.getVarFromParents(SchemaValidationErrorCollector.class);
GraphQLSchema schema = context.getVarFromParents(GraphQLSchema.class);
if (node instanceof GraphQLDirectiveContainer) {
GraphQLDirectiveContainer directiveContainer = (GraphQLDirectiveContainer) node;
for (Map.Entry<String, List<GraphQLDirective>> entry : directiveContainer.getAllDirectivesByName().entrySet()) {
String directiveName = entry.getKey();
GraphQLDirective directiveDef = schema.getDirective(directiveName);
if (directiveDef != null) {
checkNonRepeatable(collector, directiveContainer, directiveDef, entry.getValue());
} else {
addError(collector, format("A definition for directive '%s' could not be found", directiveName));
}
}
}
return TraversalControl.CONTINUE;
}
use of graphql.schema.GraphQLDirectiveContainer in project graphql-java-extended-validation by graphql-java.
the class DirectivesAndTypeWalker method walkInputType.
private boolean walkInputType(GraphQLInputType inputType, List<GraphQLDirective> directives, BiFunction<GraphQLInputType, GraphQLDirective, Boolean> isSuitable) {
String typeName = GraphQLTypeUtil.unwrapAll(inputType).getName();
GraphQLInputType unwrappedInputType = Util.unwrapNonNull(inputType);
for (GraphQLDirective directive : directives) {
if (isSuitable.apply(unwrappedInputType, directive)) {
return seen(typeName, true);
}
}
if (unwrappedInputType instanceof GraphQLInputObjectType) {
GraphQLInputObjectType inputObjType = (GraphQLInputObjectType) unwrappedInputType;
if (seenTypes.containsKey(typeName)) {
return seenTypes.get(typeName);
}
seen(typeName, false);
for (GraphQLInputObjectField inputField : inputObjType.getFieldDefinitions()) {
inputType = inputField.getType();
directives = inputField.getDirectives();
if (walkInputType(inputType, directives, isSuitable)) {
return seen(typeName, true);
}
}
}
if (unwrappedInputType instanceof GraphQLList) {
GraphQLInputType innerListType = Util.unwrapOneAndAllNonNull(unwrappedInputType);
if (innerListType instanceof GraphQLDirectiveContainer) {
directives = ((GraphQLDirectiveContainer) innerListType).getDirectives();
if (walkInputType(innerListType, directives, isSuitable)) {
return seen(typeName, true);
}
}
}
return seen(typeName, false);
}
Aggregations