Sha256: b69712f23f624627438ddaa8eb55f56bcf646388a95908474f037927dcf8cb5c

Contents?: true

Size: 1.31 KB

Versions: 6

Compression:

Stored size: 1.31 KB

Contents

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

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

      private

      def validate_field_selections(ast_field, field_defn, context)
        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, context: context)
        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, context: context)
        else
          error = nil
        end

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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
graphql-1.4.4 lib/graphql/static_validation/rules/fields_have_appropriate_selections.rb
graphql-1.4.3 lib/graphql/static_validation/rules/fields_have_appropriate_selections.rb
graphql-1.4.2 lib/graphql/static_validation/rules/fields_have_appropriate_selections.rb
graphql-1.4.1 lib/graphql/static_validation/rules/fields_have_appropriate_selections.rb
graphql-1.4.0 lib/graphql/static_validation/rules/fields_have_appropriate_selections.rb
graphql-1.3.0 lib/graphql/static_validation/rules/fields_have_appropriate_selections.rb