Skip to content

Commit b8fdebb

Browse files
committed
refactoring
add some comments
1 parent 2288eb6 commit b8fdebb

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

src/main/java/graphql/validation/TraversalContext.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ private void enterImpl(Field field) {
102102
fieldDefinition = getFieldDef(schema, parentType, field);
103103
}
104104
addFieldDef(fieldDefinition);
105-
addType(fieldDefinition != null ? fieldDefinition.getType() : null);
105+
addOutputType(fieldDefinition != null ? fieldDefinition.getType() : null);
106106
}
107107

108108
private void enterImpl(Directive directive) {
@@ -111,11 +111,11 @@ private void enterImpl(Directive directive) {
111111

112112
private void enterImpl(OperationDefinition operationDefinition) {
113113
if (operationDefinition.getOperation() == OperationDefinition.Operation.MUTATION) {
114-
addType(schema.getMutationType());
114+
addOutputType(schema.getMutationType());
115115
} else if (operationDefinition.getOperation() == OperationDefinition.Operation.QUERY) {
116-
addType(schema.getQueryType());
116+
addOutputType(schema.getQueryType());
117117
} else if (operationDefinition.getOperation() == OperationDefinition.Operation.SUBSCRIPTION) {
118-
addType(schema.getSubscriptionType());
118+
addOutputType(schema.getSubscriptionType());
119119
} else {
120120
throw new ShouldNotHappenException();
121121
}
@@ -129,12 +129,12 @@ private void enterImpl(InlineFragment inlineFragment) {
129129
} else {
130130
type = (GraphQLOutputType) getParentType();
131131
}
132-
addType(type);
132+
addOutputType(type);
133133
}
134134

135135
private void enterImpl(FragmentDefinition fragmentDefinition) {
136136
GraphQLType type = schema.getType(fragmentDefinition.getTypeCondition().getName());
137-
addType((GraphQLOutputType) type);
137+
addOutputType((GraphQLOutputType) type);
138138
}
139139

140140
private void enterImpl(VariableDefinition variableDefinition) {
@@ -214,11 +214,15 @@ private GraphQLNullableType getNullableType(GraphQLType type) {
214214
return (GraphQLNullableType) (type instanceof GraphQLNonNull ? ((GraphQLNonNull) type).getWrappedType() : type);
215215
}
216216

217+
/**
218+
* @return can be null if current node does not have a OutputType associated: for example
219+
* if the current field is unknown
220+
*/
217221
public GraphQLOutputType getOutputType() {
218222
return lastElement(outputTypeStack);
219223
}
220224

221-
private void addType(GraphQLOutputType type) {
225+
private void addOutputType(GraphQLOutputType type) {
222226
outputTypeStack.add(type);
223227
}
224228

@@ -228,6 +232,9 @@ private <T> T lastElement(List<T> list) {
228232
return list.get(list.size() - 1);
229233
}
230234

235+
/**
236+
* @return can be null if the parent is not a CompositeType
237+
*/
231238
public GraphQLCompositeType getParentType() {
232239
return lastElement(parentTypeStack);
233240
}

src/main/java/graphql/validation/rules/FieldsOnCorrectType.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
import graphql.language.Field;
55
import graphql.schema.GraphQLCompositeType;
66
import graphql.schema.GraphQLFieldDefinition;
7-
import graphql.validation.*;
7+
import graphql.validation.AbstractRule;
8+
import graphql.validation.ValidationContext;
9+
import graphql.validation.ValidationError;
10+
import graphql.validation.ValidationErrorCollector;
11+
import graphql.validation.ValidationErrorType;
812

913
public class FieldsOnCorrectType extends AbstractRule {
1014

@@ -17,6 +21,7 @@ public FieldsOnCorrectType(ValidationContext validationContext, ValidationErrorC
1721
@Override
1822
public void checkField(Field field) {
1923
GraphQLCompositeType parentType = getValidationContext().getParentType();
24+
// this means the parent type is not a CompositeType, which is an error handled elsewhere
2025
if (parentType == null) return;
2126
GraphQLFieldDefinition fieldDef = getValidationContext().getFieldDef();
2227
if (fieldDef == null) {

0 commit comments

Comments
 (0)