Skip to content

Commit

Permalink
Merge branch 'master' into bigdrum/edge
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-ramon committed Aug 5, 2017
2 parents e6bf0db + c68a65c commit 08ca841
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 31 deletions.
65 changes: 40 additions & 25 deletions rules.go
Expand Up @@ -214,30 +214,38 @@ func FieldsOnCorrectTypeRule(context *ValidationContext) *ValidationRuleInstance
var result interface{}
if node, ok := p.Node.(*ast.Field); ok {
ttype := context.ParentType()
if ttype == nil {
return action, result
}
if t, ok := ttype.(*Object); ok && t == nil {
return action, result
}
if t, ok := ttype.(*Interface); ok && t == nil {
return action, result
}
if t, ok := ttype.(*Union); ok && t == nil {
return action, result
}
fieldDef := context.FieldDef()
if fieldDef == nil {
// This field doesn't exist, lets look for suggestions.
nodeName := ""
if node.Name != nil {
nodeName = node.Name.Value
}
// First determine if there are any suggested types to condition on.
suggestedTypeNames := getSuggestedTypeNames(context.Schema(), ttype, nodeName)

if ttype != nil {
fieldDef := context.FieldDef()
if fieldDef == nil {
// This field doesn't exist, lets look for suggestions.
nodeName := ""
if node.Name != nil {
nodeName = node.Name.Value
}
// First determine if there are any suggested types to condition on.
suggestedTypeNames := getSuggestedTypeNames(context.Schema(), ttype, nodeName)

// If there are no suggested types, then perhaps this was a typo?
suggestedFieldNames := []string{}
if len(suggestedTypeNames) == 0 {
suggestedFieldNames = getSuggestedFieldNames(context.Schema(), ttype, nodeName)
}

reportError(
context,
UndefinedFieldMessage(nodeName, ttype.Name(), suggestedTypeNames, suggestedFieldNames),
[]ast.Node{node},
)
// If there are no suggested types, then perhaps this was a typo?
suggestedFieldNames := []string{}
if len(suggestedTypeNames) == 0 {
suggestedFieldNames = getSuggestedFieldNames(context.Schema(), ttype, nodeName)
}
reportError(
context,
UndefinedFieldMessage(nodeName, ttype.Name(), suggestedTypeNames, suggestedFieldNames),
[]ast.Node{node},
)
}
}
return action, result
Expand Down Expand Up @@ -1517,7 +1525,7 @@ func UniqueInputFieldNamesRule(context *ValidationContext) *ValidationRuleInstan
//
// A GraphQL document is only valid if all defined operations have unique names.
func UniqueOperationNamesRule(context *ValidationContext) *ValidationRuleInstance {
knownOperationNames := map[string]*ast.Name{}
knownOperationNames := make(map[string]ast.Node)

visitorOpts := &visitor.VisitorOptions{
KindFuncMap: map[string]visitor.NamedVisitFuncs{
Expand All @@ -1528,14 +1536,18 @@ func UniqueOperationNamesRule(context *ValidationContext) *ValidationRuleInstanc
if node.Name != nil {
operationName = node.Name.Value
}
var errNode ast.Node = node
if node.Name != nil {
errNode = node.Name
}
if nameAST, ok := knownOperationNames[operationName]; ok {
reportError(
context,
fmt.Sprintf(`There can only be one operation named "%v".`, operationName),
[]ast.Node{nameAST, node.Name},
[]ast.Node{nameAST, errNode},
)
} else {
knownOperationNames[operationName] = node.Name
knownOperationNames[operationName] = errNode
}
}
return visitor.ActionSkip, nil
Expand Down Expand Up @@ -1717,6 +1729,9 @@ func VariablesInAllowedPositionRule(context *ValidationContext) *ValidationRuleI
func isValidLiteralValue(ttype Input, valueAST ast.Value) (bool, []string) {
// A value must be provided if the type is non-null.
if ttype, ok := ttype.(*NonNull); ok {
if e := ttype.Error(); e != nil {
return false, []string{e.Error()}
}
if valueAST == nil {
if ttype.OfType.Name() != "" {
return false, []string{fmt.Sprintf(`Expected "%v!", found null.`, ttype.OfType.Name())}
Expand Down
4 changes: 4 additions & 0 deletions rules_default_values_of_correct_type_test.go
Expand Up @@ -101,3 +101,7 @@ func TestValidate_VariableDefaultValuesOfCorrectType_ListVariablesWithInvalidIte
2, 40),
})
}

func TestValidate_VariableDefaultValuesOfCorrectType_InvalidNonNull(t *testing.T) {
testutil.ExpectPassesRule(t, graphql.DefaultValuesOfCorrectTypeRule, `query($g:e!){a}`)
}
4 changes: 4 additions & 0 deletions rules_fields_on_correct_type_test.go
Expand Up @@ -236,3 +236,7 @@ func TestValidate_FieldsOnCorrectTypeErrorMessage_LimitLotsOfFieldSuggestions(t
t.Fatalf("Unexpected message, expected: %v, got %v", expected, message)
}
}

func TestValidate_FieldsOnCorrectType_NilCrash(t *testing.T) {
testutil.ExpectPassesRule(t, graphql.FieldsOnCorrectTypeRule, `mutation{o}`)
}
4 changes: 2 additions & 2 deletions rules_overlapping_fields_can_be_merged.go
Expand Up @@ -467,10 +467,10 @@ func (rule *overlappingFieldsCanBeMergedRule) getFieldsAndFragmentNames(parentTy
fieldName = selection.Name.Value
}
var fieldDef *FieldDefinition
if parentType, ok := parentType.(*Object); ok {
if parentType, ok := parentType.(*Object); ok && parentType != nil {
fieldDef, _ = parentType.Fields()[fieldName]
}
if parentType, ok := parentType.(*Interface); ok {
if parentType, ok := parentType.(*Interface); ok && parentType != nil {
fieldDef, _ = parentType.Fields()[fieldName]
}

Expand Down
4 changes: 4 additions & 0 deletions rules_overlapping_fields_can_be_merged_test.go
Expand Up @@ -888,3 +888,7 @@ func TestValidate_OverlappingFieldsCanBeMerged_ReturnTypesMustBeUnambiguous_Igno
}
`)
}

func TestValidate_OverlappingFieldsCanBeMerged_NilCrash(t *testing.T) {
testutil.ExpectPassesRule(t, graphql.OverlappingFieldsCanBeMergedRule, `subscription {e}`)
}
6 changes: 6 additions & 0 deletions rules_unique_operation_names_test.go
Expand Up @@ -102,3 +102,9 @@ func TestValidate_UniqueOperationNames_MultipleOperationsOfSameNameOfDifferentTy
testutil.RuleError(`There can only be one operation named "Foo".`, 2, 13, 5, 20),
})
}

func TestValidate_UniqueOperationNames_MultipleAnonymousOperations(t *testing.T) {
testutil.ExpectFailsRule(t, graphql.UniqueOperationNamesRule, `{a}{b}`, []gqlerrors.FormattedError{
testutil.RuleError(`There can only be one operation named "".`, 1, 1, 1, 4),
})
}
8 changes: 4 additions & 4 deletions type_info.go
Expand Up @@ -252,14 +252,14 @@ func DefaultTypeInfoFieldDef(schema *Schema, parentType Type, fieldAST *ast.Fiel
schema.QueryType() == parentType {
return TypeMetaFieldDef
}
if name == TypeNameMetaFieldDef.Name {
if _, ok := parentType.(*Object); ok && parentType != nil {
if name == TypeNameMetaFieldDef.Name && parentType != nil {
if t, ok := parentType.(*Object); ok && t != nil {
return TypeNameMetaFieldDef
}
if _, ok := parentType.(*Interface); ok && parentType != nil {
if t, ok := parentType.(*Interface); ok && t != nil {
return TypeNameMetaFieldDef
}
if _, ok := parentType.(*Union); ok && parentType != nil {
if t, ok := parentType.(*Union); ok && t != nil {
return TypeNameMetaFieldDef
}
}
Expand Down

0 comments on commit 08ca841

Please sign in to comment.