Sha256: 7cea0f2aea41cdcb102324d7d496931f78a1a552cbddba050a4f32ecdfe0f645

Contents?: true

Size: 1.14 KB

Versions: 63

Compression:

Stored size: 1.14 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)
        # urlsafe_decode64 is for forward compatibility
        Base64Bp.urlsafe_decode64(node_id).split(separator, 2)
      end
    end
  end
end

Version data entries

63 entries across 63 versions & 1 rubygems

Version Path
graphql-1.9.7 lib/graphql/schema/unique_within_type.rb
graphql-1.9.6 lib/graphql/schema/unique_within_type.rb
graphql-1.9.5 lib/graphql/schema/unique_within_type.rb
graphql-1.9.4 lib/graphql/schema/unique_within_type.rb
graphql-1.9.3 lib/graphql/schema/unique_within_type.rb
graphql-1.9.2 lib/graphql/schema/unique_within_type.rb
graphql-1.8.17 lib/graphql/schema/unique_within_type.rb
graphql-1.8.16 lib/graphql/schema/unique_within_type.rb
graphql-1.9.1 lib/graphql/schema/unique_within_type.rb
graphql-1.9.0 lib/graphql/schema/unique_within_type.rb
graphql-1.8.15 lib/graphql/schema/unique_within_type.rb
graphql-1.9.0.pre4 lib/graphql/schema/unique_within_type.rb
graphql-1.8.14 lib/graphql/schema/unique_within_type.rb
graphql-1.9.0.pre3 lib/graphql/schema/unique_within_type.rb
graphql-1.9.0.pre2 lib/graphql/schema/unique_within_type.rb
graphql-1.8.13 lib/graphql/schema/unique_within_type.rb
graphql-1.8.12 lib/graphql/schema/unique_within_type.rb
graphql-1.9.0.pre1 lib/graphql/schema/unique_within_type.rb
graphql-1.8.11 lib/graphql/schema/unique_within_type.rb
graphql-1.8.10 lib/graphql/schema/unique_within_type.rb