Sha256: 675739e27bd053d753703ae43303542ee2ad91a260d56354a1b6d476d5795cdb

Contents?: true

Size: 1.1 KB

Versions: 109

Compression:

Stored size: 1.1 KB

Contents

# frozen_string_literal: true
require 'graphql/schema/base_64_bp'

module GraphQL
  class Schema
    module UniqueWithinType
      class << self
        attr_accessor :default_id_separator
      end
      self.default_id_separator = "-"

      module_function

      # @param type_name [String]
      # @param object_value [Any]
      # @return [String] a unique, opaque ID generated as a function of the two inputs
      def encode(type_name, object_value, separator: self.default_id_separator)
        object_value_str = object_value.to_s

        if type_name.include?(separator)
          raise "encode(#{type_name}, #{object_value_str}) contains reserved characters `#{separator}` in the type name"
        end

        Base64.strict_encode64([type_name, object_value_str].join(separator))
      end

      # @param node_id [String] A unique ID generated by {.encode}
      # @return [Array<(String, String)>] The type name & value passed to {.encode}
      def decode(node_id, separator: self.default_id_separator)
        GraphQL::Schema::Base64Encoder.decode(node_id).split(separator, 2)
      end
    end
  end
end

Version data entries

109 entries across 109 versions & 2 rubygems

Version Path
graphql-2.1.13 lib/graphql/schema/unique_within_type.rb
graphql-2.0.31 lib/graphql/schema/unique_within_type.rb
graphql-1.13.23 lib/graphql/schema/unique_within_type.rb
graphql-2.0.29 lib/graphql/schema/unique_within_type.rb
graphql-2.1.12 lib/graphql/schema/unique_within_type.rb
graphql-1.13.22 lib/graphql/schema/unique_within_type.rb
graphql-2.2.7 lib/graphql/schema/unique_within_type.rb
graphql-2.2.6 lib/graphql/schema/unique_within_type.rb
graphql-2.1.11 lib/graphql/schema/unique_within_type.rb
graphql-2.0.28 lib/graphql/schema/unique_within_type.rb
graphql-1.13.21 lib/graphql/schema/unique_within_type.rb
graphql-2.2.5 lib/graphql/schema/unique_within_type.rb
graphql-2.2.4 lib/graphql/schema/unique_within_type.rb
graphql-2.2.3 lib/graphql/schema/unique_within_type.rb
graphql-2.1.10 lib/graphql/schema/unique_within_type.rb
graphql-2.2.2 lib/graphql/schema/unique_within_type.rb
graphql-2.1.9 lib/graphql/schema/unique_within_type.rb
graphql-2.2.1 lib/graphql/schema/unique_within_type.rb
graphql-2.2.0 lib/graphql/schema/unique_within_type.rb
graphql-2.1.8 lib/graphql/schema/unique_within_type.rb