Sha256: aa94f78ddf31d972d70a15244fcece7b9c7380edcdf5367c1b142233090372ef

Contents?: true

Size: 1.1 KB

Versions: 11

Compression:

Stored size: 1.1 KB

Contents

module GraphQL
  module StaticValidation
    class FieldsAreDefinedOnType
      include GraphQL::StaticValidation::Message::MessageHelper

      def validate(context)
        visitor = context.visitor
        visitor[GraphQL::Language::Nodes::Field] << -> (node, parent) {
          return if context.skip_field?(node.name)
          parent_type = context.object_types[-2]
          parent_type = parent_type.unwrap
          validate_field(context, node, parent_type, parent)
        }
      end

      private

      def validate_field(context, ast_field, parent_type, parent)
        if parent_type.kind.union?
          context.errors << message("Selections can't be made directly on unions (see selections on #{parent_type.name})", parent, context: context)
          return GraphQL::Language::Visitor::SKIP
        end

        field =  parent_type.get_field(ast_field.name)
        if field.nil?
          context.errors << message("Field '#{ast_field.name}' doesn't exist on type '#{parent_type.name}'", parent, context: context)
          return GraphQL::Language::Visitor::SKIP
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
graphql-0.18.10 lib/graphql/static_validation/rules/fields_are_defined_on_type.rb
graphql-0.18.9 lib/graphql/static_validation/rules/fields_are_defined_on_type.rb
graphql-0.18.8 lib/graphql/static_validation/rules/fields_are_defined_on_type.rb
graphql-0.18.7 lib/graphql/static_validation/rules/fields_are_defined_on_type.rb
graphql-0.18.6 lib/graphql/static_validation/rules/fields_are_defined_on_type.rb
graphql-0.18.5 lib/graphql/static_validation/rules/fields_are_defined_on_type.rb
graphql-0.18.4 lib/graphql/static_validation/rules/fields_are_defined_on_type.rb
graphql-0.18.3 lib/graphql/static_validation/rules/fields_are_defined_on_type.rb
graphql-0.18.2 lib/graphql/static_validation/rules/fields_are_defined_on_type.rb
graphql-0.18.1 lib/graphql/static_validation/rules/fields_are_defined_on_type.rb
graphql-0.18.0 lib/graphql/static_validation/rules/fields_are_defined_on_type.rb