Module: GraphQL::Subscriptions::Serialize

Defined in:
lib/graphql/subscriptions/serialize.rb

Overview

Serialization helpers for passing subscription data around.

Constant Summary collapse

GLOBALID_KEY =

:nodoc:

"__gid__"
SYMBOL_KEY =
"__sym__"
SYMBOL_KEYS_KEY =
"__sym_keys__"
TIMESTAMP_KEY =
"__timestamp__"
TIMESTAMP_FORMAT =

eg '2020-01-01 23:59:59.123456789+05:00'

"%Y-%m-%d %H:%M:%S.%N%z"
TIMESTAMP_CLASS_NAMES =
["Date", "DateTime", "Time"].freeze
OPEN_STRUCT_KEY =
"__ostruct__"
HASH_KEY =
"__graphql_hash__"
RESERVED_KEYS =
[GLOBALID_KEY, SYMBOL_KEY, SYMBOL_KEYS_KEY, TIMESTAMP_KEY, OPEN_STRUCT_KEY, HASH_KEY].freeze

Class Method Summary collapse

Class Method Details

.dump(obj) ⇒ Object

Parameters

  • obj (Object) — Some subscription-related data to dump

Returns

  • String — The stringified object

:call-seq:

dump(Object obj) -> String


45
46
47
# File 'lib/graphql/subscriptions/serialize.rb', line 45

def self.dump(obj)
  JSON.generate(dump_value(obj))
end

.dump_recursive(obj) ⇒ Object

This is for turning objects into subscription scopes. It's a one-way transformation, can't reload this :'(

Parameters

  • obj (Object)

Returns

  • String

:call-seq:

dump_recursive(Object obj) -> String


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/graphql/subscriptions/serialize.rb', line 62

def self.dump_recursive(obj)
  case
  when obj.is_a?(Array)
    obj.map { |i| dump_recursive(i) }.join(':')
  when obj.is_a?(Hash)
    obj.map { |k, v| "#{dump_recursive(k)}:#{dump_recursive(v)}" }.join(":")
  when obj.is_a?(GraphQL::Schema::InputObject)
    dump_recursive(obj.to_h)
  when obj.respond_to?(:to_gid_param)
    obj.to_gid_param
  when obj.respond_to?(:to_param)
    obj.to_param
  else
    obj.to_s
  end
end

.load(str) ⇒ Object

Parameters

  • str (String) — A serialized object from .dump

Returns

  • Object — An object equivalent to the one passed to .dump

:call-seq:

load(String str) -> Object


30
31
32
33
# File 'lib/graphql/subscriptions/serialize.rb', line 30

def self.load(str)
  parsed_obj = JSON.parse(str)
  load_value(parsed_obj)
end