@sitecore-jss/sitecore-jss
Version:
This module is provided as a part of Sitecore JavaScript Rendering SDK. It contains the core JSS APIs (layout service) and utilities.
62 lines (61 loc) • 2.96 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SearchQueryService = void 0;
/**
* @deprecated use GraphQLClient instead
* Provides functionality for performing GraphQL 'search' operations, including handling pagination.
* This class is meant to be extended or used as a mixin; it's not meant to be used directly.
* @template T The type of objects being requested.
* @mixin
*/
class SearchQueryService {
/**
* Creates an instance of search query service.
* @param {GraphQLClient} client that fetches data from a GraphQL endpoint.
*/
constructor(client) {
this.client = client;
}
/**
* 1. Validates mandatory search query arguments
* 2. Executes search query with pagination
* 3. Aggregates pagination results into a single result-set.
* @template T The type of objects being requested.
* @param {string | DocumentNode} query the search query.
* @param {SearchQueryVariables} args search query arguments.
* @returns {T[]} array of result objects.
* @throws {RangeError} if a valid root item ID is not provided.
* @throws {RangeError} if the provided language(s) is(are) not valid.
*/
fetch(query, args) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
if (!args.rootItemId) {
throw new RangeError('"rootItemId" and "language" must be non-empty strings');
}
if (!args.language) {
throw new RangeError('"rootItemId" and "language" must be non-empty strings');
}
let results = [];
let hasNext = true;
let after = '';
while (hasNext) {
const fetchResponse = yield this.client.request(query, Object.assign(Object.assign({}, args), { after }));
results = results.concat((_a = fetchResponse === null || fetchResponse === void 0 ? void 0 : fetchResponse.search) === null || _a === void 0 ? void 0 : _a.results);
hasNext = fetchResponse.search.pageInfo.hasNext;
after = fetchResponse.search.pageInfo.endCursor;
}
return results;
});
}
}
exports.SearchQueryService = SearchQueryService;