github.com/dgraph-io/dgraph@v1.2.8/graphql/schema/validation_rules.go (about)

     1  /*
     2   * Copyright 2019 Dgraph Labs, Inc. and Contributors
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package schema
    18  
    19  import (
    20  	"github.com/vektah/gqlparser/ast"
    21  	"github.com/vektah/gqlparser/validator"
    22  )
    23  
    24  func listTypeCheck(observers *validator.Events, addError validator.AddErrFunc) {
    25  	observers.OnValue(func(walker *validator.Walker, value *ast.Value) {
    26  		if value.Definition == nil || value.ExpectedType == nil {
    27  			return
    28  		}
    29  
    30  		if value.Kind == ast.Variable {
    31  			return
    32  		}
    33  
    34  		// ExpectedType.Elem will be not nil if it is of list type. Otherwise
    35  		// it will be nil. So it's safe to say that value.Kind should be list
    36  		if !(value.ExpectedType.Elem != nil && value.Kind != ast.ListValue) {
    37  			return
    38  		}
    39  
    40  		addError(validator.Message("Value provided %s is incompatible with expected type %s",
    41  			value.String(),
    42  			value.ExpectedType.String()), validator.At(value.Position))
    43  	})
    44  }
    45  
    46  func variableTypeCheck(observers *validator.Events, addError validator.AddErrFunc) {
    47  	observers.OnValue(func(walker *validator.Walker, value *ast.Value) {
    48  		if value.Definition == nil || value.ExpectedType == nil ||
    49  			value.VariableDefinition == nil {
    50  			return
    51  		}
    52  
    53  		if value.Kind != ast.Variable {
    54  			return
    55  		}
    56  
    57  		if value.VariableDefinition.Type.IsCompatible(value.ExpectedType) {
    58  			return
    59  		}
    60  
    61  		addError(validator.Message("Variable type provided %s is incompatible with expected type %s",
    62  			value.VariableDefinition.Type.String(),
    63  			value.ExpectedType.String()), validator.At(value.Position))
    64  	})
    65  }