@metrichor/epi2me-client-node
Advanced tools
Comparing version 0.2.10360724 to 0.2.10361372
@@ -1,6 +0,13 @@ | ||
import type { TypePolicies } from '@apollo/client/core'; | ||
import type { FieldFunctionOptions, FieldMergeFunction, FieldReadFunction, StoreObject, TypePolicies } from '@apollo/client/core'; | ||
import type { PaginatedInterface, PaginatedOptions } from './pagination.type'; | ||
export declare const paginated_cache_key_fn: (args: Record<string, unknown> | null) => string[]; | ||
export declare function create_custom_key_fn(typename: string, key: string): (object: Readonly<StoreObject>) => string; | ||
export declare const paginated_merge: FieldMergeFunction<PaginatedInterface, PaginatedInterface, FieldFunctionOptions<PaginatedOptions>>; | ||
export declare const paginated_read: FieldReadFunction<PaginatedInterface, PaginatedInterface, FieldFunctionOptions<PaginatedOptions>>; | ||
export declare function create_item_read(type: string, id: string): FieldReadFunction; | ||
export declare function generate_type_policy(description: { | ||
cache_identifiers: Record<string, string | null>; | ||
paginated: string[]; | ||
query_relations: Record<string, string>; | ||
}): TypePolicies; | ||
//# sourceMappingURL=generate_type_policy.d.ts.map |
@@ -1,3 +0,4 @@ | ||
import type { NormalizedCacheObject, ApolloClient } from '@apollo/client/core'; | ||
import type { NormalizedCacheObject, ApolloClient, ApolloCache } from '@apollo/client/core'; | ||
export type GraphQLClient = ApolloClient<NormalizedCacheObject>; | ||
export type GraphQLCache = ApolloCache<NormalizedCacheObject>; | ||
//# sourceMappingURL=GraphQLClient.type.d.ts.map |
@@ -13,3 +13,3 @@ export type { ErrorHandler } from '@apollo/client/link/error'; | ||
export { create_mock_client } from '../common/mock_client'; | ||
export { GraphQLClient } from './GraphQLClient.type'; | ||
export { GraphQLClient, GraphQLCache } from './GraphQLClient.type'; | ||
export { GraphQLError } from 'graphql'; | ||
@@ -16,0 +16,0 @@ export { ApolloError, isApolloError, gql } from '@apollo/client/core'; |
124
index.js
@@ -7,4 +7,4 @@ 'use strict'; | ||
var error = require('@apollo/client/link/error'); | ||
var tsRuntimeTypecheck = require('ts-runtime-typecheck'); | ||
var graphql = require('graphql'); | ||
var tsRuntimeTypecheck = require('ts-runtime-typecheck'); | ||
@@ -75,7 +75,100 @@ function sha1_digest(message, key) { | ||
const NOCACHE_PAGINATION_KEYS = new Set(['page', 'pageSize', 'search']); | ||
const paginated_cache_key_fn = (args) => Object.keys(args !== null && args !== void 0 ? args : {}).filter(key => !NOCACHE_PAGINATION_KEYS.has(key)); | ||
function create_custom_key_fn(typename, key) { | ||
return (object) => { | ||
const id = object[key]; | ||
tsRuntimeTypecheck.invariant(typeof id === 'string', `Missing primary key "${key}" in "${typename}" response object.`); | ||
return `${typename}:${id}`; | ||
}; | ||
} | ||
const paginated_merge = (existing, incoming, { args }) => { | ||
var _a, _b; | ||
const { pageSize = 10, search = '' } = args !== null && args !== void 0 ? args : {}; | ||
const { page, totalCount } = incoming; | ||
tsRuntimeTypecheck.invariant(typeof page === 'number', 'The required field "page" was not included in the response'); | ||
tsRuntimeTypecheck.invariant(typeof totalCount === 'number', 'The required field "totalCount" was not included in the response'); | ||
const start = (page - 1) * pageSize; | ||
const existing_results = ((_a = existing === null || existing === void 0 ? void 0 : existing.search) !== null && _a !== void 0 ? _a : '') === search ? existing === null || existing === void 0 ? void 0 : existing.results : undefined; | ||
const merged = { | ||
page: 1, | ||
totalCount, | ||
search, | ||
results: existing_results, | ||
}; | ||
if (incoming.results) { | ||
merged.results = (_b = existing_results === null || existing_results === void 0 ? void 0 : existing_results.slice(0)) !== null && _b !== void 0 ? _b : []; | ||
let i = start; | ||
for (const incoming_item of incoming.results) { | ||
merged.results[i] = incoming_item; | ||
i += 1; | ||
} | ||
} | ||
return merged; | ||
}; | ||
const paginated_read = (existing, { args }) => { | ||
var _a, _b; | ||
const { page, pageSize = 10, search = '' } = args !== null && args !== void 0 ? args : {}; | ||
if (!existing || existing.search !== search) { | ||
return undefined; | ||
} | ||
tsRuntimeTypecheck.invariant(typeof existing.totalCount === 'number', 'The required field "totalCount" was not included in the cached data'); | ||
const pages = Math.ceil(existing.totalCount / pageSize); | ||
const current_page = Math.min(page !== null && page !== void 0 ? page : 1, pages); | ||
let end; | ||
let start; | ||
if (typeof page !== 'number' && ((_a = existing.results) === null || _a === void 0 ? void 0 : _a.length)) { | ||
const current_results = existing.results.length; | ||
const current_pages = Math.floor(current_results / pageSize); | ||
end = Math.max(current_pages, 1) * pageSize; | ||
start = 0; | ||
} | ||
else { | ||
end = Math.min(current_page * pageSize, existing.totalCount); | ||
start = Math.max(0, end - pageSize); | ||
} | ||
const results = (_b = existing.results) === null || _b === void 0 ? void 0 : _b.slice(start, end); | ||
const has_next = end < existing.totalCount; | ||
const merged = { | ||
page: current_page, | ||
pages, | ||
nextPage: page || !has_next ? null : Math.floor(end / pageSize) + 1, | ||
totalCount: existing.totalCount, | ||
hasPrevious: current_page > 1, | ||
hasNext: has_next, | ||
}; | ||
const target_length = end - start; | ||
if (results && results.length === target_length) { | ||
let gap = -1; | ||
for (let i = 0; i < target_length; i += 1) { | ||
if (typeof results[i] === 'undefined') { | ||
gap = i; | ||
break; | ||
} | ||
} | ||
if (gap < 0) { | ||
merged.results = results; | ||
} | ||
else if (gap >= pageSize && !page) { | ||
const valid_pages = Math.floor(gap / pageSize); | ||
merged.results = results.slice(0, valid_pages * pageSize); | ||
merged.nextPage = valid_pages + 1; | ||
merged.hasNext = true; | ||
} | ||
} | ||
return merged; | ||
}; | ||
function create_item_read(type, id) { | ||
return (_, { args, toReference }) => { | ||
return toReference({ | ||
__typename: type, | ||
[id]: args === null || args === void 0 ? void 0 : args[id], | ||
}); | ||
}; | ||
} | ||
function generate_type_policy(description) { | ||
const type_policy = {}; | ||
for (const [type, id] of Object.entries(description.cache_identifiers)) { | ||
for (const [typename, id] of Object.entries(description.cache_identifiers)) { | ||
if (id !== null) { | ||
type_policy[type] = { keyFields: [id] }; | ||
type_policy[typename] = { keyFields: create_custom_key_fn(typename, id) }; | ||
} | ||
@@ -89,9 +182,9 @@ } | ||
} | ||
query_policies[query] = { read: create_item_read(type, id) }; | ||
} | ||
for (const query of description.paginated) { | ||
query_policies[query] = { | ||
read(_, { args, toReference }) { | ||
return toReference({ | ||
__typename: type, | ||
[id]: args === null || args === void 0 ? void 0 : args[id], | ||
}); | ||
}, | ||
keyArgs: paginated_cache_key_fn, | ||
merge: paginated_merge, | ||
read: paginated_read, | ||
}; | ||
@@ -143,2 +236,14 @@ } | ||
}; | ||
var paginated = [ | ||
"allAccounts", | ||
"allDatasetDownloadBundles", | ||
"allDatasets", | ||
"allProducts", | ||
"allReports", | ||
"allUsergroupMembers", | ||
"allUsergroups", | ||
"allUsers", | ||
"allWorkflowInstances", | ||
"allWorkflows" | ||
]; | ||
var query_relations = { | ||
@@ -159,2 +264,3 @@ product: "ProductType", | ||
cache_identifiers: cache_identifiers, | ||
paginated: paginated, | ||
query_relations: query_relations | ||
@@ -161,0 +267,0 @@ }; |
{ | ||
"name": "@metrichor/epi2me-client-node", | ||
"version": "0.2.10360724", | ||
"version": "0.2.10361372", | ||
"license": "MPL-2.0", | ||
@@ -5,0 +5,0 @@ "author": "Metrichor <support@nanoporetech.com>", |
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
73798
56
1390