Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/main/java/graphql/schema/GraphQLSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

import graphql.Directives;
import graphql.schema.validation.InvalidSchemaException;
import graphql.schema.validation.ValidationError;
import graphql.schema.validation.Validator;
import graphql.schema.validation.SchemaValidationError;
import graphql.schema.validation.SchemaValidator;

import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -125,7 +125,7 @@ public GraphQLSchema build(Set<GraphQLType> additionalTypes) {
assertNotNull(additionalTypes, "additionalTypes can't be null");
GraphQLSchema graphQLSchema = new GraphQLSchema(queryType, mutationType, subscriptionType, additionalTypes);
new SchemaUtil().replaceTypeReferences(graphQLSchema);
Collection<ValidationError> errors = new Validator().validateSchema(graphQLSchema);
Collection<SchemaValidationError> errors = new SchemaValidator().validateSchema(graphQLSchema);
if (errors.size() > 0) {
throw new InvalidSchemaException(errors);
}
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/graphql/schema/GraphQLTypeUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package graphql.schema;

public class GraphQLTypeUtil {

/**
* This will get the unwrapped type name that includes the non null and list wrappers
* so it might be '[typeName!]'
*
* @param type the type in play
*
* @return the unwrapped type name
*/
public static String getUnwrappedTypeName(GraphQLType type) {
StringBuilder sb = new StringBuilder();
if (type instanceof GraphQLNonNull) {
sb.append(getUnwrappedTypeName(((GraphQLNonNull) type).getWrappedType()));
sb.append("!");
} else if (type instanceof GraphQLList) {
sb.append("[");
sb.append(getUnwrappedTypeName(((GraphQLList) type).getWrappedType()));
sb.append("]");
} else {
sb.append(type.getName());
}
return sb.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

public class InvalidSchemaException extends GraphQLException {

public InvalidSchemaException(Collection<ValidationError> errors) {
public InvalidSchemaException(Collection<SchemaValidationError> errors) {
super(buildErrorMsg(errors));
}

private static String buildErrorMsg(Collection<ValidationError> errors) {
private static String buildErrorMsg(Collection<SchemaValidationError> errors) {
StringBuilder message = new StringBuilder("invalid schema:");
for (ValidationError error : errors) {
for (SchemaValidationError error : errors) {
message.append("\n").append(error.getDescription());
}
return message.toString();
Expand Down
27 changes: 16 additions & 11 deletions src/main/java/graphql/schema/validation/NoUnbrokenInputCycles.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
package graphql.schema.validation;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import graphql.schema.GraphQLArgument;
import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLInputObjectField;
Expand All @@ -15,13 +10,23 @@
import graphql.schema.GraphQLNonNull;
import graphql.schema.GraphQLType;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* Schema validation rule ensuring no input type forms an unbroken non-nullable recursion,
* as such a type would be impossible to satisfy
*/
public class NoUnbrokenInputCycles implements ValidationRule {
public class NoUnbrokenInputCycles implements SchemaValidationRule {

public void check(GraphQLFieldDefinition fieldDef, ValidationErrorCollector validationErrorCollector) {
@Override
public void check(GraphQLType type, SchemaValidationErrorCollector validationErrorCollector) {
}

@Override
public void check(GraphQLFieldDefinition fieldDef, SchemaValidationErrorCollector validationErrorCollector) {
for (GraphQLArgument argument : fieldDef.getArguments()) {
GraphQLInputType argumentType = argument.getType();
if (argumentType instanceof GraphQLInputObjectType) {
Expand All @@ -32,9 +37,9 @@ public void check(GraphQLFieldDefinition fieldDef, ValidationErrorCollector vali
}
}

private void check(GraphQLInputObjectType type, Set<GraphQLType> seen, List<String> path, ValidationErrorCollector validationErrorCollector) {
private void check(GraphQLInputObjectType type, Set<GraphQLType> seen, List<String> path, SchemaValidationErrorCollector validationErrorCollector) {
if (seen.contains(type)) {
validationErrorCollector.addError(new ValidationError(ValidationErrorType.UnbrokenInputCycle, getErrorMessage(path)));
validationErrorCollector.addError(new SchemaValidationError(SchemaValidationErrorType.UnbrokenInputCycle, getErrorMessage(path)));
return;
}
seen.add(type);
Expand Down Expand Up @@ -70,11 +75,11 @@ private GraphQLType unwrap(GraphQLType type) {
}
return type;
}

private String getErrorMessage(List<String> path) {
StringBuilder message = new StringBuilder();
message.append("[");
for (int i = 0; i < path.size(); i++) {
for (int i = 0; i < path.size(); i++) {
if (i != 0) {
message.append(".");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package graphql.schema.validation;

import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLInterfaceType;
import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLOutputType;
import graphql.schema.GraphQLType;

import java.util.List;

import static graphql.schema.GraphQLTypeUtil.getUnwrappedTypeName;
import static graphql.schema.validation.SchemaValidationErrorType.ObjectDoesNotImplementItsInterfaces;
import static java.lang.String.format;

/**
* Schema validation rule ensuring object types have all the fields that they need to implement the interfaces
* they say they implement
*/
public class ObjectsImplementInterfaces implements SchemaValidationRule {

@Override
public void check(GraphQLFieldDefinition fieldDef, SchemaValidationErrorCollector validationErrorCollector) {
}

@Override
public void check(GraphQLType type, SchemaValidationErrorCollector validationErrorCollector) {
if (type instanceof GraphQLObjectType) {
check((GraphQLObjectType) type, validationErrorCollector);
}
}

// visible for testing
private void check(GraphQLObjectType objectTyoe, SchemaValidationErrorCollector validationErrorCollector) {
List<GraphQLOutputType> interfaces = objectTyoe.getInterfaces();
interfaces.forEach(interfaceType -> {
// we have resolved the interfaces at this point and hence the cast is ok
checkObjectImplementsInterface(objectTyoe, (GraphQLInterfaceType) interfaceType, validationErrorCollector);
});

}

private void checkObjectImplementsInterface(GraphQLObjectType objectTyoe, GraphQLInterfaceType interfaceType, SchemaValidationErrorCollector validationErrorCollector) {
List<GraphQLFieldDefinition> fieldDefinitions = interfaceType.getFieldDefinitions();
for (GraphQLFieldDefinition interfaceFieldDef : fieldDefinitions) {
GraphQLFieldDefinition objectFieldDef = objectTyoe.getFieldDefinition(interfaceFieldDef.getName());
if (objectFieldDef == null) {
validationErrorCollector.addError(
error(format("object type '%s' does not implement interface '%s' because field '%s' is missing",
objectTyoe.getName(), interfaceType.getName(), interfaceFieldDef.getName())));
} else {
checkFieldTypeEquivalence(objectTyoe, interfaceType, validationErrorCollector, interfaceFieldDef, objectFieldDef);
}
}
}

private void checkFieldTypeEquivalence(GraphQLObjectType objectTyoe, GraphQLInterfaceType interfaceType, SchemaValidationErrorCollector validationErrorCollector, GraphQLFieldDefinition interfaceFieldDef, GraphQLFieldDefinition objectFieldDef) {
// the reference implementation has a full but complicated abstract type equivalence check
// this is not that but we can add that later. It does cover the major cases however
String interfaceFieldDefStr = getUnwrappedTypeName(interfaceFieldDef.getType());
String objectFieldDefStr = getUnwrappedTypeName(objectFieldDef.getType());

if (!interfaceFieldDefStr.equals(objectFieldDefStr)) {
validationErrorCollector.addError(
error(format("object type '%s' does not implement interface '%s' because field '%s' is defined as '%s' type and not as '%s' type",
objectTyoe.getName(), interfaceType.getName(), interfaceFieldDef.getName(), objectFieldDefStr, interfaceFieldDefStr)));
}
}

private SchemaValidationError error(String msg) {
return new SchemaValidationError(ObjectDoesNotImplementItsInterfaces, msg);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

import static graphql.Assert.assertNotNull;

public class ValidationError {
public class SchemaValidationError {

private final ValidationErrorType errorType;
private final SchemaValidationErrorType errorType;
private final String description;

public ValidationError(ValidationErrorType errorType, String description) {
public SchemaValidationError(SchemaValidationErrorType errorType, String description) {
assertNotNull(errorType, "error type can not be null");
assertNotNull(description, "error description can not be null");
this.errorType = errorType;
this.description = description;
}

public ValidationErrorType getErrorType() {
public SchemaValidationErrorType getErrorType() {
return errorType;
}

Expand All @@ -32,10 +32,10 @@ public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof ValidationError)) {
if (!(other instanceof SchemaValidationError)) {
return false;
}
ValidationError that = (ValidationError) other;
SchemaValidationError that = (SchemaValidationError) other;
return this.errorType.equals(that.errorType) && this.description.equals(that.description);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package graphql.schema.validation;

import java.util.LinkedHashSet;
import java.util.Set;

public class SchemaValidationErrorCollector {

private final LinkedHashSet<SchemaValidationError> errors = new LinkedHashSet<>();

public void addError(SchemaValidationError validationError) {
this.errors.add(validationError);
}

public Set<SchemaValidationError> getErrors() {
return errors;
}

public boolean containsValidationError(SchemaValidationErrorType validationErrorType) {
for (SchemaValidationError validationError : errors) {
if (validationError.getErrorType() == validationErrorType) return true;
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package graphql.schema.validation;

public enum SchemaValidationErrorType {

UnbrokenInputCycle,
ObjectDoesNotImplementItsInterfaces
}
11 changes: 11 additions & 0 deletions src/main/java/graphql/schema/validation/SchemaValidationRule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package graphql.schema.validation;

import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLType;

public interface SchemaValidationRule {

void check(GraphQLFieldDefinition fieldDef, SchemaValidationErrorCollector validationErrorCollector);

void check(GraphQLType type, SchemaValidationErrorCollector validationErrorCollector);
}
Original file line number Diff line number Diff line change
@@ -1,39 +1,48 @@
package graphql.schema.validation;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLFieldsContainer;
import graphql.schema.GraphQLOutputType;
import graphql.schema.GraphQLSchema;
import graphql.schema.GraphQLType;

public class Validator {
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class SchemaValidator {

private final Set<GraphQLOutputType> processed = new HashSet<>();
public Set<ValidationError> validateSchema(GraphQLSchema schema) {
ValidationErrorCollector validationErrorCollector = new ValidationErrorCollector();
List<ValidationRule> rules = new ArrayList<>();

public Set<SchemaValidationError> validateSchema(GraphQLSchema schema) {
SchemaValidationErrorCollector validationErrorCollector = new SchemaValidationErrorCollector();
List<SchemaValidationRule> rules = new ArrayList<>();
rules.add(new NoUnbrokenInputCycles());

rules.add(new ObjectsImplementInterfaces());

List<GraphQLType> types = schema.getAllTypesAsList();
types.forEach(type -> {
for (SchemaValidationRule rule : rules) {
rule.check(type, validationErrorCollector);
}
});

traverse(schema.getQueryType(), rules, validationErrorCollector);
if (schema.isSupportingMutations()) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here I think subscription is missing?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes it is missing that. We can do that in another PR.

#414

traverse(schema.getMutationType(), rules, validationErrorCollector);
}
return validationErrorCollector.getErrors();
}
private void traverse(GraphQLOutputType root, List<ValidationRule> rules, ValidationErrorCollector validationErrorCollector) {

private void traverse(GraphQLOutputType root, List<SchemaValidationRule> rules, SchemaValidationErrorCollector validationErrorCollector) {
if (processed.contains(root)) {
return;
}
processed.add(root);
if (root instanceof GraphQLFieldsContainer) {
for (GraphQLFieldDefinition fieldDefinition : ((GraphQLFieldsContainer) root).getFieldDefinitions()) {
for (ValidationRule rule : rules) {
for (SchemaValidationRule rule : rules) {
rule.check(fieldDefinition, validationErrorCollector);
}
traverse(fieldDefinition.getType(), rules, validationErrorCollector);
Expand Down

This file was deleted.

This file was deleted.

9 changes: 0 additions & 9 deletions src/main/java/graphql/schema/validation/ValidationRule.java

This file was deleted.

Loading