|
| 1 | +package graphql.schema.validation; |
| 2 | + |
| 3 | +import graphql.Internal; |
| 4 | +import graphql.schema.GraphQLArgument; |
| 5 | +import graphql.schema.GraphQLFieldDefinition; |
| 6 | +import graphql.schema.GraphQLInputObjectField; |
| 7 | +import graphql.schema.GraphQLInputType; |
| 8 | +import graphql.schema.GraphQLModifiedType; |
| 9 | +import graphql.schema.GraphQLOutputType; |
| 10 | +import graphql.schema.GraphQLSchemaElement; |
| 11 | +import graphql.schema.GraphQLType; |
| 12 | +import graphql.schema.GraphQLTypeUtil; |
| 13 | +import graphql.schema.GraphQLTypeVisitorStub; |
| 14 | +import graphql.util.TraversalControl; |
| 15 | +import graphql.util.TraverserContext; |
| 16 | + |
| 17 | +import java.util.function.BiFunction; |
| 18 | +import java.util.function.Predicate; |
| 19 | + |
| 20 | +/** |
| 21 | + * Schema validation rule ensuring no input type forms an unbroken non-nullable recursion, |
| 22 | + * as such a type would be impossible to satisfy |
| 23 | + */ |
| 24 | +@Internal |
| 25 | +public class InputAndOutputTypesUsedAppropriately extends GraphQLTypeVisitorStub { |
| 26 | + |
| 27 | + @Override |
| 28 | + public TraversalControl visitGraphQLFieldDefinition(GraphQLFieldDefinition fieldDef, TraverserContext<GraphQLSchemaElement> context) { |
| 29 | + String typeName = getTypeName((GraphQLType) context.getParentNode()); |
| 30 | + String fieldName = typeName + "." + fieldDef.getName(); |
| 31 | + SchemaValidationErrorCollector validationErrorCollector = context.getVarFromParents(SchemaValidationErrorCollector.class); |
| 32 | + for (GraphQLArgument argument : fieldDef.getArguments()) { |
| 33 | + String argName = fieldName + "." + argument.getName(); |
| 34 | + GraphQLInputType argumentType = argument.getType(); |
| 35 | + checkIsAllInputTypes(argumentType, validationErrorCollector, argName); |
| 36 | + } |
| 37 | + checkIsAllOutputTypes(fieldDef.getType(), validationErrorCollector, fieldName); |
| 38 | + return TraversalControl.CONTINUE; |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public TraversalControl visitGraphQLInputObjectField(GraphQLInputObjectField fieldDef, TraverserContext<GraphQLSchemaElement> context) { |
| 43 | + String typeName = getTypeName((GraphQLType) context.getParentNode()); |
| 44 | + String fieldName = typeName + "." + fieldDef.getName(); |
| 45 | + SchemaValidationErrorCollector validationErrorCollector = context.getVarFromParents(SchemaValidationErrorCollector.class); |
| 46 | + checkIsAllInputTypes(fieldDef.getType(), validationErrorCollector, fieldName); |
| 47 | + return TraversalControl.CONTINUE; |
| 48 | + } |
| 49 | + |
| 50 | + private void checkIsAllInputTypes(GraphQLInputType inputType, |
| 51 | + SchemaValidationErrorCollector validationErrorCollector, |
| 52 | + String argName) { |
| 53 | + checkTypeContext(inputType, validationErrorCollector, argName, |
| 54 | + typeToCheck -> typeToCheck instanceof GraphQLInputType, |
| 55 | + (typeToCheck, path) -> new SchemaValidationError(SchemaValidationErrorType.OutputTypeUsedInInputTypeContext, |
| 56 | + String.format("The output type '%s' has been used in an input type context : '%s'", typeToCheck, path))); |
| 57 | + } |
| 58 | + |
| 59 | + private void checkIsAllOutputTypes(GraphQLOutputType outputType, |
| 60 | + SchemaValidationErrorCollector validationErrorCollector, |
| 61 | + String fieldName) { |
| 62 | + checkTypeContext(outputType, validationErrorCollector, fieldName, |
| 63 | + typeToCheck -> typeToCheck instanceof GraphQLOutputType, |
| 64 | + (typeToCheck, path) -> new SchemaValidationError(SchemaValidationErrorType.InputTypeUsedInOutputTypeContext, |
| 65 | + String.format("The input type '%s' has been used in a output type context : '%s'", typeToCheck, path))); |
| 66 | + } |
| 67 | + |
| 68 | + private void checkTypeContext(GraphQLType type, |
| 69 | + SchemaValidationErrorCollector validationErrorCollector, |
| 70 | + String path, |
| 71 | + Predicate<GraphQLType> typePredicate, |
| 72 | + BiFunction<String, String, SchemaValidationError> errorMaker) { |
| 73 | + while (true) { |
| 74 | + String typeName = getTypeName(type); |
| 75 | + boolean isOk = typePredicate.test(type); |
| 76 | + if (!isOk) { |
| 77 | + validationErrorCollector.addError(errorMaker.apply(typeName, path)); |
| 78 | + } |
| 79 | + if (type instanceof GraphQLModifiedType) { |
| 80 | + type = ((GraphQLModifiedType) type).getWrappedType(); |
| 81 | + } else { |
| 82 | + return; |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private String getTypeName(GraphQLType type) { |
| 88 | + return GraphQLTypeUtil.simplePrint(type); |
| 89 | + } |
| 90 | +} |
0 commit comments