@graphql-box/cache-manager 5.3.0 → 5.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,2 +1,2 @@
1
- import e from"@babel/runtime/helpers/defineProperty";import t from"@babel/runtime/helpers/applyDecoratedDescriptor";import"core-js/modules/web.structured-clone.js";import"core-js/modules/es.array.push.js";import{CACHE_ENTRY_ADDED as a,CACHE_ENTRY_QUERIED as s,PARTIAL_QUERY_COMPILED as i,TYPE_NAME_KEY as n,DEFAULT_TYPE_ID_KEY as r,QUERY_RESPONSES as c,REQUEST_FIELD_PATHS as h,DATA_ENTITIES as o}from"@graphql-box/core";import{isPlainObject as d,isArray as l,mergeObjects as y,isObjectLike as p,resolveFragments as u,getName as f,buildFieldKeysAndPaths as _,deleteFragmentSpreads as m,getChildFields as C,deleteChildFields as g,getInlineFragments as D,deleteInlineFragments as F,hasChildFields as P,deleteFragmentDefinitions as q,getOperationDefinitions as N,ArgsError as w,GroupedError as R,getFragmentDefinitions as E,hashRequest as b,rehydrateCacheMetadata as M,iterateChildFields as K,dehydrateCacheMetadata as v}from"@graphql-box/helpers";import{Cacheability as I}from"cacheability";import{Kind as k,print as T,OperationTypeNode as A}from"graphql";import{isNumber as x,isString as O,isEmpty as Q,keys as $,merge as j,set as S,get as z,isUndefined as H,assign as U,unset as G,isEqual as L}from"lodash-es";const V="noCache",Y=(e,t)=>l(e)?e.reduce(((e,a)=>!!e&&Y(a,t)),!0):!!d(e)&&B(e,t),B=(e,t)=>{const a=d(e)?Object.keys(e):[...e.keys()];return!(1!==a.length||!d(e)||!e[t])||a.reduce(((a,s)=>!!a&&(x(s)&&l(e)||O(s)&&d(e)?Y(e[s],t):a)),!0)},J=(e,t,a)=>y(e,t,((e,t)=>d(t)&&t[a]?t[a]:void 0)),W=(e,t,a)=>!e&&t?t:p(e)&&p(t)?J(e,t,a):e,X=(e,t,a)=>`${t.isEntity?t.typeName:e.__typename}::${String(e[a.typeIDKey])}`,Z=(e,t,a)=>{if(!d(e)||!(a in e))return!1;const{isEntity:s=!1,possibleTypes:i=[]}=t??{};return!!s||0!==i.length&&i.some((t=>t.typeName===e.__typename))},ee=(e,t,a,s)=>{if(!d(e))return e;const i=u(t.selectionSet?.selections,s.fragmentDefinitions);return $(e).reduce(((t,n)=>{const r=i.find((({fieldNode:e})=>f(e)===n));if(r){const t=_(r.fieldNode,a,s),i=s.fieldTypeMap.get(t.requestFieldPath);(Z(e[n],i,s.typeIDKey)||i?.hasArguments||i?.hasDirectives)&&delete e[n]}return t}),e)},te=(e,t,a,s)=>{const i=u(t.selectionSet?.selections,s.fragmentDefinitions);return $(e).reduce(((t,n)=>{const r=i.find((({fieldNode:e})=>f(e)===n));if(r){const{requestFieldPath:t}=_(r.fieldNode,a,s),i=s.fieldTypeMap.get(t);(Z(e[n],i,s.typeIDKey)||i?.hasArguments)&&delete e[n]}return t}),e)},ae=(e,t)=>{if(!e||0===e.length)return{hasData:!1,typeUnused:!!t};const[a]=e;if(a){const{hasData:e,typeName:s}=a;return{hasData:e,typeUnused:s?s!==t:void 0}}return{hasData:e.some((({hasData:e,typeName:a})=>a===t&&e)),typeUnused:!e.every((({typeName:e})=>e===t))}},se=(e,t,a,s,i)=>{const{fragmentDefinitions:r,typeIDKey:c}=i,h=C(e,{fragmentDefinitions:r});if(!h)return!1;const o={};for(let d=h.length-1;d>=0;d-=1){const l=h[d];if(!l)continue;const{fieldNode:y,fragmentKind:p,fragmentName:u,typeName:m}=l;p===k.FRAGMENT_SPREAD&&u&&!o[u]&&(o[u]={hasData:0,total:r?.[u]?C(r[u],{fragmentDefinitions:r})?.length??0:0});const D=f(y);if(D===c||D===n)continue;const{requestFieldPath:F}=_(y,{requestFieldPath:s},i),{hasData:q,typeUnused:N}=ae(t.get(F),m);if(q||N)if(p===k.FRAGMENT_SPREAD&&u){const e=o[u];e&&(e.hasData+=1)}else P(y,{fragmentDefinitions:r})?se(y,t,a,F,i)&&g(e,y):g(e,y)}return((e,t,a,s)=>{if(!Q(t))for(const i of Object.keys(t)){const n=a[i];if(!n)continue;n.paths.push(s);const{hasData:r,total:c}=t[i];r===c&&(m(e,i),n.deleted+=1)}})(e,o,a,s),((e,{fragmentDefinitions:t,typeIDKey:a})=>{const s=D(e);let i=!1;for(const n of s){const s=C(n,{fragmentDefinitions:t});if(!s||0===s.length){F(e,n),i=!0;continue}const[r]=s;if(r){const{fieldNode:t}=r;f(t)===a&&(F(e,n),i=!0)}}})(e,i),((e,{fragmentDefinitions:t,typeIDKey:a})=>{const s=C(e,{fragmentDefinitions:t});if(!s||s.length>3)return!1;const i=s.map((({fieldNode:e})=>f(e)));if(2===i.length&&i.every((e=>e===a||e===n)))return g(e,s.map((({fieldNode:e})=>e))),!0;if(1===i.length&&i[0]===a||i[0]===n){const[t]=s;if(t){const{fieldNode:a}=t;return g(e,a),!0}}})(e,i),!P(e,{fragmentDefinitions:r})},ie=(e,{fieldPathChecklist:t},a)=>{const{ast:s}=e,i=N(s,a.operation)[0];if(!i)return s;const{fragmentDefinitions:n,operation:r}=a,c=C(i,{fragmentDefinitions:n});if(!c)return s;const h=(({request:e},{fragmentDefinitions:t})=>$(t??{}).reduce(((t,a)=>(t[a]={deleted:0,paths:[],total:(e.match(new RegExp(`\\.\\.\\.${a}`,"g"))??[]).length},t)),{}))(e,a);for(let e=c.length-1;e>=0;e-=1){const{fieldNode:s}=c[e],{requestFieldPath:n}=_(s,{requestFieldPath:r},a);se(s,t,h,n,a)&&g(i,s)}return a.queryFiltered=!0,((e,t,a,s)=>{const i=$(a).reduce(((e,t)=>{const s=a[t];if(!s)return e;const{deleted:i,paths:n,total:r}=s;return 0===i&&1===r?[...e,{name:t,path:n[0]}]:e}),[]),{fragmentDefinitions:n={}}=s;for(const{name:e,path:r}of i){const i=n[e];i&&se(i,t,a,r,s)}const r=$(a).reduce(((e,t)=>{const s=a[t];if(!s)return e;const{deleted:i,total:n}=s;return i>0&&i===n?[...e,t]:e}),[]);return 0===r.length?e:q(e,{include:r})})(s,t,h,a)},ne=(e,t)=>l(e)&&x(t)||d(e)&&O(t)?e[t]:void 0,re=e=>!!p(e)&&(n in e&&O(e[n]));var ce,he,oe,de;let le=(ce=(e,t,a)=>{const i=a.value;i&&(a.value=async function(...e){return new Promise((t=>{(async()=>{const{debugManager:a,requestFieldCacheKey:n,...r}=e[3];if(!a)return void t(await i.apply(this,e));const c=a.now(),h=await i.apply(this,e),o=a.now(),d=o-c;t(h);const l={cacheType:e[0],context:r,options:e[2],requestHash:e[1],result:h,stats:{duration:d,endTime:o,startTime:c},...n?{decryptedCacheKey:n}:{}};a.log(s,l)})()}))})},he=(e,t,s)=>{const i=s.value;i&&(s.value=async function(...e){return new Promise((t=>{(async()=>{const{debugManager:s,requestFieldCacheKey:n,...r}=e[5];if(!s)return await i.apply(this,e),void t();const c=s.now();await i.apply(this,e);const h=s.now(),o=h-c;t();const d={cacheType:e[0],cachemapOptions:e[3],context:r,options:e[4],requestHash:e[1],stats:{duration:o,endTime:h,startTime:c},value:e[2],...n?{decryptedCacheKey:n}:{}};s.log(a,d)})()}))})},oe=(e,t,a)=>{const s=a.value;s&&(a.value=function(...e){const{debugManager:t,...a}=e[3];if(!t)return void s.apply(this,e);const n=t.now();s.apply(this,e);const r=t.now(),c=r-n;t.log(i,{context:a,options:e[2],requestHash:e[0],result:e[1],stats:{duration:c,endTime:r,startTime:n}})})},de=class t{static _countFieldPathChecklist(e){const t={missing:0,total:0};for(const[,a]of e){t.total+=a.length;const e=a.filter((({hasData:e})=>!e));t.missing+=e.length}return t}static _getFieldDataFromAncestor(e,t){const a=ne(e,t);return p(a)?structuredClone(a):a}static _getOperationCacheControl(e,t){const a="no-cache";if(!e)return a;const s=e.get(t);return s?s.printCacheControl():a}static _isNodeEntity(e){if(!e)return!1;const{isEntity:t,possibleTypes:a}=e;return t||a.some((e=>!!e.isEntity))}static _isNodeRequestFieldPath(e){return!!e&&(this._isNodeEntity(e)||e.hasArguments||e.hasDirectives)}static _isValid(e){return!z(e,["metadata","cacheControl",V],!1)&&e.checkTTL()}static _mergeResponseCacheMetadata(e,t){return t?new Map([...t.cacheMetadata,...e]):e}static _setCachedData(e,{data:t},a){const s=t=>{(l(e)&&x(a)||d(e))&&(e[a]=t)};p(t)||H(t)?l(t)?s([]):d(t)&&s({}):s(t)}static _setCachedResponseData(e,{cacheMetadata:a,data:s,fieldPathChecklist:i},{propNameOrIndex:n,requestFieldPath:r},c,h,{operation:o}){t._setCacheMetadata(a,e.cacheability,r,o),t._setFieldPathChecklist(i,e,r,c),t._setCachedData(s,e,n)}static _setCacheMetadata(e,t,a,s){if(!t)return;e.set(a,t);const i=e.get(s);(!i||i.metadata.ttl>t.metadata.ttl)&&e.set(s,t)}static _setFieldPathChecklist(e,{data:t},a,{dataTypename:s,fieldTypename:i,fragmentKind:n,fragmentName:r}){if(H(i)||n===k.FRAGMENT_SPREAD){if(e.has(a))return;return void e.set(a,[{fragmentKind:n,fragmentName:r,hasData:!H(t)}])}if(s!==i)return;const c=e.get(a)??[];c.some((({typeName:e})=>e===s))||e.set(a,[...c,{fragmentKind:n,fragmentName:r,hasData:!H(t),typeName:s}])}constructor(t){e(this,"_cache",void 0),e(this,"_cascadeCacheControl",void 0),e(this,"_fallbackOperationCacheability",void 0),e(this,"_partialQueryResponses",new Map),e(this,"_responseChunksAwaitingCaching",new Map),e(this,"_typeCacheDirectives",void 0),e(this,"_typeIDKey",void 0);const a=[];if("cache"in t||a.push(new w("@graphql-box/cache-manager expected cache to be in options.")),t.typeCacheDirectives&&!d(t.typeCacheDirectives)){const e="@graphql-box/cache-manager expected options.typeCacheDirectives to be a plain object.";a.push(new w(e))}if(a.length>0)throw new R("@graphql-box/cache-manager argument validation errors.",a);this._cache=t.cache,this._cascadeCacheControl=t.cascadeCacheControl??!1,this._fallbackOperationCacheability=t.fallbackOperationCacheability??V,this._typeCacheDirectives=t.typeCacheDirectives??{},this._typeIDKey=t.typeIDKey??r}async analyzeQuery(e,t,a){const{ast:s,hash:i}=e,n={...a,fragmentDefinitions:E(s),typeIDKey:this._typeIDKey},r=await this._retrieveCachedResponseData(e,t,n),{cacheMetadata:c,data:h,fieldCount:o}=r;if(o.missing===o.total||((e,t)=>B(e,t))(h,this._typeIDKey))return{updated:e};if(!o.missing){const e=this._setQueryResponseCacheEntry(i,{cacheMetadata:c,data:h},t,n);return t.awaitDataCaching&&await e,{response:{cacheMetadata:c,data:h}}}const d=ie(e,r,n),l=T(d),{fragmentDefinitions:y,typeIDKey:p,...u}=n;return U(a,{...u,filteredRequest:l}),this._setPartialQueryResponse(i,{cacheMetadata:c,data:h},t,a),{updated:{ast:d,hash:b(l),request:l}}}get cache(){return this._cache}async cacheQuery(e,t,a,s,i){const n={...i,fragmentDefinitions:E((t??e).ast),typeIDKey:this._typeIDKey};return this._cacheResponse(e,t,a,s,n)}async cacheResponse(e,t,a,s){const i={...s,fragmentDefinitions:E(e.ast),typeIDKey:this._typeIDKey};return this._cacheResponse(e,void 0,t,a,i)}async checkCacheEntry(e,t,a,s){return this._checkCacheEntry(e,t,a,s)}async checkQueryResponseCacheEntry(e,t,a){const s=await this._checkCacheEntry(c,e,t,a);if(!s)return!1;const{cacheMetadata:i,data:n}=s.entry;return{cacheMetadata:M(i),data:n}}deletePartialQueryResponse(e){this._partialQueryResponses.delete(e)}async setQueryResponseCacheEntry(e,t,a,s){return this._setQueryResponseCacheEntry(e.hash,t,a,s)}async _analyzeFieldNode(e,t,a,s,i){await(P(e,{fragmentDefinitions:i.fragmentDefinitions})?this._analyzeParentFieldNode(e,t,a,s,i):this._analyzeLeafFieldNode(e,t,a,s,i))}async _analyzeLeafFieldNode(e,a,s,i,n){const r=_(e,a,n),{hashedRequestFieldCacheKey:c,propNameOrIndex:h,requestFieldCacheKey:o,requestFieldPath:d}=r,l=n.fieldTypeMap.get(d),{entityData:y,fragmentKind:p,fragmentName:u,requestFieldPathData:f,typeName:m}=a,C={dataTypename:re(y)?y.__typename:re(f)?f.__typename:void 0,fieldTypename:m,fragmentKind:p,fragmentName:u};if(t._isNodeRequestFieldPath(l)){const{cacheability:e,entry:a}=await this._retrieveCachedRequestFieldPathData(c,o,i,n);t._setCachedResponseData({cacheability:e,data:a},s,r,C,i,n)}else{const e=t._getFieldDataFromAncestor(y,h)??t._getFieldDataFromAncestor(f,h);t._setFieldPathChecklist(s.fieldPathChecklist,{data:e},d,C),t._setCachedData(s.data,{data:e},h)}}async _analyzeParentFieldNode(e,a,s,i,r){const c=_(e,a,r),{propNameOrIndex:h,requestFieldCacheKey:o,requestFieldPath:d}=c,l=r.fieldTypeMap.get(d),{cacheability:y,data:u,entityData:f,requestFieldPathData:m}=await this._retrieveCachedParentNodeData(a,c,l,i,r),{fragmentKind:C,fragmentName:g,typeName:D}=a;if(t._setCachedResponseData({cacheability:y,data:u},s,c,{dataTypename:z(u,n),fieldTypename:D,fragmentKind:C,fragmentName:g},i,r),!p(u))return;const F=[];K(e,u,r.fragmentDefinitions,((e,t,a,n,c)=>{F.push(this._analyzeFieldNode(e,{cacheability:y,entityData:f,fragmentKind:a,fragmentName:n,index:c,requestFieldCacheKey:o,requestFieldPath:d,requestFieldPathData:m,typeName:t},{...s,data:ne(s.data,h)},i,r))})),await Promise.all(F)}_buildCacheMetadata({ast:e},{data:t,...a},s,i){const n=this._createCacheMetadata({data:t,...a},i),r=N(e,i.operation)[0];if(!r)return n;const c=C(r);if(!c)return n;for(const{fieldNode:e}of c)this._setFieldCacheability(e,{requestFieldPath:i.operation},{cacheMetadata:n,data:t},s,i);return n}async _cacheResponse(e,a,s,i,n){const r=((e,t)=>{if(!t.normalizePatchResponseData)return e;const{data:a,paths:s,...i}=e;return s?.length&&O(s[0])?{...i,data:S({},s[0],a),paths:s}:e})(s,n);let c=r;((e,t)=>t.hasDeferOrStream&&e.hasNext)(s,n)&&(this._setResponseChunksAwaitingCaching(r,n),c=void 0),((e,t)=>t.hasDeferOrStream&&!e.hasNext&&e.paths)(s,n)&&(c=this._retrieveResponseDataForCaching(r,n));const h=[];if(c){const{data:s}=c,o=this._buildCacheMetadata(e,c,i,n);let d,l;if(h.push(this._setEntityAndRequestFieldPathCacheEntries(e,{cacheMetadata:o,entityData:structuredClone(s),requestFieldPathData:structuredClone(s)},i,n)),n.operation===A.QUERY){let r;n.queryFiltered&&a&&(h.push(this._setQueryResponseCacheEntry(a.hash,{cacheMetadata:o,data:s},i,n)),r=this._getPartialQueryResponse(e.hash)),d=t._mergeResponseCacheMetadata(o,r),l=this._mergeResponseData(s,r),h.push(this._setQueryResponseCacheEntry(e.hash,{cacheMetadata:d,data:l},i,n))}if(i.awaitDataCaching&&await Promise.all(h),((e,t)=>!t.hasDeferOrStream&&!e.hasNext&&!e.paths)(r,n)&&d&&l)return{cacheMetadata:d,data:l}}const{data:o,hasNext:d,paths:l}=r;return{cacheMetadata:this._buildCacheMetadata(e,r,i,n),data:o,hasNext:d,paths:l}}async _checkCacheEntry(e,a,s,i){try{const n=await this._hasCacheEntry(e,a);if(!n||!t._isValid(n))return!1;const r=await this._getCacheEntry(e,a,s,i);return!H(r)&&{cacheability:n,entry:r}}catch{return!1}}_createCacheMetadata({_cacheMetadata:e,headers:t},{operation:a}){const s=new Map,i=(({_cacheMetadata:e,fallback:t,headers:a})=>{if(e&&!Q(e)){const[t,...a]=Object.values(e);return new I({metadata:a.reduce(((e,t)=>e?t.ttl<e.ttl?t:e:t),t)})}return a?.has("cache-control")?new I({headers:a}):new I({cacheControl:t})})({_cacheMetadata:e,fallback:this._fallbackOperationCacheability,headers:t});return s.set(a,i),e&&M(e,s),s}async _getCacheEntry(e,t,a,s){return this._cache.get(`${e}::${t}`)}_getPartialQueryResponse(e){const t=this._partialQueryResponses.get(e);return this._partialQueryResponses.delete(e),t}async _hasCacheEntry(e,t){try{return await this._cache.has(`${e}::${t}`)}catch{return!1}}_mergeResponseData(e,t){return t?J(t.data,e,this._typeIDKey):e}async _parseEntityAndRequestFieldPathCacheEntryData(e,t,{cacheMetadata:a,entityData:s,requestFieldPathData:i},n,r){const c=_(e,t,r),{hashedRequestFieldCacheKey:d,requestFieldCacheKey:l,requestFieldPath:y,responseDataPath:u}=c,f=z(i,u),m=r.fieldTypeMap.get(y),C=a.get(y);if(!p(f)&&!m?.hasDirectives)return;if(p(f)){const t=[];K(e,f,r.fragmentDefinitions,((e,c,h,o,d)=>{t.push(this._parseEntityAndRequestFieldPathCacheEntryData(e,{index:d,requestFieldCacheKey:l,requestFieldPath:y,responseDataPath:u},{cacheMetadata:a,entityData:s,requestFieldPathData:i},n,r))})),await Promise.all(t)}if(H(f)||!m||!C)return;const g=Z(f,m,this._typeIDKey),D=!!m.hasArguments||!!m.hasDirectives;r.operation===A.QUERY&&(g||D)&&(await this._setRequestFieldPathCacheEntry(c,{cacheability:C,fieldData:ee(structuredClone(f),e,c,r),fieldTypeInfo:m},n,r),P(e,{fragmentDefinitions:r.fragmentDefinitions})&&(g?S(i,u,{__cacheKey:`${h}::${d}`}):G(i,u))),g&&(await this._setEntityCacheEntry({cacheability:C,fieldData:te(structuredClone(z(s,u)),e,c,r),fieldTypeInfo:m},n,r),S(s,u,{__cacheKey:`${o}::${X(f,m,r)}`}))}async _retrieveCachedEntityData(e,{possibleTypes:t,typeName:a},s,i){const n=[...t.map((e=>e.typeName)),a],r=(await Promise.all(n.map((t=>this._checkCacheEntry(o,`${t}::${e}`,s,i))))).filter((e=>!!e));let c;return 1===r.length?c=r[0]:r.length>1&&(r.sort((({cacheability:e},{cacheability:t})=>e.metadata.ttl-t.metadata.ttl)),c={cacheability:r[0].cacheability,entry:r.reduce(((e,{entry:t})=>J(e,t,this._typeIDKey)),{})}),c??{}}async _retrieveCachedParentNodeData({entityData:e,requestFieldPathData:a},{hashedRequestFieldCacheKey:s,propNameOrIndex:i,requestFieldCacheKey:n},r,c,h){let o,l=t._getFieldDataFromAncestor(e,i),y=t._getFieldDataFromAncestor(a,i);if(t._isNodeRequestFieldPath(r)){const{cacheability:e,entry:t}=await this._retrieveCachedRequestFieldPathData(s,n,c,h);y=W(y,t,this._typeIDKey),e&&(o=e)}const p=((e,{typeIDValue:t},a)=>t||(d(e)?e[a]:void 0))(y,r,this._typeIDKey);if(t._isNodeEntity(r)&&p){const{cacheability:e,entry:t}=await this._retrieveCachedEntityData(p,r,c,h);l=W(l,t,this._typeIDKey),e&&(!o||e.metadata.ttl>o.metadata.ttl)&&(o=e)}return{cacheability:o,data:L(l,y)?l:W(l,y,this._typeIDKey),entityData:l,requestFieldPathData:y}}async _retrieveCachedRequestFieldPathData(e,t,a,s){return await this._checkCacheEntry(h,e,a,{...s,requestFieldCacheKey:t})||{}}async _retrieveCachedResponseData({ast:e},a,s){const i={cacheMetadata:new Map,data:{},fieldCount:{missing:0,total:0},fieldPathChecklist:new Map},n=N(e,s.operation)[0];if(!n)return i;const r=C(n);return r?(await Promise.all(r.map((({fieldNode:e})=>this._analyzeFieldNode(e,{requestFieldPath:s.operation},i,a,s)))),i.fieldCount=t._countFieldPathChecklist(i.fieldPathChecklist),i):i}_retrieveResponseDataForCaching(e,t){const a=this._responseChunksAwaitingCaching.get(t.requestID);return this._responseChunksAwaitingCaching.delete(t.requestID),(s=[...a,e]).reduce(((e,t,a)=>{const{_cacheMetadata:i,data:n,hasNext:r,headers:c,paths:h}=t;return i&&(e._cacheMetadata=e._cacheMetadata?{...e._cacheMetadata,...i}:i),e.data=j(e.data,n),0===a&&(e.headers=c),a===s.length-1&&(e.hasNext=r),h&&(e.paths||(e.paths=[]),e.paths.push(...h)),e}),{data:{}});var s}async _setCacheEntry(e,t,a,s,i,n){try{await this._cache.set(`${e}::${t}`,structuredClone(a),s)}catch{}}async _setEntityAndRequestFieldPathCacheEntries(e,t,a,s){const i=N(e.ast,s.operation)[0];if(!i)return;const n=C(i);n&&await Promise.all(n.map((({fieldNode:e})=>this._parseEntityAndRequestFieldPathCacheEntryData(e,{requestFieldPath:s.operation},t,a,s))))}async _setEntityCacheEntry({cacheability:e,fieldData:t,fieldTypeInfo:a},s,i){const n=`${a.isEntity?a.typeName:t.__typename}::${String(t[this._typeIDKey])}`,r=await this._checkCacheEntry(o,n,s,i);r&&(t=J(r.entry,t,this._typeIDKey)),await this._setCacheEntry(o,n,t,{cacheHeaders:{cacheControl:e.printCacheControl()},tag:s.tag},s,i)}_setFieldCacheability(e,t,{cacheMetadata:a,data:s},i,n){const{requestFieldPath:r}=t,c=_(e,t,n),{requestFieldPath:h,responseDataPath:o}=c;if(!p(s))return;const d=z(s,o),l=n.fieldTypeMap.get(h);(p(d)||l?.hasDirectives)&&(this._setFieldTypeCacheDirective(a,{ancestorRequestFieldPath:r,requestFieldPath:h},n),p(d)&&K(e,d,n.fragmentDefinitions,((e,t,r,c,d)=>{this._setFieldCacheability(e,{index:d,requestFieldPath:h,responseDataPath:o},{cacheMetadata:a,data:s},i,n)})))}_setFieldTypeCacheDirective(e,{ancestorRequestFieldPath:a,requestFieldPath:s},{fieldTypeMap:i,operation:n}){if(e.has(s))return;const r=i.get(s);if(r&&this._typeCacheDirectives[r.typeName]){const a=new I({cacheControl:this._typeCacheDirectives[r.typeName]});t._setCacheMetadata(e,a,s,n)}else this._cascadeCacheControl&&a&&t._setCacheMetadata(e,e.get(a),s,n)}_setPartialQueryResponse(e,t,a,s){this._partialQueryResponses.set(e,t)}async _setQueryResponseCacheEntry(e,{cacheMetadata:a,data:s},i,n){const r=v(a),h=t._getOperationCacheControl(a,n.operation);await this._setCacheEntry(c,e,{cacheMetadata:r,data:s},{cacheHeaders:{cacheControl:h},tag:i.tag},i,n)}async _setRequestFieldPathCacheEntry(e,{cacheability:t,fieldData:a},s,i){const{hashedRequestFieldCacheKey:n,requestFieldCacheKey:r}=e,c=await this._checkCacheEntry(h,n,s,{...i,requestFieldCacheKey:r});c&&p(c.entry)&&p(a)&&(a=J(c.entry,a,this._typeIDKey)),this._setCacheEntry(h,n,a,{cacheHeaders:{cacheControl:t.printCacheControl()},tag:s.tag},s,{...i,requestFieldCacheKey:r})}_setResponseChunksAwaitingCaching(e,t){const a=this._responseChunksAwaitingCaching.get(t.requestID);a?this._responseChunksAwaitingCaching.set(t.requestID,[...a,e]):this._responseChunksAwaitingCaching.set(t.requestID,[e])}},t(de.prototype,"_getCacheEntry",[ce],Object.getOwnPropertyDescriptor(de.prototype,"_getCacheEntry"),de.prototype),t(de.prototype,"_setCacheEntry",[he],Object.getOwnPropertyDescriptor(de.prototype,"_setCacheEntry"),de.prototype),t(de.prototype,"_setPartialQueryResponse",[oe],Object.getOwnPropertyDescriptor(de.prototype,"_setPartialQueryResponse"),de.prototype),de);export{le as CacheManager};
1
+ import e from"@babel/runtime/helpers/defineProperty";import t from"@babel/runtime/helpers/applyDecoratedDescriptor";import"core-js/modules/web.structured-clone.js";import"core-js/modules/es.array.push.js";import{CACHE_ENTRY_ADDED as a,CACHE_ENTRY_QUERIED as s,PARTIAL_QUERY_COMPILED as i,TYPE_NAME_KEY as n,DEFAULT_TYPE_ID_KEY as r,QUERY_RESPONSES as c,REQUEST_FIELD_PATHS as h,DATA_ENTITIES as o}from"@graphql-box/core";import{isPlainObject as d,isArray as l,mergeObjects as y,isObjectLike as p,resolveFragments as u,getName as f,buildFieldKeysAndPaths as _,deleteFragmentSpreads as m,getChildFields as C,deleteChildFields as g,getInlineFragments as D,deleteInlineFragments as F,hasChildFields as P,deleteFragmentDefinitions as q,getOperationDefinitions as E,ArgsError as b,GroupedError as R,getFragmentDefinitions as N,hashRequest as w,rehydrateCacheMetadata as M,iterateChildFields as v,dehydrateCacheMetadata as K}from"@graphql-box/helpers";import{Cacheability as T}from"cacheability";import{Kind as I,print as k,OperationTypeNode as A}from"graphql";import{isNumber as x,isString as O,isEmpty as Q,keys as $,merge as S,set as j,get as z,isUndefined as H,assign as U,unset as G,isEqual as L}from"lodash-es";const V="noCache",Y=(e,t)=>l(e)?e.reduce(((e,a)=>!!e&&Y(a,t)),!0):!!d(e)&&B(e,t),B=(e,t)=>{const a=d(e)?Object.keys(e):[...e.keys()];return!(1!==a.length||!d(e)||!e[t])||a.reduce(((a,s)=>!!a&&(x(s)&&l(e)||O(s)&&d(e)?Y(e[s],t):a)),!0)},J=({entity:e,queryResponse:t,requestPath:a})=>!e&&!t&&!a,W=e=>!!e.entity,X=({entity:e,requestPath:t})=>!!(e??t),Z=e=>!!e.queryResponse,ee=e=>!!e.requestPath,te=(e,t,a)=>y(e,t,((e,t)=>d(t)&&t[a]?t[a]:void 0)),ae=(e,t,a)=>!e&&t?t:p(e)&&p(t)?te(e,t,a):e,se=(e,t,a)=>`${t.isEntity?t.typeName:e.__typename}::${String(e[a.typeIDKey])}`,ie=(e,t,a)=>{if(!d(e)||!(a in e))return!1;const{isEntity:s=!1,possibleTypes:i=[]}=t??{};return!!s||0!==i.length&&i.some((t=>t.typeName===e.__typename))},ne=(e,t,a,s)=>{if(!d(e))return e;const i=u(t.selectionSet?.selections,s.fragmentDefinitions);return $(e).reduce(((t,n)=>{const r=i.find((({fieldNode:e})=>f(e)===n));if(r){const t=_(r.fieldNode,a,s),i=s.fieldTypeMap.get(t.requestFieldPath);(ie(e[n],i,s.typeIDKey)||i?.hasArguments||i?.hasDirectives)&&delete e[n]}return t}),e)},re=(e,t,a,s)=>{const i=u(t.selectionSet?.selections,s.fragmentDefinitions);return $(e).reduce(((t,n)=>{const r=i.find((({fieldNode:e})=>f(e)===n));if(r){const{requestFieldPath:t}=_(r.fieldNode,a,s),i=s.fieldTypeMap.get(t);(ie(e[n],i,s.typeIDKey)||i?.hasArguments)&&delete e[n]}return t}),e)},ce=(e,t)=>{if(!e||0===e.length)return{hasData:!1,typeUnused:!!t};const[a]=e;if(a){const{hasData:e,typeName:s}=a;return{hasData:e,typeUnused:s?s!==t:void 0}}return{hasData:e.some((({hasData:e,typeName:a})=>a===t&&e)),typeUnused:!e.every((({typeName:e})=>e===t))}},he=(e,t,a,s,i)=>{const{fragmentDefinitions:r,typeIDKey:c}=i,h=C(e,{fragmentDefinitions:r});if(!h)return!1;const o={};for(let d=h.length-1;d>=0;d-=1){const l=h[d];if(!l)continue;const{fieldNode:y,fragmentKind:p,fragmentName:u,typeName:m}=l;p===I.FRAGMENT_SPREAD&&u&&!o[u]&&(o[u]={hasData:0,total:r?.[u]?C(r[u],{fragmentDefinitions:r})?.length??0:0});const D=f(y);if(D===c||D===n)continue;const{requestFieldPath:F}=_(y,{requestFieldPath:s},i),{hasData:q,typeUnused:E}=ce(t.get(F),m);if(q||E)if(p===I.FRAGMENT_SPREAD&&u){const e=o[u];e&&(e.hasData+=1)}else P(y,{fragmentDefinitions:r})?he(y,t,a,F,i)&&g(e,y):g(e,y)}return((e,t,a,s)=>{if(!Q(t))for(const i of Object.keys(t)){const n=a[i];if(!n)continue;n.paths.push(s);const{hasData:r,total:c}=t[i];r===c&&(m(e,i),n.deleted+=1)}})(e,o,a,s),((e,{fragmentDefinitions:t,typeIDKey:a})=>{const s=D(e);let i=!1;for(const n of s){const s=C(n,{fragmentDefinitions:t});if(!s||0===s.length){F(e,n),i=!0;continue}const[r]=s;if(r){const{fieldNode:t}=r;f(t)===a&&(F(e,n),i=!0)}}})(e,i),((e,{fragmentDefinitions:t,typeIDKey:a})=>{const s=C(e,{fragmentDefinitions:t});if(!s||s.length>3)return!1;const i=s.map((({fieldNode:e})=>f(e)));if(2===i.length&&i.every((e=>e===a||e===n)))return g(e,s.map((({fieldNode:e})=>e))),!0;if(1===i.length&&i[0]===a||i[0]===n){const[t]=s;if(t){const{fieldNode:a}=t;return g(e,a),!0}}})(e,i),!P(e,{fragmentDefinitions:r})},oe=(e,{fieldPathChecklist:t},a)=>{const{ast:s}=e,i=E(s,a.operation)[0];if(!i)return s;const{fragmentDefinitions:n,operation:r}=a,c=C(i,{fragmentDefinitions:n});if(!c)return s;const h=(({request:e},{fragmentDefinitions:t})=>$(t??{}).reduce(((t,a)=>(t[a]={deleted:0,paths:[],total:(e.match(new RegExp(`\\.\\.\\.${a}`,"g"))??[]).length},t)),{}))(e,a);for(let e=c.length-1;e>=0;e-=1){const{fieldNode:s}=c[e],{requestFieldPath:n}=_(s,{requestFieldPath:r},a);he(s,t,h,n,a)&&g(i,s)}return a.queryFiltered=!0,((e,t,a,s)=>{const i=$(a).reduce(((e,t)=>{const s=a[t];if(!s)return e;const{deleted:i,paths:n,total:r}=s;return 0===i&&1===r?[...e,{name:t,path:n[0]}]:e}),[]),{fragmentDefinitions:n={}}=s;for(const{name:e,path:r}of i){const i=n[e];i&&he(i,t,a,r,s)}const r=$(a).reduce(((e,t)=>{const s=a[t];if(!s)return e;const{deleted:i,total:n}=s;return i>0&&i===n?[...e,t]:e}),[]);return 0===r.length?e:q(e,{include:r})})(s,t,h,a)},de=(e,t)=>l(e)&&x(t)||d(e)&&O(t)?e[t]:void 0,le=e=>!!p(e)&&(n in e&&O(e[n]));var ye,pe,ue,fe;let _e=(ye=(e,t,a)=>{const i=a.value;i&&(a.value=async function(...e){return new Promise((t=>{(async()=>{const{debugManager:a,requestFieldCacheKey:n,...r}=e[3];if(!a)return void t(await i.apply(this,e));const c=a.now(),h=await i.apply(this,e),o=a.now(),d=o-c;t(h);const l={cacheType:e[0],context:r,options:e[2],requestHash:e[1],result:h,stats:{duration:d,endTime:o,startTime:c},...n?{decryptedCacheKey:n}:{}};a.log(s,l)})()}))})},pe=(e,t,s)=>{const i=s.value;i&&(s.value=async function(...e){return new Promise((t=>{(async()=>{const{debugManager:s,requestFieldCacheKey:n,...r}=e[5];if(!s)return await i.apply(this,e),void t();const c=s.now();await i.apply(this,e);const h=s.now(),o=h-c;t();const d={cacheType:e[0],cachemapOptions:e[3],context:r,options:e[4],requestHash:e[1],stats:{duration:o,endTime:h,startTime:c},value:e[2],...n?{decryptedCacheKey:n}:{}};s.log(a,d)})()}))})},ue=(e,t,a)=>{const s=a.value;s&&(a.value=function(...e){const{debugManager:t,...a}=e[3];if(!t)return void s.apply(this,e);const n=t.now();s.apply(this,e);const r=t.now(),c=r-n;t.log(i,{context:a,options:e[2],requestHash:e[0],result:e[1],stats:{duration:c,endTime:r,startTime:n}})})},fe=class t{static _countFieldPathChecklist(e){const t={missing:0,total:0};for(const[,a]of e){t.total+=a.length;const e=a.filter((({hasData:e})=>!e));t.missing+=e.length}return t}static _getFieldDataFromAncestor(e,t){const a=de(e,t);return p(a)?structuredClone(a):a}static _getOperationCacheControl(e,t){const a="no-cache";if(!e)return a;const s=e.get(t);return s?s.printCacheControl():a}static _isNodeEntity(e){if(!e)return!1;const{isEntity:t,possibleTypes:a}=e;return t||a.some((e=>!!e.isEntity))}static _isNodeRequestFieldPath(e){return!!e&&(this._isNodeEntity(e)||e.hasArguments||e.hasDirectives)}static _isValid(e){return!z(e,["metadata","cacheControl",V],!1)&&e.checkTTL()}static _mergeResponseCacheMetadata(e,t){return t?new Map([...t.cacheMetadata,...e]):e}static _setCachedData(e,{data:t},a){const s=t=>{(l(e)&&x(a)||d(e))&&(e[a]=t)};p(t)||H(t)?l(t)?s([]):d(t)&&s({}):s(t)}static _setCachedResponseSlice(e,{cacheMetadata:a,data:s,fieldPathChecklist:i},{propNameOrIndex:n,requestFieldPath:r},c,h,{operation:o}){t._setCacheMetadata(a,e.cacheability,r,o),t._setFieldPathChecklist(i,e,r,c),t._setCachedData(s,e,n)}static _setCacheMetadata(e,t,a,s){if(!t)return;e.set(a,t);const i=e.get(s);(!i||i.metadata.ttl>t.metadata.ttl)&&e.set(s,t)}static _setFieldPathChecklist(e,{data:t},a,{dataTypename:s,fieldTypename:i,fragmentKind:n,fragmentName:r}){if(H(i)||n===I.FRAGMENT_SPREAD){if(e.has(a))return;return void e.set(a,[{fragmentKind:n,fragmentName:r,hasData:!H(t)}])}if(s!==i)return;const c=e.get(a)??[];c.some((({typeName:e})=>e===s))||e.set(a,[...c,{fragmentKind:n,fragmentName:r,hasData:!H(t),typeName:s}])}constructor(t){e(this,"_cache",void 0),e(this,"_cacheTiersEnabled",{entity:!0,queryResponse:!0,requestPath:!0}),e(this,"_cascadeCacheControl",void 0),e(this,"_fallbackOperationCacheability",void 0),e(this,"_partialQueryResponses",new Map),e(this,"_responseChunksAwaitingCaching",new Map),e(this,"_typeCacheDirectives",void 0),e(this,"_typeIDKey",void 0);const a=[];if("cache"in t||a.push(new b("@graphql-box/cache-manager expected cache to be in options.")),t.typeCacheDirectives&&!d(t.typeCacheDirectives)){const e="@graphql-box/cache-manager expected options.typeCacheDirectives to be a plain object.";a.push(new b(e))}if(a.length>0)throw new R("@graphql-box/cache-manager argument validation errors.",a);this._cache=t.cache,this._cacheTiersEnabled={...this._cacheTiersEnabled,...t.cacheTiersEnabled},this._cascadeCacheControl=t.cascadeCacheControl??!1,this._fallbackOperationCacheability=t.fallbackOperationCacheability??V,this._typeCacheDirectives=t.typeCacheDirectives??{},this._typeIDKey=t.typeIDKey??r}async analyzeQuery(e,t,a){if(!X(this._cacheTiersEnabled))return{updated:e};const{ast:s,hash:i}=e,n={...a,fragmentDefinitions:N(s),typeIDKey:this._typeIDKey},r=await this._retrieveCachedResponseData(e,t,n),{cacheMetadata:c,data:h,fieldCount:o}=r;if(o.missing===o.total||((e,t)=>B(e,t))(h,this._typeIDKey))return{updated:e};if(!o.missing&&Z(this._cacheTiersEnabled)){const e=this._setQueryResponseCacheEntry(i,{cacheMetadata:c,data:h},t,n);return t.awaitDataCaching&&await e,{response:{cacheMetadata:c,data:h}}}const d=oe(e,r,n),l=k(d),{fragmentDefinitions:y,typeIDKey:p,...u}=n;return U(a,{...u,filteredRequest:l}),Z(this._cacheTiersEnabled)&&this._setPartialQueryResponse(i,{cacheMetadata:c,data:h},t,a),{updated:{ast:d,hash:w(l),request:l}}}get cache(){return this._cache}async cacheQuery(e,t,a,s,i){const n={...i,fragmentDefinitions:N((t??e).ast),typeIDKey:this._typeIDKey};return this._cacheResponse(e,t,a,s,n)}async cacheResponse(e,t,a,s){const i={...s,fragmentDefinitions:N(e.ast),typeIDKey:this._typeIDKey};return this._cacheResponse(e,void 0,t,a,i)}async checkCacheEntry(e,t,a,s){return!J(this._cacheTiersEnabled)&&this._checkCacheEntry(e,t,a,s)}async checkQueryResponseCacheEntry(e,t,a){if(!Z(this._cacheTiersEnabled))return!1;const s=await this._checkCacheEntry(c,e,t,a);if(!s)return!1;const{cacheMetadata:i,data:n}=s.entry;return{cacheMetadata:M(i),data:n}}deletePartialQueryResponse(e){Z(this._cacheTiersEnabled)&&this._partialQueryResponses.delete(e)}async setQueryResponseCacheEntry(e,t,a,s){if(Z(this._cacheTiersEnabled))return this._setQueryResponseCacheEntry(e.hash,t,a,s)}async _analyzeFieldNode(e,t,a,s,i){await(P(e,{fragmentDefinitions:i.fragmentDefinitions})?this._analyzeParentFieldNode(e,t,a,s,i):this._analyzeLeafFieldNode(e,t,a,s,i))}async _analyzeLeafFieldNode(e,a,s,i,n){const r=_(e,a,n),{hashedRequestFieldCacheKey:c,propNameOrIndex:h,requestFieldCacheKey:o,requestFieldPath:d}=r,l=n.fieldTypeMap.get(d),{entityData:y,fragmentKind:p,fragmentName:u,requestFieldPathData:f,typeName:m}=a,C={dataTypename:le(y)?y.__typename:le(f)?f.__typename:void 0,fieldTypename:m,fragmentKind:p,fragmentName:u};if(t._isNodeRequestFieldPath(l)){if(ee(this._cacheTiersEnabled)){const{cacheability:e,entry:a}=await this._retrieveCachedRequestFieldPathData(c,o,i,n);t._setCachedResponseSlice({cacheability:e,data:a},s,r,C,i,n)}}else{const e=t._getFieldDataFromAncestor(y,h)??t._getFieldDataFromAncestor(f,h);t._setFieldPathChecklist(s.fieldPathChecklist,{data:e},d,C),t._setCachedData(s.data,{data:e},h)}}async _analyzeParentFieldNode(e,a,s,i,r){const c=_(e,a,r),{propNameOrIndex:h,requestFieldCacheKey:o,requestFieldPath:d}=c,l=r.fieldTypeMap.get(d),{cacheability:y,data:u,entityData:f,requestFieldPathData:m}=await this._retrieveCachedParentNodeData(a,c,l,i,r),{fragmentKind:C,fragmentName:g,typeName:D}=a;if(t._setCachedResponseSlice({cacheability:y,data:u},s,c,{dataTypename:z(u,n),fieldTypename:D,fragmentKind:C,fragmentName:g},i,r),!p(u))return;const F=[];v(e,u,r.fragmentDefinitions,((e,t,a,n,c)=>{F.push(this._analyzeFieldNode(e,{cacheability:y,entityData:f,fragmentKind:a,fragmentName:n,index:c,requestFieldCacheKey:o,requestFieldPath:d,requestFieldPathData:m,typeName:t},{...s,data:de(s.data,h)},i,r))})),await Promise.all(F)}_buildCacheMetadata({ast:e},{data:t,...a},s,i){const n=this._createCacheMetadata({data:t,...a},i),r=E(e,i.operation)[0];if(!r)return n;const c=C(r);if(!c)return n;for(const{fieldNode:e}of c)this._setFieldCacheability(e,{requestFieldPath:i.operation},{cacheMetadata:n,data:t},s,i);return n}async _cacheResponse(e,a,s,i,n){const r=((e,t)=>{if(!t.normalizePatchResponseData)return e;const{data:a,paths:s,...i}=e;return s?.length&&O(s[0])?{...i,data:j({},s[0],a),paths:s}:e})(s,n);let c=r;((e,t)=>t.hasDeferOrStream&&e.hasNext)(s,n)&&(this._setResponseChunksAwaitingCaching(r,n),c=void 0),((e,t)=>t.hasDeferOrStream&&!e.hasNext&&e.paths)(s,n)&&(c=this._retrieveResponseDataForCaching(r,n));const h=[];if(c&&(o=this._cacheTiersEnabled,!J(o))){const{data:s}=c,o=this._buildCacheMetadata(e,c,i,n);let d,l;if(X(this._cacheTiersEnabled)&&h.push(this._setEntityAndRequestFieldPathCacheEntries(e,{cacheMetadata:o,entityData:structuredClone(s),requestFieldPathData:structuredClone(s)},i,n)),n.operation===A.QUERY&&Z(this._cacheTiersEnabled)){let r;n.queryFiltered&&a&&(h.push(this._setQueryResponseCacheEntry(a.hash,{cacheMetadata:o,data:s},i,n)),r=this._getPartialQueryResponse(e.hash)),d=t._mergeResponseCacheMetadata(o,r),l=this._mergeResponseData(s,r),h.push(this._setQueryResponseCacheEntry(e.hash,{cacheMetadata:d,data:l},i,n))}if(i.awaitDataCaching&&await Promise.all(h),((e,t)=>!t.hasDeferOrStream&&!e.hasNext&&!e.paths)(r,n)&&d&&l)return{cacheMetadata:d,data:l}}var o;const{data:d,hasNext:l,paths:y}=r;return{cacheMetadata:this._buildCacheMetadata(e,r,i,n),data:d,hasNext:l,paths:y}}async _checkCacheEntry(e,a,s,i){try{const n=await this._hasCacheEntry(e,a);if(!n||!t._isValid(n))return!1;const r=await this._getCacheEntry(e,a,s,i);return!H(r)&&{cacheability:n,entry:r}}catch{return!1}}_createCacheMetadata({_cacheMetadata:e,headers:t},{operation:a}){const s=new Map,i=(({_cacheMetadata:e,fallback:t,headers:a})=>{if(e&&!Q(e)){const[t,...a]=Object.values(e);return new T({metadata:a.reduce(((e,t)=>e?t.ttl<e.ttl?t:e:t),t)})}return a?.has("cache-control")?new T({headers:a}):new T({cacheControl:t})})({_cacheMetadata:e,fallback:this._fallbackOperationCacheability,headers:t});return s.set(a,i),e&&M(e,s),s}async _getCacheEntry(e,t,a,s){return this._cache.get(`${e}::${t}`)}_getPartialQueryResponse(e){const t=this._partialQueryResponses.get(e);return this._partialQueryResponses.delete(e),t}async _hasCacheEntry(e,t){try{return await this._cache.has(`${e}::${t}`)}catch{return!1}}_mergeResponseData(e,t){return t?te(t.data,e,this._typeIDKey):e}async _parseEntityAndRequestFieldPathCacheEntryData(e,t,{cacheMetadata:a,entityData:s,requestFieldPathData:i},n,r){const c=_(e,t,r),{hashedRequestFieldCacheKey:d,requestFieldCacheKey:l,requestFieldPath:y,responseDataPath:u}=c,f=z(i,u),m=r.fieldTypeMap.get(y),C=a.get(y);if(!p(f)&&!m?.hasDirectives)return;if(p(f)){const t=[];v(e,f,r.fragmentDefinitions,((e,c,h,o,d)=>{t.push(this._parseEntityAndRequestFieldPathCacheEntryData(e,{index:d,requestFieldCacheKey:l,requestFieldPath:y,responseDataPath:u},{cacheMetadata:a,entityData:s,requestFieldPathData:i},n,r))})),await Promise.all(t)}if(H(f)||!m||!C)return;const g=ie(f,m,this._typeIDKey),D=!!m.hasArguments||!!m.hasDirectives;r.operation===A.QUERY&&(g||D)&&ee(this._cacheTiersEnabled)&&(await this._setRequestFieldPathCacheEntry(c,{cacheability:C,fieldData:ne(structuredClone(f),e,c,r),fieldTypeInfo:m},n,r),P(e,{fragmentDefinitions:r.fragmentDefinitions})&&(g?j(i,u,{__cacheKey:`${h}::${d}`}):G(i,u))),g&&W(this._cacheTiersEnabled)&&(await this._setEntityCacheEntry({cacheability:C,fieldData:re(structuredClone(z(s,u)),e,c,r),fieldTypeInfo:m},n,r),j(s,u,{__cacheKey:`${o}::${se(f,m,r)}`}))}async _retrieveCachedEntityData(e,{possibleTypes:t,typeName:a},s,i){const n=[...t.map((e=>e.typeName)),a],r=(await Promise.all(n.map((t=>this._checkCacheEntry(o,`${t}::${e}`,s,i))))).filter((e=>!!e));let c;return 1===r.length?c=r[0]:r.length>1&&(r.sort((({cacheability:e},{cacheability:t})=>e.metadata.ttl-t.metadata.ttl)),c={cacheability:r[0].cacheability,entry:r.reduce(((e,{entry:t})=>te(e,t,this._typeIDKey)),{})}),c??{}}async _retrieveCachedParentNodeData({entityData:e,requestFieldPathData:a},{hashedRequestFieldCacheKey:s,propNameOrIndex:i,requestFieldCacheKey:n},r,c,h){let o,l=t._getFieldDataFromAncestor(e,i),y=t._getFieldDataFromAncestor(a,i);if(t._isNodeRequestFieldPath(r)&&ee(this._cacheTiersEnabled)){const{cacheability:e,entry:t}=await this._retrieveCachedRequestFieldPathData(s,n,c,h);y=ae(y,t,this._typeIDKey),e&&(o=e)}const p=((e,{typeIDValue:t},a)=>t||(d(e)?e[a]:void 0))(y,r,this._typeIDKey);if(t._isNodeEntity(r)&&p&&W(this._cacheTiersEnabled)){const{cacheability:e,entry:t}=await this._retrieveCachedEntityData(p,r,c,h);l=ae(l,t,this._typeIDKey),e&&(!o||e.metadata.ttl>o.metadata.ttl)&&(o=e)}return{cacheability:o,data:L(l,y)?l:ae(l,y,this._typeIDKey),entityData:l,requestFieldPathData:y}}async _retrieveCachedRequestFieldPathData(e,t,a,s){return await this._checkCacheEntry(h,e,a,{...s,requestFieldCacheKey:t})||{}}async _retrieveCachedResponseData({ast:e},a,s){const i={cacheMetadata:new Map,data:{},fieldCount:{missing:0,total:0},fieldPathChecklist:new Map},n=E(e,s.operation)[0];if(!n)return i;const r=C(n);return r?(await Promise.all(r.map((({fieldNode:e})=>this._analyzeFieldNode(e,{requestFieldPath:s.operation},i,a,s)))),i.fieldCount=t._countFieldPathChecklist(i.fieldPathChecklist),i):i}_retrieveResponseDataForCaching(e,t){const a=this._responseChunksAwaitingCaching.get(t.requestID);return this._responseChunksAwaitingCaching.delete(t.requestID),(s=[...a,e]).reduce(((e,t,a)=>{const{_cacheMetadata:i,data:n,hasNext:r,headers:c,paths:h}=t;return i&&(e._cacheMetadata=e._cacheMetadata?{...e._cacheMetadata,...i}:i),e.data=S(e.data,n),0===a&&(e.headers=c),a===s.length-1&&(e.hasNext=r),h&&(e.paths||(e.paths=[]),e.paths.push(...h)),e}),{data:{}});var s}async _setCacheEntry(e,t,a,s,i,n){try{await this._cache.set(`${e}::${t}`,structuredClone(a),s)}catch{}}async _setEntityAndRequestFieldPathCacheEntries(e,t,a,s){const i=E(e.ast,s.operation)[0];if(!i)return;const n=C(i);n&&await Promise.all(n.map((({fieldNode:e})=>this._parseEntityAndRequestFieldPathCacheEntryData(e,{requestFieldPath:s.operation},t,a,s))))}async _setEntityCacheEntry({cacheability:e,fieldData:t,fieldTypeInfo:a},s,i){const n=`${a.isEntity?a.typeName:t.__typename}::${String(t[this._typeIDKey])}`,r=await this._checkCacheEntry(o,n,s,i);r&&(t=te(r.entry,t,this._typeIDKey)),await this._setCacheEntry(o,n,t,{cacheHeaders:{cacheControl:e.printCacheControl()},tag:s.tag},s,i)}_setFieldCacheability(e,t,{cacheMetadata:a,data:s},i,n){const{requestFieldPath:r}=t,c=_(e,t,n),{requestFieldPath:h,responseDataPath:o}=c;if(!p(s))return;const d=z(s,o),l=n.fieldTypeMap.get(h);(p(d)||l?.hasDirectives)&&(this._setFieldTypeCacheDirective(a,{ancestorRequestFieldPath:r,requestFieldPath:h},n),p(d)&&v(e,d,n.fragmentDefinitions,((e,t,r,c,d)=>{this._setFieldCacheability(e,{index:d,requestFieldPath:h,responseDataPath:o},{cacheMetadata:a,data:s},i,n)})))}_setFieldTypeCacheDirective(e,{ancestorRequestFieldPath:a,requestFieldPath:s},{fieldTypeMap:i,operation:n}){if(e.has(s))return;const r=i.get(s);if(r&&this._typeCacheDirectives[r.typeName]){const a=new T({cacheControl:this._typeCacheDirectives[r.typeName]});t._setCacheMetadata(e,a,s,n)}else this._cascadeCacheControl&&a&&t._setCacheMetadata(e,e.get(a),s,n)}_setPartialQueryResponse(e,t,a,s){this._partialQueryResponses.set(e,t)}async _setQueryResponseCacheEntry(e,{cacheMetadata:a,data:s},i,n){const r=K(a),h=t._getOperationCacheControl(a,n.operation);await this._setCacheEntry(c,e,{cacheMetadata:r,data:s},{cacheHeaders:{cacheControl:h},tag:i.tag},i,n)}async _setRequestFieldPathCacheEntry(e,{cacheability:t,fieldData:a},s,i){const{hashedRequestFieldCacheKey:n,requestFieldCacheKey:r}=e,c=await this._checkCacheEntry(h,n,s,{...i,requestFieldCacheKey:r});c&&p(c.entry)&&p(a)&&(a=te(c.entry,a,this._typeIDKey)),this._setCacheEntry(h,n,a,{cacheHeaders:{cacheControl:t.printCacheControl()},tag:s.tag},s,{...i,requestFieldCacheKey:r})}_setResponseChunksAwaitingCaching(e,t){const a=this._responseChunksAwaitingCaching.get(t.requestID);a?this._responseChunksAwaitingCaching.set(t.requestID,[...a,e]):this._responseChunksAwaitingCaching.set(t.requestID,[e])}},t(fe.prototype,"_getCacheEntry",[ye],Object.getOwnPropertyDescriptor(fe.prototype,"_getCacheEntry"),fe.prototype),t(fe.prototype,"_setCacheEntry",[pe],Object.getOwnPropertyDescriptor(fe.prototype,"_setCacheEntry"),fe.prototype),t(fe.prototype,"_setPartialQueryResponse",[ue],Object.getOwnPropertyDescriptor(fe.prototype,"_setPartialQueryResponse"),fe.prototype),fe);export{_e as CacheManager};
2
2
  //# sourceMappingURL=index.mjs.map