Sha256: fbbb2e4a5f2768a4cb490425bd0a3957759782c09860a85b36bd4584630fb3bd

Contents?: true

Size: 957 Bytes

Versions: 7

Compression:

Stored size: 957 Bytes

Contents

module GraphQL
  class Schema
    module UniqueWithinType
      DEFAULT_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: DEFAULT_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: DEFAULT_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.1.0 lib/graphql/schema/unique_within_type.rb
graphql-1.0.0 lib/graphql/schema/unique_within_type.rb
graphql-0.19.4 lib/graphql/schema/unique_within_type.rb
graphql-0.19.3 lib/graphql/schema/unique_within_type.rb
graphql-0.19.2 lib/graphql/schema/unique_within_type.rb
graphql-0.19.1 lib/graphql/schema/unique_within_type.rb
graphql-0.19.0 lib/graphql/schema/unique_within_type.rb