Skip to content

Commit 33807b3

Browse files
authored
Merge branch 'master' into fix-unique-name-nil
2 parents a2900f8 + 36ee564 commit 33807b3

6 files changed

+51
-28
lines changed

rules.go

+33-22
Original file line numberDiff line numberDiff line change
@@ -214,30 +214,38 @@ func FieldsOnCorrectTypeRule(context *ValidationContext) *ValidationRuleInstance
214214
var result interface{}
215215
if node, ok := p.Node.(*ast.Field); ok {
216216
ttype := context.ParentType()
217+
if ttype == nil {
218+
return action, result
219+
}
220+
if t, ok := ttype.(*Object); ok && t == nil {
221+
return action, result
222+
}
223+
if t, ok := ttype.(*Interface); ok && t == nil {
224+
return action, result
225+
}
226+
if t, ok := ttype.(*Union); ok && t == nil {
227+
return action, result
228+
}
229+
fieldDef := context.FieldDef()
230+
if fieldDef == nil {
231+
// This field doesn't exist, lets look for suggestions.
232+
nodeName := ""
233+
if node.Name != nil {
234+
nodeName = node.Name.Value
235+
}
236+
// First determine if there are any suggested types to condition on.
237+
suggestedTypeNames := getSuggestedTypeNames(context.Schema(), ttype, nodeName)
217238

218-
if ttype != nil {
219-
fieldDef := context.FieldDef()
220-
if fieldDef == nil {
221-
// This field doesn't exist, lets look for suggestions.
222-
nodeName := ""
223-
if node.Name != nil {
224-
nodeName = node.Name.Value
225-
}
226-
// First determine if there are any suggested types to condition on.
227-
suggestedTypeNames := getSuggestedTypeNames(context.Schema(), ttype, nodeName)
228-
229-
// If there are no suggested types, then perhaps this was a typo?
230-
suggestedFieldNames := []string{}
231-
if len(suggestedTypeNames) == 0 {
232-
suggestedFieldNames = getSuggestedFieldNames(context.Schema(), ttype, nodeName)
233-
}
234-
235-
reportError(
236-
context,
237-
UndefinedFieldMessage(nodeName, ttype.Name(), suggestedTypeNames, suggestedFieldNames),
238-
[]ast.Node{node},
239-
)
239+
// If there are no suggested types, then perhaps this was a typo?
240+
suggestedFieldNames := []string{}
241+
if len(suggestedTypeNames) == 0 {
242+
suggestedFieldNames = getSuggestedFieldNames(context.Schema(), ttype, nodeName)
240243
}
244+
reportError(
245+
context,
246+
UndefinedFieldMessage(nodeName, ttype.Name(), suggestedTypeNames, suggestedFieldNames),
247+
[]ast.Node{node},
248+
)
241249
}
242250
}
243251
return action, result
@@ -1721,6 +1729,9 @@ func VariablesInAllowedPositionRule(context *ValidationContext) *ValidationRuleI
17211729
func isValidLiteralValue(ttype Input, valueAST ast.Value) (bool, []string) {
17221730
// A value must be provided if the type is non-null.
17231731
if ttype, ok := ttype.(*NonNull); ok {
1732+
if e := ttype.Error(); e != nil {
1733+
return false, []string{e.Error()}
1734+
}
17241735
if valueAST == nil {
17251736
if ttype.OfType.Name() != "" {
17261737
return false, []string{fmt.Sprintf(`Expected "%v!", found null.`, ttype.OfType.Name())}

rules_default_values_of_correct_type_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,7 @@ func TestValidate_VariableDefaultValuesOfCorrectType_ListVariablesWithInvalidIte
101101
2, 40),
102102
})
103103
}
104+
105+
func TestValidate_VariableDefaultValuesOfCorrectType_InvalidNonNull(t *testing.T) {
106+
testutil.ExpectPassesRule(t, graphql.DefaultValuesOfCorrectTypeRule, `query($g:e!){a}`)
107+
}

rules_fields_on_correct_type_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -236,3 +236,7 @@ func TestValidate_FieldsOnCorrectTypeErrorMessage_LimitLotsOfFieldSuggestions(t
236236
t.Fatalf("Unexpected message, expected: %v, got %v", expected, message)
237237
}
238238
}
239+
240+
func TestValidate_FieldsOnCorrectType_NilCrash(t *testing.T) {
241+
testutil.ExpectPassesRule(t, graphql.FieldsOnCorrectTypeRule, `mutation{o}`)
242+
}

rules_overlapping_fields_can_be_merged.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -467,10 +467,10 @@ func (rule *overlappingFieldsCanBeMergedRule) getFieldsAndFragmentNames(parentTy
467467
fieldName = selection.Name.Value
468468
}
469469
var fieldDef *FieldDefinition
470-
if parentType, ok := parentType.(*Object); ok {
470+
if parentType, ok := parentType.(*Object); ok && parentType != nil {
471471
fieldDef, _ = parentType.Fields()[fieldName]
472472
}
473-
if parentType, ok := parentType.(*Interface); ok {
473+
if parentType, ok := parentType.(*Interface); ok && parentType != nil {
474474
fieldDef, _ = parentType.Fields()[fieldName]
475475
}
476476

rules_overlapping_fields_can_be_merged_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -888,3 +888,7 @@ func TestValidate_OverlappingFieldsCanBeMerged_ReturnTypesMustBeUnambiguous_Igno
888888
}
889889
`)
890890
}
891+
892+
func TestValidate_OverlappingFieldsCanBeMerged_NilCrash(t *testing.T) {
893+
testutil.ExpectPassesRule(t, graphql.OverlappingFieldsCanBeMergedRule, `subscription {e}`)
894+
}

type_info.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -252,14 +252,14 @@ func DefaultTypeInfoFieldDef(schema *Schema, parentType Type, fieldAST *ast.Fiel
252252
schema.QueryType() == parentType {
253253
return TypeMetaFieldDef
254254
}
255-
if name == TypeNameMetaFieldDef.Name {
256-
if _, ok := parentType.(*Object); ok && parentType != nil {
255+
if name == TypeNameMetaFieldDef.Name && parentType != nil {
256+
if t, ok := parentType.(*Object); ok && t != nil {
257257
return TypeNameMetaFieldDef
258258
}
259-
if _, ok := parentType.(*Interface); ok && parentType != nil {
259+
if t, ok := parentType.(*Interface); ok && t != nil {
260260
return TypeNameMetaFieldDef
261261
}
262-
if _, ok := parentType.(*Union); ok && parentType != nil {
262+
if t, ok := parentType.(*Union); ok && t != nil {
263263
return TypeNameMetaFieldDef
264264
}
265265
}

0 commit comments

Comments
 (0)