getCollectionById method

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

Returns a collection by id.

Implementation

Future<Collection?> getCollectionById(
  String collectionId, {
  List<MetafieldIdentifier>? metafields,
}) async {
  try {
    final WatchQueryOptions _options = WatchQueryOptions(
      document: gql(getCollectionsByIdsQuery),
      variables: {
        'ids': [collectionId],
        'metafields': metafields != null
            ? metafields.map((e) => e.toJson()).toList()
            : [],
      },
      fetchPolicy: ShopifyConfig.fetchPolicy,
    );
    final QueryResult result = await _graphQLClient!.query(_options);
    checkForError(result);
    return Collection.fromGraphJson(result.data!);
  } catch (e) {
    log(e.toString());
  }
  return null;
}