|
| 1 | +package graphql.schema.validation; |
| 2 | + |
| 3 | +import graphql.Internal; |
| 4 | +import graphql.schema.GraphQLArgument; |
| 5 | +import graphql.schema.GraphQLFieldDefinition; |
| 6 | +import graphql.schema.GraphQLFieldsContainer; |
| 7 | +import graphql.schema.GraphQLInputObjectField; |
| 8 | +import graphql.schema.GraphQLInputObjectType; |
| 9 | +import graphql.schema.GraphQLInputType; |
| 10 | +import graphql.schema.GraphQLModifiedType; |
| 11 | +import graphql.schema.GraphQLOutputType; |
| 12 | +import graphql.schema.GraphQLType; |
| 13 | +import org.jspecify.annotations.NullMarked; |
| 14 | + |
| 15 | +import static graphql.schema.GraphQLTypeUtil.simplePrint; |
| 16 | +import static java.lang.String.format; |
| 17 | + |
| 18 | +/** |
| 19 | + * Schema validation rule ensuring output types are only used in output positions and input types only in input positions. |
| 20 | + */ |
| 21 | +@Internal |
| 22 | +@NullMarked |
| 23 | +public class InputAndOutputTypesUsedAppropriately implements SchemaValidationRule { |
| 24 | + |
| 25 | + private final SchemaValidationErrorCollector errors; |
| 26 | + |
| 27 | + public InputAndOutputTypesUsedAppropriately(SchemaValidationErrorCollector errors) { |
| 28 | + this.errors = errors; |
| 29 | + } |
| 30 | + |
| 31 | + @Override |
| 32 | + public void checkFieldDefinition(GraphQLFieldsContainer container, GraphQLFieldDefinition fieldDefinition) { |
| 33 | + for (GraphQLArgument argument : fieldDefinition.getArguments()) { |
| 34 | + if (hasMisusedType(argument.getType(), true)) { |
| 35 | + String path = container.getName() + "." + fieldDefinition.getName() + "." + argument.getName(); |
| 36 | + addTypeContextErrors(argument.getType(), true, path); |
| 37 | + } |
| 38 | + } |
| 39 | + if (hasMisusedType(fieldDefinition.getType(), false)) { |
| 40 | + String path = container.getName() + "." + fieldDefinition.getName(); |
| 41 | + addTypeContextErrors(fieldDefinition.getType(), false, path); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public void checkInputObjectField(GraphQLInputObjectType type, GraphQLInputObjectField inputObjectField) { |
| 47 | + if (hasMisusedType(inputObjectField.getType(), true)) { |
| 48 | + String path = type.getName() + "." + inputObjectField.getName(); |
| 49 | + addTypeContextErrors(inputObjectField.getType(), true, path); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + private boolean hasMisusedType(GraphQLType type, boolean inputContext) { |
| 54 | + GraphQLType current = type; |
| 55 | + while (true) { |
| 56 | + if (isMisused(current, inputContext)) { |
| 57 | + return true; |
| 58 | + } |
| 59 | + if (!(current instanceof GraphQLModifiedType)) { |
| 60 | + return false; |
| 61 | + } |
| 62 | + current = ((GraphQLModifiedType) current).getWrappedType(); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private void addTypeContextErrors(GraphQLType type, boolean inputContext, String path) { |
| 67 | + GraphQLType current = type; |
| 68 | + while (true) { |
| 69 | + if (isMisused(current, inputContext)) { |
| 70 | + addTypeContextError(current, inputContext, path); |
| 71 | + } |
| 72 | + if (!(current instanceof GraphQLModifiedType)) { |
| 73 | + return; |
| 74 | + } |
| 75 | + current = ((GraphQLModifiedType) current).getWrappedType(); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + private void addTypeContextError(GraphQLType misusedType, boolean inputContext, String path) { |
| 80 | + if (inputContext) { |
| 81 | + addError(SchemaValidationErrorType.OutputTypeUsedInInputTypeContext, |
| 82 | + format("The output type '%s' has been used in an input type context : '%s'", simplePrint(misusedType), path)); |
| 83 | + } else { |
| 84 | + addError(SchemaValidationErrorType.InputTypeUsedInOutputTypeContext, |
| 85 | + format("The input type '%s' has been used in a output type context : '%s'", simplePrint(misusedType), path)); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + private boolean isMisused(GraphQLType type, boolean inputContext) { |
| 90 | + return inputContext ? !(type instanceof GraphQLInputType) : !(type instanceof GraphQLOutputType); |
| 91 | + } |
| 92 | + |
| 93 | + private void addError(SchemaValidationErrorType errorType, String message) { |
| 94 | + errors.addError(new SchemaValidationError(errorType, message)); |
| 95 | + } |
| 96 | +} |
0 commit comments