Sha256: d315666929fc7923d994fe7dd348f10c9430823579cf824cc9c26e2b6416b5e1

Contents?: true

Size: 1.07 KB

Versions: 8

Compression:

Stored size: 1.07 KB

Contents

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

8 entries across 8 versions & 1 rubygems

Version Path
graphql-1.2.6 lib/graphql/static_validation/rules/fragments_are_on_composite_types.rb
graphql-1.2.5 lib/graphql/static_validation/rules/fragments_are_on_composite_types.rb
graphql-1.2.4 lib/graphql/static_validation/rules/fragments_are_on_composite_types.rb
graphql-1.2.3 lib/graphql/static_validation/rules/fragments_are_on_composite_types.rb
graphql-1.2.2 lib/graphql/static_validation/rules/fragments_are_on_composite_types.rb
graphql-1.2.1 lib/graphql/static_validation/rules/fragments_are_on_composite_types.rb
graphql-1.2.0 lib/graphql/static_validation/rules/fragments_are_on_composite_types.rb
graphql-1.1.0 lib/graphql/static_validation/rules/fragments_are_on_composite_types.rb