OperationMessage.fromJson constructor

OperationMessage.fromJson(
  1. Map map
)

Implementation

factory OperationMessage.fromJson(Map map) {
  final Object? type = map['type'];
  Object? payload = map['payload'];
  Object? 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: 1I 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<Object?, Object?>.from(map);
  return OperationMessage(type, id: id as String?, payload: payload);
}