Sha256: 6faa9f9e1d199575eea668d18f88839cee9dc637f9d8c4218d87602d8fc2b9bf

Contents?: true

Size: 1.03 KB

Versions: 7

Compression:

Stored size: 1.03 KB

Contents

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) || object_value_str.include?(separator)
          raise "encode(#{type_name}, #{object_value_str}) contains reserved characters `#{separator}`"
        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)
        Base64.decode64(node_id).split(separator)
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
graphql-1.2.6 lib/graphql/schema/unique_within_type.rb
graphql-1.2.5 lib/graphql/schema/unique_within_type.rb
graphql-1.2.4 lib/graphql/schema/unique_within_type.rb
graphql-1.2.3 lib/graphql/schema/unique_within_type.rb
graphql-1.2.2 lib/graphql/schema/unique_within_type.rb
graphql-1.2.1 lib/graphql/schema/unique_within_type.rb
graphql-1.2.0 lib/graphql/schema/unique_within_type.rb