@bitblit/ratchet-graphql 4.0.159-alpha → 4.0.162-alpha

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.
Files changed (2) hide show
  1. package/lib/index.mjs +1 -1
  2. package/package.json +4 -4
package/lib/index.mjs CHANGED
@@ -1,2 +1,2 @@
1
- import{gql as e,InMemoryCache as t,ApolloClient as o}from"@apollo/client/core";import{HttpLink as n}from"@apollo/client/link/http/HttpLink.js";import{ApolloLink as r}from"@apollo/client/link/core/ApolloLink.js";import{LoggerLevelName as i,Logger as a,RequireRatchet as c,StringRatchet as s,ErrorRatchet as h}from"@bitblit/ratchet-common";import l from"cross-fetch";class d{constructor(){}static buildInformation(){return{version:"159",hash:"596a26211bc6f6f1e820c1e9f9fe01649b528f12",branch:"alpha-2023-06-09-1",tag:"alpha-2023-06-09-1",timeBuiltISO:"2023-06-09T10:05:30-0700",notes:"No notes"}}}class u{logLevel;rethrow;constructor(e=i.warn,t=!1){this.logLevel=e,this.rethrow=t}handleError(e,t,o,n){if(a.logByLevel(this.logLevel,"Graphql failed : %s : %s : Anon-%s : %j",e,t,n,o),this.rethrow)throw e}}class p{queryProvider;endpointProvider;jwtTokenProvider;errorHandler;apolloCache=new Map;noAuthApollo;cachedEndpoint;constructor(e,t,o,n=new u){this.queryProvider=e,this.endpointProvider=t,this.jwtTokenProvider=o,this.errorHandler=n,c.notNullOrUndefined(e,"queryProvider"),c.notNullOrUndefined(t,"endpointProvider"),c.notNullOrUndefined(o,"jwtTokenProvider"),c.notNullOrUndefined(n,"errorHandler"),this.cachedEndpoint=this.endpointProvider.fetchGraphqlEndpoint()}async fetchQueryAsGql(t){let o=null;const n=await this.queryProvider.fetchQueryText(t);return n?o=e(n):a.warn("Could not find requested query : %s",t),o}createAnonymousApi(){a.info("Creating anonymous apollo client");const e={link:new n({uri:this.cachedEndpoint}),cache:new t};return new o(e)}fetchApi(e){let t=null;const o=this.jwtTokenProvider.fetchJwtToken();if(this.checkIfEndpointChanged(),a.info("Fetch auth apollo client %s",s.obscure(s.trimToEmpty(o),2,2)),s.trimToNull(o)){if(a.debug("Fetching authd api"),this.apolloCache.has(o))a.debug("Fetching apollo client from cache");else{const e=this.createAuthApi(o);a.debug("Setting apollo cache for this token to %s",e),this.apolloCache.set(o,e)}t=this.apolloCache.get(o)}else a.debug("Fetching unauthd ap"),e?(this.noAuthApollo?a.debug("Fetching anonymous client from cache"):this.noAuthApollo=this.createAnonymousApi(),t=this.noAuthApollo):h.throwFormattedErr("Cannot fetch api - no token and runAnonymous is set to %s : -%s-",e,o);return a.debug("FetchApi returning %s",t),t}createAuthApi(e){a.info("Creating a new authenticated api for %s : %s",this.cachedEndpoint,s.obscure(e,2,2));let i=null;c.notNullUndefinedOrOnlyWhitespaceString(e,"jwtToken"),a.info("Creating auth apollo client %s",s.obscure(e,2,2));const h=new n({uri:this.cachedEndpoint}),l={link:new r(((t,o)=>(t.setContext({headers:{authorization:`Bearer ${e}`}}),o(t)))).concat(h),cache:new t};return i=new o(l),i}checkIfEndpointChanged(){const e=this.endpointProvider.fetchGraphqlEndpoint();e!==this.cachedEndpoint&&(a.info("Endpoint changed from %s to %s - clearing apollo caches"),this.apolloCache=new Map,this.noAuthApollo=null,this.cachedEndpoint=e)}clearCaches(){a.info("Clearing cached apollo"),this.apolloCache=new Map,this.noAuthApollo=null,this.cachedEndpoint=null}async executeQuery(e,t,o=!1){let n=null;try{const r=this.fetchApi(o);if(r){a.debug("API fetched for %s, fetching gql",e);const o=await this.fetchQueryAsGql(e);a.debug("API and GQL fetched for %s - running %s %s",e,o,r);const i=await r.query({query:o,variables:t,fetchPolicy:"no-cache"});a.silly("Query returned: %j",i);const c=Object.keys(i.data);1!==c.length&&h.throwFormattedErr("Unexpected number of keys : %s : %j",c.length,c),n=i.data[c[0]]}else h.throwFormattedErr("Cannot run - no api fetched")}catch(n){a.silly("Exception caught in executeQuery : %s %s %j %s",n,e,t,o,n),this.errorHandler.handleError(n,e,t,o)}return n}async executeMutate(e,t,o=!1){a.info("Mutate : %s : %j",e,t);let n=null;const r=this.fetchApi(o);try{if(r){const o=await this.fetchQueryAsGql(e),i=await r.mutate({mutation:o,variables:t,fetchPolicy:"no-cache"}),a=Object.keys(i.data);1!==a.length&&h.throwFormattedErr("Unexpected number of keys : %s : %j",a.length,a),n=i.data[a[0]]}else h.throwFormattedErr("Cannot run - no api fetched")}catch(n){this.errorHandler.handleError(n,e,t,o)}return n}}class f{pathTemplate;forcePathToLowerCase;cacheMap=new Map;constructor(e="assets/gql/{{QUERY_NAME}}.gql",t=!1){this.pathTemplate=e,this.forcePathToLowerCase=t}async fetchQueryText(e){let t=this.cacheMap.get(e.toLowerCase());if(!t){const o=this.forcePathToLowerCase?e.toLowerCase():e,n=this.pathTemplate.split("{{QUERY_NAME}}").join(o);a.info("Cache miss, loading %s from %s",e,n);const r=await l(n);t=s.trimToNull(await r.text()),t&&this.cacheMap.set(e.toLowerCase(),t)}return t}}export{u as DefaultGraphqlRatchetErrorHandler,p as GraphqlRatchet,f as LocalFetchQueryProvider,d as RatchetGraphqlInfo};
1
+ import{gql as e,InMemoryCache as t,ApolloClient as o}from"@apollo/client/core";import{HttpLink as n}from"@apollo/client/link/http/HttpLink.js";import{ApolloLink as r}from"@apollo/client/link/core/ApolloLink.js";import{LoggerLevelName as i,Logger as a,RequireRatchet as c,StringRatchet as s,ErrorRatchet as h}from"@bitblit/ratchet-common";import l from"cross-fetch";class d{constructor(){}static buildInformation(){return{version:"162",hash:"86497fe1e6bc434493115b4e8bbcb3eb529de458",branch:"alpha-2023-06-28-2",tag:"alpha-2023-06-28-2",timeBuiltISO:"2023-06-28T00:39:33-0700",notes:"No notes"}}}class u{logLevel;rethrow;constructor(e=i.warn,t=!1){this.logLevel=e,this.rethrow=t}handleError(e,t,o,n){if(a.logByLevel(this.logLevel,"Graphql failed : %s : %s : Anon-%s : %j",e,t,n,o),this.rethrow)throw e}}class p{queryProvider;endpointProvider;jwtTokenProvider;errorHandler;apolloCache=new Map;noAuthApollo;cachedEndpoint;constructor(e,t,o,n=new u){this.queryProvider=e,this.endpointProvider=t,this.jwtTokenProvider=o,this.errorHandler=n,c.notNullOrUndefined(e,"queryProvider"),c.notNullOrUndefined(t,"endpointProvider"),c.notNullOrUndefined(o,"jwtTokenProvider"),c.notNullOrUndefined(n,"errorHandler"),this.cachedEndpoint=this.endpointProvider.fetchGraphqlEndpoint()}async fetchQueryAsGql(t){let o=null;const n=await this.queryProvider.fetchQueryText(t);return n?o=e(n):a.warn("Could not find requested query : %s",t),o}createAnonymousApi(){a.info("Creating anonymous apollo client");const e={link:new n({uri:this.cachedEndpoint}),cache:new t};return new o(e)}fetchApi(e){let t=null;const o=this.jwtTokenProvider.fetchJwtToken();if(this.checkIfEndpointChanged(),a.info("Fetch auth apollo client %s",s.obscure(s.trimToEmpty(o),2,2)),s.trimToNull(o)){if(a.debug("Fetching authd api"),this.apolloCache.has(o))a.debug("Fetching apollo client from cache");else{const e=this.createAuthApi(o);a.debug("Setting apollo cache for this token to %s",e),this.apolloCache.set(o,e)}t=this.apolloCache.get(o)}else a.debug("Fetching unauthd ap"),e?(this.noAuthApollo?a.debug("Fetching anonymous client from cache"):this.noAuthApollo=this.createAnonymousApi(),t=this.noAuthApollo):h.throwFormattedErr("Cannot fetch api - no token and runAnonymous is set to %s : -%s-",e,o);return a.debug("FetchApi returning %s",t),t}createAuthApi(e){a.info("Creating a new authenticated api for %s : %s",this.cachedEndpoint,s.obscure(e,2,2));let i=null;c.notNullUndefinedOrOnlyWhitespaceString(e,"jwtToken"),a.info("Creating auth apollo client %s",s.obscure(e,2,2));const h=new n({uri:this.cachedEndpoint}),l={link:new r(((t,o)=>(t.setContext({headers:{authorization:`Bearer ${e}`}}),o(t)))).concat(h),cache:new t};return i=new o(l),i}checkIfEndpointChanged(){const e=this.endpointProvider.fetchGraphqlEndpoint();e!==this.cachedEndpoint&&(a.info("Endpoint changed from %s to %s - clearing apollo caches"),this.apolloCache=new Map,this.noAuthApollo=null,this.cachedEndpoint=e)}clearCaches(){a.info("Clearing cached apollo"),this.apolloCache=new Map,this.noAuthApollo=null,this.cachedEndpoint=null}async executeQuery(e,t,o=!1){let n=null;try{const r=this.fetchApi(o);if(r){a.debug("API fetched for %s, fetching gql",e);const o=await this.fetchQueryAsGql(e);a.debug("API and GQL fetched for %s - running %s %s",e,o,r);const i=await r.query({query:o,variables:t,fetchPolicy:"no-cache"});a.silly("Query returned: %j",i);const c=Object.keys(i.data);1!==c.length&&h.throwFormattedErr("Unexpected number of keys : %s : %j",c.length,c),n=i.data[c[0]]}else h.throwFormattedErr("Cannot run - no api fetched")}catch(n){a.silly("Exception caught in executeQuery : %s %s %j %s",n,e,t,o,n),this.errorHandler.handleError(n,e,t,o)}return n}async executeMutate(e,t,o=!1){a.info("Mutate : %s : %j",e,t);let n=null;const r=this.fetchApi(o);try{if(r){const o=await this.fetchQueryAsGql(e),i=await r.mutate({mutation:o,variables:t,fetchPolicy:"no-cache"}),a=Object.keys(i.data);1!==a.length&&h.throwFormattedErr("Unexpected number of keys : %s : %j",a.length,a),n=i.data[a[0]]}else h.throwFormattedErr("Cannot run - no api fetched")}catch(n){this.errorHandler.handleError(n,e,t,o)}return n}}class f{pathTemplate;forcePathToLowerCase;cacheMap=new Map;constructor(e="assets/gql/{{QUERY_NAME}}.gql",t=!1){this.pathTemplate=e,this.forcePathToLowerCase=t}async fetchQueryText(e){let t=this.cacheMap.get(e.toLowerCase());if(!t){const o=this.forcePathToLowerCase?e.toLowerCase():e,n=this.pathTemplate.split("{{QUERY_NAME}}").join(o);a.info("Cache miss, loading %s from %s",e,n);const r=await l(n);t=s.trimToNull(await r.text()),t&&this.cacheMap.set(e.toLowerCase(),t)}return t}}export{u as DefaultGraphqlRatchetErrorHandler,p as GraphqlRatchet,f as LocalFetchQueryProvider,d as RatchetGraphqlInfo};
2
2
  //# sourceMappingURL=index.mjs.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bitblit/ratchet-graphql",
3
- "version": "4.0.159-alpha",
3
+ "version": "4.0.162-alpha",
4
4
  "description": "Ratchet tools to simplify use of graphql",
5
5
  "sideEffects": false,
6
6
  "type": "module",
@@ -63,9 +63,9 @@
63
63
  },
64
64
  "license": "Apache-2.0",
65
65
  "dependencies": {
66
- "@apollo/client": "3.7.15",
67
- "@bitblit/ratchet-common": "4.0.159-alpha",
68
- "graphql": "16.6.0"
66
+ "@apollo/client": "3.7.16",
67
+ "@bitblit/ratchet-common": "4.0.162-alpha",
68
+ "graphql": "16.7.1"
69
69
  },
70
70
  "peerDependencies": {},
71
71
  "devDependencies": {}