get<_Model extends GraphqlModel> method

  1. @override
Future<List<_Model>> get<_Model extends GraphqlModel>({
  1. Query? query,
  2. ModelRepository<GraphqlModel>? repository,
})

Query for raw data and construct it with an Adapter

Implementation

@override
Future<List<_Model>> get<_Model extends GraphqlModel>({query, repository}) async {
  final adapter = modelDictionary.adapterFor[_Model]!;
  final request = GraphqlRequest<_Model>(
    action: QueryAction.get,
    modelDictionary: modelDictionary,
    query: query,
    variableNamespace: variableNamespace,
  ).request;
  if (request == null) return <_Model>[];
  await for (final resp in link.request(request)) {
    if (resp.data?.values == null) return <_Model>[];
    if (resp.data!.values.isEmpty || resp.data!.values.first == null) {
      return <_Model>[];
    }

    if (resp.data?.values.first is Iterable) {
      final results = resp.data?.values.first
          .map((v) => adapter.fromGraphql(v, provider: this, repository: repository))
          .toList()
          .cast<Future<_Model>>();

      return await Future.wait<_Model>(results);
    }

    if (resp.data?.values.first is Map) {
      return [
        await adapter.fromGraphql(resp.data?.values.first!,
            provider: this, repository: repository) as _Model
      ];
    }

    return [
      await adapter.fromGraphql(resp.data!, provider: this, repository: repository) as _Model
    ];
  }
  return <_Model>[];
}