Class: GraphQL::Schema::Validation

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/schema/validation.rb

Overview

This module provides a function for validating GraphQL types.

Its RULES contain objects that respond to #call(type). Rules are looked up for given types (by class ancestry), then applied to the object until an error is returned.

Defined Under Namespace

Modules: Rules

Constant Summary collapse

RULES =

A mapping of {Class => [Proc, Proc...]} pairs. To validate an instance, find entries where object.is_a?(key) is true. Then apply each rule from the matching values.

{
  GraphQL::Field => [
    Rules::NAME_IS_STRING,
    Rules::RESERVED_NAME,
    Rules::DESCRIPTION_IS_STRING_OR_NIL,
    Rules.assert_property(:deprecation_reason, String, NilClass),
    Rules.assert_property(:type, GraphQL::BaseType, GraphQL::Schema::LateBoundType),
    Rules.assert_property(:property, Symbol, NilClass),
    Rules::ARGUMENTS_ARE_STRING_TO_ARGUMENT,
    Rules::ARGUMENTS_ARE_VALID,
  ],
  GraphQL::Argument => [
    Rules::NAME_IS_STRING,
    Rules::RESERVED_NAME,
    Rules::DESCRIPTION_IS_STRING_OR_NIL,
    Rules::TYPE_IS_VALID_INPUT_TYPE,
    Rules::DEFAULT_VALUE_IS_VALID_FOR_TYPE,
  ],
  GraphQL::BaseType => [
    Rules::NAME_IS_STRING,
    Rules::RESERVED_TYPE_NAME,
    Rules::DESCRIPTION_IS_STRING_OR_NIL,
  ],
  GraphQL::ObjectType => [
    Rules.assert_property_list_of(:interfaces, GraphQL::InterfaceType),
    Rules::FIELDS_ARE_VALID,
    Rules::INTERFACES_ARE_IMPLEMENTED,
  ],
  GraphQL::InputObjectType => [
    Rules::ARGUMENTS_ARE_STRING_TO_ARGUMENT,
    Rules::ARGUMENTS_ARE_VALID,
  ],
  GraphQL::UnionType => [
    Rules.assert_property_list_of(:possible_types, GraphQL::ObjectType),
    Rules::HAS_ONE_OR_MORE_POSSIBLE_TYPES,
  ],
  GraphQL::InterfaceType => [
    Rules::FIELDS_ARE_VALID,
  ],
  GraphQL::Schema => [
    Rules::SCHEMA_INSTRUMENTERS_ARE_VALID,
    Rules::SCHEMA_CAN_RESOLVE_TYPES,
    Rules::SCHEMA_CAN_FETCH_IDS,
    Rules::SCHEMA_CAN_GENERATE_IDS,
  ],
}

Class Method Summary collapse

Class Method Details

.validate(object) ⇒ String, Nil

Lookup the rules for object based on its class, Then returns an error message or nil

Parameters:

  • object (Object)

    something to be validated

Returns:

  • (String, Nil)

    error message, if there was one



14
15
16
17
18
19
20
# File 'lib/graphql/schema/validation.rb', line 14

def self.validate(object)
  rules = RULES.reduce([]) do |memo, (parent_class, validations)|
    memo + (object.is_a?(parent_class) ? validations : [])
  end
  # Stops after the first error
  rules.reduce(nil) { |memo, rule| memo || rule.call(object) }
end