Sha256: 3d496f0311ab3e4c8893be4cca59e1647fb4c49001d5d2f73dd0bbd3e3b79f71

Contents?: true

Size: 855 Bytes

Versions: 1

Compression:

Stored size: 855 Bytes

Contents

module GraphQL
  module StaticValidation
    class FragmentTypesExist
      include GraphQL::StaticValidation::Message::MessageHelper

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

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

      private

      def validate_type_exists(node, context)
        return unless node.type
        type = context.schema.types.fetch(node.type, nil)
        if type.nil?
          context.errors << message("No such type #{node.type}, so it can't be a fragment condition", node, context: context)
          GraphQL::Language::Visitor::SKIP
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
graphql-0.19.4 lib/graphql/static_validation/rules/fragment_types_exist.rb