Sha256: 07bd47588bf7eecff53211c5b30c0a3c0ebdd206ac0c6b99059690aae4b88b09

Contents?: true

Size: 1.1 KB

Versions: 75

Compression:

Stored size: 1.1 KB

Contents

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

      HAS_TYPE_CONDITION = [
        GraphQL::Language::Nodes::FragmentDefinition,
        GraphQL::Language::Nodes::InlineFragment,
      ]

      def validate(context)
        HAS_TYPE_CONDITION.each do |node_class|
          context.visitor[node_class] << ->(node, parent) {
            validate_type_is_composite(node, context)
          }
        end
      end

      private

      def validate_type_is_composite(node, context)
        node_type = node.type
        if node_type.nil?
          # Inline fragment on the same type
        else
          type_name = node_type.to_query_string
          type_def = context.warden.get_type(type_name)
          if type_def.nil? || !type_def.kind.composite?
            context.errors <<  message("Invalid fragment on type #{type_name} (must be Union, Interface or Object)", node, context: context)
            GraphQL::Language::Visitor::SKIP
          end
        end
      end
    end
  end
end

Version data entries

75 entries across 75 versions & 1 rubygems

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