getCollectionsByIds method

Future<List<Collection>?> getCollectionsByIds(
  1. List<String> idList, {
  2. List<MetafieldIdentifier>? metafields,
})

Returns a List of Collection

Implementation

Future<List<Collection>?> getCollectionsByIds(
  List<String> idList, {
  List<MetafieldIdentifier>? metafields,
}) async {
  try {
    final WatchQueryOptions _options = WatchQueryOptions(
      document: gql(getCollectionsByIdsQuery),
      variables: {
        'ids': idList,
        'metafields': metafields != null
            ? metafields.map((e) => e.toJson()).toList()
            : [],
      },
      fetchPolicy: ShopifyConfig.fetchPolicy,
    );
    final QueryResult result = await _graphQLClient!.query(_options);
    checkForError(result);
    var newResponse = List.generate(result.data!['nodes']?.length ?? 0,
        (index) => {"node": (result.data!['nodes'] ?? const {})[index]});
    var tempCollection = {"edges": newResponse};
    return Collections.fromGraphJson(tempCollection).collectionList;
  } catch (e) {
    log(e.toString());
  }
  return [Collection.fromJson({})];
}