Sha256: 4f654759827df781c4d09faa55f1f4977f54dc2397d5b31140c3d83590f97d62

Contents?: true

Size: 1.07 KB

Versions: 18

Compression:

Stored size: 1.07 KB

Contents

# frozen_string_literal: true
module GraphQL
  module StaticValidation
    class FieldsAreDefinedOnType
      include GraphQL::StaticValidation::Message::MessageHelper

      def validate(context)
        visitor = context.visitor
        visitor[GraphQL::Language::Nodes::Field] << ->(node, parent) {
          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)
        field = context.warden.get_field(parent_type, ast_field.name)

        if field.nil?
          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)
          else
            context.errors << message("Field '#{ast_field.name}' doesn't exist on type '#{parent_type.name}'", ast_field, context: context)
          end
          return GraphQL::Language::Visitor::SKIP
        end
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
graphql-1.8.18 lib/graphql/static_validation/rules/fields_are_defined_on_type.rb
graphql-1.8.17 lib/graphql/static_validation/rules/fields_are_defined_on_type.rb
graphql-1.8.16 lib/graphql/static_validation/rules/fields_are_defined_on_type.rb
graphql-1.8.15 lib/graphql/static_validation/rules/fields_are_defined_on_type.rb
graphql-1.8.14 lib/graphql/static_validation/rules/fields_are_defined_on_type.rb
graphql-1.8.13 lib/graphql/static_validation/rules/fields_are_defined_on_type.rb
graphql-1.8.12 lib/graphql/static_validation/rules/fields_are_defined_on_type.rb
graphql-1.8.11 lib/graphql/static_validation/rules/fields_are_defined_on_type.rb
graphql-1.8.10 lib/graphql/static_validation/rules/fields_are_defined_on_type.rb
graphql-1.8.9 lib/graphql/static_validation/rules/fields_are_defined_on_type.rb
graphql-1.8.8 lib/graphql/static_validation/rules/fields_are_defined_on_type.rb
graphql-1.8.7 lib/graphql/static_validation/rules/fields_are_defined_on_type.rb
graphql-1.8.6 lib/graphql/static_validation/rules/fields_are_defined_on_type.rb
graphql-1.8.5 lib/graphql/static_validation/rules/fields_are_defined_on_type.rb
graphql-1.8.4 lib/graphql/static_validation/rules/fields_are_defined_on_type.rb
graphql-1.8.3 lib/graphql/static_validation/rules/fields_are_defined_on_type.rb
graphql-1.8.2 lib/graphql/static_validation/rules/fields_are_defined_on_type.rb
graphql-1.8.1 lib/graphql/static_validation/rules/fields_are_defined_on_type.rb