Sha256: 05db3f6a8850ec4f74ab388cb55021fa71bbd22759595e23736f47eece49099a

Contents?: true

Size: 1.06 KB

Versions: 13

Compression:

Stored size: 1.06 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.errors, node, parent_type, parent)
        }
      end

      private

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

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

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
graphql-0.17.2 lib/graphql/static_validation/rules/fields_are_defined_on_type.rb
graphql-0.17.1 lib/graphql/static_validation/rules/fields_are_defined_on_type.rb
graphql-0.17.0 lib/graphql/static_validation/rules/fields_are_defined_on_type.rb
graphql-0.16.1 lib/graphql/static_validation/rules/fields_are_defined_on_type.rb
graphql-0.16.0 lib/graphql/static_validation/rules/fields_are_defined_on_type.rb
graphql-0.15.3 lib/graphql/static_validation/rules/fields_are_defined_on_type.rb
graphql-0.15.2 lib/graphql/static_validation/rules/fields_are_defined_on_type.rb
graphql-0.14.2 lib/graphql/static_validation/rules/fields_are_defined_on_type.rb
graphql-0.15.1 lib/graphql/static_validation/rules/fields_are_defined_on_type.rb
graphql-0.15.0 lib/graphql/static_validation/rules/fields_are_defined_on_type.rb
graphql-0.14.1 lib/graphql/static_validation/rules/fields_are_defined_on_type.rb
graphql-0.14.0 lib/graphql/static_validation/rules/fields_are_defined_on_type.rb
graphql-0.13.0 lib/graphql/static_validation/rules/fields_are_defined_on_type.rb