Sha256: 91ba5cbf084ec23255c864b423bdf2026340096f033c1976e7aa0c3b5984d432

Contents?: true

Size: 1.16 KB

Versions: 20

Compression:

Stored size: 1.16 KB

Contents

# Scalars _can't_ have selections
# Objects _must_ have selections
class GraphQL::StaticValidation::FieldsHaveAppropriateSelections
  include GraphQL::StaticValidation::Message::MessageHelper

  def validate(context)
    context.visitor[GraphQL::Language::Nodes::Field] << -> (node, parent)  {
      return if context.skip_field?(node.name)
      field_defn = context.field_definition
      validate_field_selections(node, field_defn, context.errors)
    }
  end

  private

  def validate_field_selections(ast_field, field_defn, errors)
    resolved_type = field_defn.type.unwrap

    if resolved_type.kind.scalar? && ast_field.selections.any?
      error = message("Selections can't be made on scalars (field '#{ast_field.name}' returns #{resolved_type.name} but has selections [#{ast_field.selections.map(&:name).join(", ")}])", ast_field)
    elsif resolved_type.kind.object? && ast_field.selections.none?
      error = message("Objects must have selections (field '#{ast_field.name}' returns #{resolved_type.name} but has no selections)", ast_field)
    else
      error = nil
    end

    if !error.nil?
      errors << error
      GraphQL::Language::Visitor::SKIP
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
graphql-0.12.1 lib/graphql/static_validation/rules/fields_have_appropriate_selections.rb
graphql-0.12.0 lib/graphql/static_validation/rules/fields_have_appropriate_selections.rb
graphql-0.11.1 lib/graphql/static_validation/rules/fields_have_appropriate_selections.rb
graphql-0.11.0 lib/graphql/static_validation/rules/fields_have_appropriate_selections.rb
graphql-0.10.9 lib/graphql/static_validation/rules/fields_have_appropriate_selections.rb
graphql-0.10.8 lib/graphql/static_validation/rules/fields_have_appropriate_selections.rb
graphql-0.10.7 lib/graphql/static_validation/rules/fields_have_appropriate_selections.rb
graphql-0.10.6 lib/graphql/static_validation/rules/fields_have_appropriate_selections.rb
graphql-0.10.5 lib/graphql/static_validation/rules/fields_have_appropriate_selections.rb
graphql-0.10.4 lib/graphql/static_validation/rules/fields_have_appropriate_selections.rb
graphql-0.10.3 lib/graphql/static_validation/rules/fields_have_appropriate_selections.rb
graphql-0.10.2 lib/graphql/static_validation/rules/fields_have_appropriate_selections.rb
graphql-0.10.1 lib/graphql/static_validation/rules/fields_have_appropriate_selections.rb
graphql-0.10.0 lib/graphql/static_validation/rules/fields_have_appropriate_selections.rb
graphql-0.9.5 lib/graphql/static_validation/rules/fields_have_appropriate_selections.rb
graphql-0.9.4 lib/graphql/static_validation/rules/fields_have_appropriate_selections.rb
graphql-0.9.3 lib/graphql/static_validation/rules/fields_have_appropriate_selections.rb
graphql-0.9.2 lib/graphql/static_validation/rules/fields_have_appropriate_selections.rb
graphql-0.8.1 lib/graphql/static_validation/rules/fields_have_appropriate_selections.rb
graphql-0.8.0 lib/graphql/static_validation/rules/fields_have_appropriate_selections.rb