@bitblit/ratchet-graphql 4.0.183-alpha → 4.0.185-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.
- package/lib/build/ratchet-graphql-info.d.ts +5 -0
- package/lib/graphql/graphql-ratchet.d.ts +22 -0
- package/lib/graphql/provider/default-graphql-ratchet-error-handler.d.ts +8 -0
- package/lib/graphql/provider/graphql-ratchet-endpoint-provider.d.ts +3 -0
- package/lib/graphql/provider/graphql-ratchet-error-handler.d.ts +3 -0
- package/lib/graphql/provider/graphql-ratchet-jwt-token-provider.d.ts +3 -0
- package/lib/graphql/provider/graphql-ratchet-query-provider.d.ts +3 -0
- package/lib/graphql/provider/local-fetch-query-provider.d.ts +8 -0
- package/lib/index.mjs +1 -1
- package/package.json +4 -4
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { GraphqlRatchetEndpointProvider } from './provider/graphql-ratchet-endpoint-provider.js';
|
|
2
|
+
import { GraphqlRatchetJwtTokenProvider } from './provider/graphql-ratchet-jwt-token-provider.js';
|
|
3
|
+
import { GraphqlRatchetQueryProvider } from './provider/graphql-ratchet-query-provider.js';
|
|
4
|
+
import { GraphqlRatchetErrorHandler } from './provider/graphql-ratchet-error-handler.js';
|
|
5
|
+
export declare class GraphqlRatchet {
|
|
6
|
+
private queryProvider;
|
|
7
|
+
private endpointProvider;
|
|
8
|
+
private jwtTokenProvider;
|
|
9
|
+
private errorHandler;
|
|
10
|
+
private apolloCache;
|
|
11
|
+
private noAuthApollo;
|
|
12
|
+
private cachedEndpoint;
|
|
13
|
+
constructor(queryProvider: GraphqlRatchetQueryProvider, endpointProvider: GraphqlRatchetEndpointProvider, jwtTokenProvider: GraphqlRatchetJwtTokenProvider, errorHandler?: GraphqlRatchetErrorHandler);
|
|
14
|
+
private fetchQueryAsGql;
|
|
15
|
+
private createAnonymousApi;
|
|
16
|
+
private fetchApi;
|
|
17
|
+
private createAuthApi;
|
|
18
|
+
private checkIfEndpointChanged;
|
|
19
|
+
clearCaches(): void;
|
|
20
|
+
executeQuery<T>(queryName: string, variables: Record<string, any>, runAnonymous?: boolean): Promise<T>;
|
|
21
|
+
executeMutate<T>(queryName: string, variables: any, runAnonymous?: boolean): Promise<T>;
|
|
22
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { GraphqlRatchetErrorHandler } from './graphql-ratchet-error-handler.js';
|
|
2
|
+
import { LoggerLevelName } from '@bitblit/ratchet-common';
|
|
3
|
+
export declare class DefaultGraphqlRatchetErrorHandler implements GraphqlRatchetErrorHandler {
|
|
4
|
+
private logLevel;
|
|
5
|
+
private rethrow;
|
|
6
|
+
constructor(logLevel?: LoggerLevelName, rethrow?: boolean);
|
|
7
|
+
handleError(error: any, queryName: string, variables: Record<string, any>, anonymous: boolean): void;
|
|
8
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { GraphqlRatchetQueryProvider } from './graphql-ratchet-query-provider.js';
|
|
2
|
+
export declare class LocalFetchQueryProvider implements GraphqlRatchetQueryProvider {
|
|
3
|
+
private pathTemplate;
|
|
4
|
+
private forcePathToLowerCase;
|
|
5
|
+
private cacheMap;
|
|
6
|
+
constructor(pathTemplate?: string, forcePathToLowerCase?: boolean);
|
|
7
|
+
fetchQueryText(qry: string): Promise<string>;
|
|
8
|
+
}
|
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:"
|
|
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:"185",hash:"9974d7fd1bdf391a5297a50e74cadb7030cfc92a",branch:"alpha-2023-07-21-1",tag:"alpha-2023-07-21-1",timeBuiltISO:"2023-07-21T13:28:20-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,14 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bitblit/ratchet-graphql",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.185-alpha",
|
|
4
4
|
"description": "Ratchet tools to simplify use of graphql",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"type": "module",
|
|
7
7
|
"module": "./lib/index.mjs",
|
|
8
8
|
"types": "./lib/index.d.ts",
|
|
9
9
|
"files": [
|
|
10
|
-
"lib
|
|
11
|
-
"bin
|
|
10
|
+
"lib/**",
|
|
11
|
+
"bin/**"
|
|
12
12
|
],
|
|
13
13
|
"exports": {
|
|
14
14
|
".": {
|
|
@@ -53,7 +53,7 @@
|
|
|
53
53
|
"license": "Apache-2.0",
|
|
54
54
|
"dependencies": {
|
|
55
55
|
"@apollo/client": "3.7.17",
|
|
56
|
-
"@bitblit/ratchet-common": "4.0.
|
|
56
|
+
"@bitblit/ratchet-common": "4.0.185-alpha",
|
|
57
57
|
"graphql": "16.7.1"
|
|
58
58
|
}
|
|
59
59
|
}
|