As long as the JSON string is only evaluated once, the JSON.parse approach is much faster compared to the JavaScript object literal, especially for cold loads.
# frozen_string_literal: truemoduleTypesclassDateTime < GraphQL::Schema::Scalar
description "A datetime type, encoded in ISO 8601 string in JSON, or timestamp type in MessagePack"# @param value [ActiveSupport::TimeWithZone]# @return [String]defself.coerce_result(value, _ctx)
value # use to_json or equivalent converterend# @param value [String,ActiveSupport::TimeWithZone]# @return [ActiveSupport::TimeWithZone]defself.coerce_input(value, _ctx)
if value.is_a?(ActiveSupport::TimeWithZone)
value
elseTime.zone.parse(value)
endrescueArgumentErrornilendendend
MessagePackを単なるJSONの代替として考えるとJavaScript的にはメリットに乏しいのですが、JSONよりも表現が豊富で拡張性にすぐれたシリアライズフォーマットと捉えるとGraphQLと相性がいいなあ…ということを考えています。というわけで最近MessagePack for JavaScriptの実装を頑張っているのでした。まずは優れた実装がないと話になりませんからね。