@sterblue/sterblue-sdk
Version:
Sterblue Graph SDK for graphile.sterblue.com
104 lines (89 loc) • 3.33 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getPaginationPath = void 0;
var _fp = require("lodash/fp");
var _graphqlTag = _interopRequireDefault(require("graphql-tag"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
// INFO: a selection is an entity in the query definition document
/**
* Check if the current selection has `offset` and `first` as arguments and are declared as variables
* @param selection {Object} a selection definition
*/
const haveFirstAndOffset = selection => {
const args = (0, _fp.get)("arguments", selection);
const indexOfFirst = (0, _fp.findIndex)({
kind: "Argument",
name: {
kind: "Name",
value: "first"
},
value: {
kind: "Variable",
name: {
kind: "Name",
value: "first"
}
}
}, args);
const indexOfOffset = (0, _fp.findIndex)({
kind: "Argument",
name: {
kind: "Name",
value: "offset"
},
value: {
kind: "Variable",
name: {
kind: "Name",
value: "offset"
}
}
}, args);
return indexOfFirst != -1 && indexOfOffset != -1;
};
const selectionsWithArguments = (0, _fp.filter)(selection => (0, _fp.size)(selection.arguments) != 0);
/**
* "Recursive" Check if there is a first and a offset on the selection if yes the return the current path if not it trigger itself for all the subselection.
* @param selection {Object} a selection definition
* @param path {String} the path taken in the query definition to get to the current selection
*/
const getPaginationPathForOneSelection = (selection, path) => {
const selectionName = (0, _fp.get)("name.value", selection);
const currentPath = (0, _fp.isNil)(path) ? [selectionName] : [...path, selectionName];
if (haveFirstAndOffset(selection)) {
return currentPath;
} else {
const subSelectionWithArguments = selectionsWithArguments((0, _fp.get)("selectionSet.selections", selection));
if ((0, _fp.size)(subSelectionWithArguments) == 0) {
return null;
} else {
return getPaginationPathFromMultipleSelections(subSelectionWithArguments, currentPath);
}
}
};
/**
* it's just Wrapper to trigger getPaginationPathForOneSelection on multiple selections
* @param selections {Object[]} multiple selections definition
* @param path {String} the path taken in the query definition to get to the current selection
*/
const getPaginationPathFromMultipleSelections = (selections, path) => {
return (0, _fp.reduce)((localPath, subSelection) => {
if ((0, _fp.isString)(localPath)) {
return localPath;
} else {
return getPaginationPathForOneSelection(subSelection, path);
}
}, null, selections);
};
/**
* exported function that get the path of the entity we want to paginate over
* @param queryDefinition {Object || String} a query definition as a string or a document
*/
const getPaginationPath = queryDefinition => {
const queryDocument = (0, _fp.isString)(queryDefinition) ? (0, _graphqlTag.default)(queryDefinition) : queryDefinition;
const selections = (0, _fp.get)("definitions[0].selectionSet.selections", queryDocument);
return getPaginationPathFromMultipleSelections(selections, null);
};
exports.getPaginationPath = getPaginationPath;