OperationMessage.fromJson constructor

OperationMessage.fromJson(
  1. Map map
)

Implementation

factory OperationMessage.fromJson(Map map) {
  var type = map['type'];
  var payload = map['payload'];
  var id = map['id'];

  if (type == null) {
    throw ArgumentError.notNull('type');
  } else if (type is! String) {
    throw ArgumentError.value(type, 'type', 'must be a string');
  } else if (id is num) {
    id = id.toString();
  } else if (id != null && id is! String) {
    throw ArgumentError.value(id, 'id', 'must be a string or number');
  }

  // TODO: This is technically a violation of the spec.
  // https://github.com/apollographql/subscriptions-transport-ws/issues/551
  if (map.containsKey('query') ||
      map.containsKey('operationName') ||
      map.containsKey('variables')) payload = Map.from(map);
  return OperationMessage(type, id: id as String?, payload: payload);
}