Sha256: 8a131cfef25577fc727553766e59b1d88c993785123b6eaf8e58d92a55ce734f

Contents?: true

Size: 1.84 KB

Versions: 40

Compression:

Stored size: 1.84 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.type.unwrap, context)
        }

        context.visitor[GraphQL::Language::Nodes::OperationDefinition] << ->(node, parent)  {
          validate_field_selections(node, context.type_definition, context)
        }
      end

      private


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

        if msg
          node_name = case ast_node
          when GraphQL::Language::Nodes::Field
            "field '#{ast_node.name}'"
          when GraphQL::Language::Nodes::OperationDefinition
            if ast_node.name.nil?
              "anonymous query"
            else
              "#{ast_node.operation_type} '#{ast_node.name}'"
            end
          else
            raise("Unexpected node #{ast_node}")
          end
          context.errors << message(msg % { node_name: node_name }, ast_node, context: context)
          GraphQL::Language::Visitor::SKIP
        end
      end
    end
  end
end

Version data entries

40 entries across 40 versions & 1 rubygems

Version Path
graphql-1.8.0.pre6 lib/graphql/static_validation/rules/fields_have_appropriate_selections.rb
graphql-1.8.0.pre5 lib/graphql/static_validation/rules/fields_have_appropriate_selections.rb
graphql-1.7.9 lib/graphql/static_validation/rules/fields_have_appropriate_selections.rb
graphql-1.8.0.pre4 lib/graphql/static_validation/rules/fields_have_appropriate_selections.rb
graphql-1.8.0.pre3 lib/graphql/static_validation/rules/fields_have_appropriate_selections.rb
graphql-1.7.8 lib/graphql/static_validation/rules/fields_have_appropriate_selections.rb
graphql-1.8.0.pre2 lib/graphql/static_validation/rules/fields_have_appropriate_selections.rb
graphql-1.7.7 lib/graphql/static_validation/rules/fields_have_appropriate_selections.rb
graphql-1.8.0.pre1 lib/graphql/static_validation/rules/fields_have_appropriate_selections.rb
graphql-1.7.6 lib/graphql/static_validation/rules/fields_have_appropriate_selections.rb
graphql-1.7.5 lib/graphql/static_validation/rules/fields_have_appropriate_selections.rb
graphql-1.7.4 lib/graphql/static_validation/rules/fields_have_appropriate_selections.rb
graphql-1.7.3 lib/graphql/static_validation/rules/fields_have_appropriate_selections.rb
graphql-1.7.2 lib/graphql/static_validation/rules/fields_have_appropriate_selections.rb
graphql-1.7.1 lib/graphql/static_validation/rules/fields_have_appropriate_selections.rb
graphql-1.7.0 lib/graphql/static_validation/rules/fields_have_appropriate_selections.rb
graphql-1.6.8 lib/graphql/static_validation/rules/fields_have_appropriate_selections.rb
graphql-1.6.7 lib/graphql/static_validation/rules/fields_have_appropriate_selections.rb
graphql-1.6.6 lib/graphql/static_validation/rules/fields_have_appropriate_selections.rb
graphql-1.6.5 lib/graphql/static_validation/rules/fields_have_appropriate_selections.rb