getAllCollections method

Future<List<Collection>> getAllCollections({
  1. SortKeyCollection sortKeyCollection = SortKeyCollection.UPDATED_AT,
  2. bool reverse = false,
})

Returns all available collections.

Tip: When editing Collections you can choose on which channel or app you want to make them available.

Implementation

Future<List<Collection>> getAllCollections(
    {SortKeyCollection sortKeyCollection = SortKeyCollection.UPDATED_AT,
    bool reverse = false}) async {
  List<Collection> collectionList = [];
  Collections tempCollection;
  String? cursor;
  WatchQueryOptions _options;
  do {
    _options = WatchQueryOptions(
      document: gql(getAllCollectionsOptimizedQuery),
      variables: {
        'cursor': cursor,
        'sortKey': sortKeyCollection.parseToString(),
        'reverse': reverse
      },
      fetchPolicy: ShopifyConfig.fetchPolicy,
    );
    final QueryResult result = await _graphQLClient!.query(_options);
    checkForError(result);
    tempCollection = (Collections.fromGraphJson(
        (result.data ?? const {})['collections'] ?? {}));
    collectionList.addAll(tempCollection.collectionList);
    cursor = collectionList.isNotEmpty ? collectionList.last.cursor : '';
  } while ((tempCollection.hasNextPage == true));
  return collectionList;
}