Repository navigation
#409 - Added interface checking on object types #412
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
bbakerman
merged 2 commits into
graphql-java:master
from
bbakerman:409-validate-interfaces-are-implemented
May 8, 2017
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
src/main/java/graphql/schema/validation/ObjectsImplementInterfaces.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/main/java/graphql/schema/validation/SchemaValidationErrorCollector.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } |
7 changes: 7 additions & 0 deletions
7
src/main/java/graphql/schema/validation/SchemaValidationErrorType.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
11
src/main/java/graphql/schema/validation/SchemaValidationRule.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } |
37 changes: 23 additions & 14 deletions
37
.../graphql/schema/validation/Validator.java → ...ql/schema/validation/SchemaValidator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 0 additions & 24 deletions
24
src/main/java/graphql/schema/validation/ValidationErrorCollector.java
This file was deleted.
Oops, something went wrong.
6 changes: 0 additions & 6 deletions
6
src/main/java/graphql/schema/validation/ValidationErrorType.java
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
here I think
subscriptionis missing?There was a problem hiding this comment.
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