apollo-link-pagination
Advanced tools
Comparing version 1.0.3 to 1.0.4
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createPaginationLink = void 0; | ||
var tslib_1 = require("tslib"); | ||
var client_1 = require("@apollo/client"); | ||
var utilities_1 = require("@apollo/client/utilities"); | ||
var graphql_1 = require("graphql"); | ||
var lodash_get_1 = tslib_1.__importDefault(require("lodash.get")); | ||
var appliedPolicies = []; | ||
const tslib_1 = require("tslib"); | ||
const client_1 = require("@apollo/client"); | ||
const utilities_1 = require("@apollo/client/utilities"); | ||
const graphql_1 = require("graphql"); | ||
const lodash_get_1 = tslib_1.__importDefault(require("lodash.get")); | ||
const appliedPolicies = []; | ||
function createPaginationLink(cache, paginatePolicies) { | ||
return new client_1.ApolloLink(function (operation, forward) { | ||
var query = operation.query; | ||
var possibleDirectives = Object.keys(paginatePolicies); | ||
return new client_1.ApolloLink((operation, forward) => { | ||
const { query } = operation; | ||
const possibleDirectives = Object.keys(paginatePolicies); | ||
graphql_1.visit(query, { | ||
Directive: function (node, _key, _parent, path) { | ||
var _a, _b; | ||
var _c, _d, _e, _f, _g, _h, _j; | ||
var foundDirective = possibleDirectives.find(function (directive) { return directive === node.name.value; }); | ||
Directive(node, _key, _parent, path) { | ||
var _a, _b, _c, _d, _e, _f, _g; | ||
const foundDirective = possibleDirectives.find((directive) => directive === node.name.value); | ||
if (!foundDirective) { | ||
return; | ||
} | ||
operation.query = (_c = utilities_1.removeDirectivesFromDocument([{ name: foundDirective }], query)) !== null && _c !== void 0 ? _c : query; | ||
var field = lodash_get_1.default(query, path.join('.').replace(/\.directives\.0$/, '')); | ||
var typenameArg = (_d = node.arguments) === null || _d === void 0 ? void 0 : _d.find(function (arg) { return arg.name.value === 'typename'; }); | ||
var typename = (_f = (_e = typenameArg === null || typenameArg === void 0 ? void 0 : typenameArg.value) === null || _e === void 0 ? void 0 : _e.value) !== null && _f !== void 0 ? _f : 'Query'; | ||
var keyArgsArg = (_g = node.arguments) === null || _g === void 0 ? void 0 : _g.find(function (arg) { return arg.name.value === 'keyArgs'; }); | ||
var keyArgs = (_j = (_h = keyArgsArg === null || keyArgsArg === void 0 ? void 0 : keyArgsArg.value) === null || _h === void 0 ? void 0 : _h.values.map(function (v) { return v.value; })) !== null && _j !== void 0 ? _j : []; | ||
var key = typename + "-" + field.name.value; | ||
operation.query = (_a = utilities_1.removeDirectivesFromDocument([{ name: foundDirective }], query)) !== null && _a !== void 0 ? _a : query; | ||
const field = lodash_get_1.default(query, path.join('.').replace(/\.directives\.0$/, '')); | ||
const typenameArg = (_b = node.arguments) === null || _b === void 0 ? void 0 : _b.find((arg) => arg.name.value === 'typename'); | ||
const typename = (_d = (_c = typenameArg === null || typenameArg === void 0 ? void 0 : typenameArg.value) === null || _c === void 0 ? void 0 : _c.value) !== null && _d !== void 0 ? _d : 'Query'; | ||
const keyArgsArg = (_e = node.arguments) === null || _e === void 0 ? void 0 : _e.find((arg) => arg.name.value === 'keyArgs'); | ||
const keyArgs = (_g = (_f = keyArgsArg === null || keyArgsArg === void 0 ? void 0 : keyArgsArg.value) === null || _f === void 0 ? void 0 : _f.values.map((v) => v.value)) !== null && _g !== void 0 ? _g : []; | ||
const key = `${typename}-${field.name.value}`; | ||
if (appliedPolicies.includes(key)) { | ||
@@ -33,9 +32,9 @@ return; | ||
appliedPolicies.push(key); | ||
cache.policies.addTypePolicies((_a = {}, | ||
_a[typename] = { | ||
fields: (_b = {}, | ||
_b[field.name.value] = tslib_1.__assign({ keyArgs: keyArgs }, paginatePolicies[foundDirective]), | ||
_b), | ||
cache.policies.addTypePolicies({ | ||
[typename]: { | ||
fields: { | ||
[field.name.value]: Object.assign({ keyArgs }, paginatePolicies[foundDirective]), | ||
}, | ||
}, | ||
_a)); | ||
}); | ||
}, | ||
@@ -42,0 +41,0 @@ }); |
@@ -1,4 +0,8 @@ | ||
export { createPaginationLink } from './createPaginationLink'; | ||
export { cursorPagination } from './presets/cursorPagination'; | ||
export { offsetLimitPagination } from './presets/offsetLimitPagination'; | ||
import { createPaginationLink } from "./createPaginationLink"; | ||
declare const _default: { | ||
createPaginationLink: typeof createPaginationLink; | ||
cursorPagination: (idField?: string) => import("@apollo/client").FieldPolicy<any, any, any>; | ||
offsetLimitPagination: (idField?: string) => import("@apollo/client").FieldPolicy<any, any, any>; | ||
}; | ||
export default _default; | ||
//# sourceMappingURL=index.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.offsetLimitPagination = exports.cursorPagination = exports.createPaginationLink = void 0; | ||
var createPaginationLink_1 = require("./createPaginationLink"); | ||
Object.defineProperty(exports, "createPaginationLink", { enumerable: true, get: function () { return createPaginationLink_1.createPaginationLink; } }); | ||
var cursorPagination_1 = require("./presets/cursorPagination"); | ||
Object.defineProperty(exports, "cursorPagination", { enumerable: true, get: function () { return cursorPagination_1.cursorPagination; } }); | ||
var offsetLimitPagination_1 = require("./presets/offsetLimitPagination"); | ||
Object.defineProperty(exports, "offsetLimitPagination", { enumerable: true, get: function () { return offsetLimitPagination_1.offsetLimitPagination; } }); | ||
const createPaginationLink_1 = require("./createPaginationLink"); | ||
const cursorPagination_1 = require("./presets/cursorPagination"); | ||
const offsetLimitPagination_1 = require("./presets/offsetLimitPagination"); | ||
exports.default = { | ||
createPaginationLink: createPaginationLink_1.createPaginationLink, | ||
cursorPagination: cursorPagination_1.cursorPagination, | ||
offsetLimitPagination: offsetLimitPagination_1.offsetLimitPagination, | ||
}; | ||
//# sourceMappingURL=index.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.cursorPagination = void 0; | ||
var tslib_1 = require("tslib"); | ||
var lodash_mergewith_1 = tslib_1.__importDefault(require("lodash.mergewith")); | ||
var lodash_uniqby_1 = tslib_1.__importDefault(require("lodash.uniqby")); | ||
var cursorPagination = function (idField) { | ||
if (idField === void 0) { idField = 'id'; } | ||
return ({ | ||
merge: function (existing, incoming, _a) { | ||
var readField = _a.readField; | ||
return lodash_mergewith_1.default(tslib_1.__assign({}, existing), tslib_1.__assign({}, incoming), function (value, srcValue) { | ||
if (Array.isArray(value)) { | ||
var result = value.concat(srcValue); | ||
return lodash_uniqby_1.default(result, function (item) { return readField(idField, item); }); | ||
} | ||
}); | ||
}, | ||
}); | ||
}; | ||
const tslib_1 = require("tslib"); | ||
const lodash_mergewith_1 = tslib_1.__importDefault(require("lodash.mergewith")); | ||
const lodash_uniqby_1 = tslib_1.__importDefault(require("lodash.uniqby")); | ||
const cursorPagination = (idField = 'id') => ({ | ||
merge(existing, incoming, { readField }) { | ||
return lodash_mergewith_1.default(Object.assign({}, existing), Object.assign({}, incoming), (value, srcValue) => { | ||
if (Array.isArray(value)) { | ||
const result = value.concat(srcValue); | ||
return lodash_uniqby_1.default(result, (item) => readField(idField, item)); | ||
} | ||
}); | ||
}, | ||
}); | ||
exports.cursorPagination = cursorPagination; | ||
//# sourceMappingURL=cursorPagination.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.offsetLimitPagination = void 0; | ||
var tslib_1 = require("tslib"); | ||
var lodash_uniqby_1 = tslib_1.__importDefault(require("lodash.uniqby")); | ||
var offsetLimitPagination = function (idField) { | ||
if (idField === void 0) { idField = 'id'; } | ||
return ({ | ||
merge: function (existing, incoming, _a) { | ||
var readField = _a.readField; | ||
return lodash_uniqby_1.default((existing !== null && existing !== void 0 ? existing : []).concat(incoming), function (item) { return readField(idField, item); }); | ||
}, | ||
}); | ||
}; | ||
const tslib_1 = require("tslib"); | ||
const lodash_uniqby_1 = tslib_1.__importDefault(require("lodash.uniqby")); | ||
const offsetLimitPagination = (idField = 'id') => ({ | ||
merge(existing, incoming, { readField }) { | ||
return lodash_uniqby_1.default((existing !== null && existing !== void 0 ? existing : []).concat(incoming), (item) => readField(idField, item)); | ||
}, | ||
}); | ||
exports.offsetLimitPagination = offsetLimitPagination; | ||
//# sourceMappingURL=offsetLimitPagination.js.map |
{ | ||
"name": "apollo-link-pagination", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "A @pagination directive for GraphQL queries", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
13138
95