@bitblit/ratchet-graphql 4.0.80-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/dist/cjs/build/ratchet-graphql-info.js +18 -0
- package/dist/cjs/graphql/graphql-ratchet.js +164 -0
- package/dist/cjs/graphql/provider/default-graphql-ratchet-error-handler.js +17 -0
- package/dist/cjs/graphql/provider/graphql-ratchet-endpoint-provider.js +2 -0
- package/dist/cjs/graphql/provider/graphql-ratchet-error-handler.js +2 -0
- package/dist/cjs/graphql/provider/graphql-ratchet-jwt-token-provider.js +2 -0
- package/dist/cjs/graphql/provider/graphql-ratchet-query-provider.js +2 -0
- package/dist/cjs/graphql/provider/local-fetch-query-provider.js +28 -0
- package/dist/cjs/index.js +11 -0
- package/dist/es/build/ratchet-graphql-info.js +14 -0
- package/dist/es/graphql/graphql-ratchet.js +160 -0
- package/dist/es/graphql/provider/default-graphql-ratchet-error-handler.js +13 -0
- package/dist/es/graphql/provider/graphql-ratchet-endpoint-provider.js +1 -0
- package/dist/es/graphql/provider/graphql-ratchet-error-handler.js +1 -0
- package/dist/es/graphql/provider/graphql-ratchet-jwt-token-provider.js +1 -0
- package/dist/es/graphql/provider/graphql-ratchet-query-provider.js +1 -0
- package/dist/es/graphql/provider/local-fetch-query-provider.js +23 -0
- package/dist/es/index.js +8 -0
- package/dist/tsconfig.cjs.tsbuildinfo +1 -0
- package/dist/tsconfig.es.tsbuildinfo +1 -0
- package/dist/tsconfig.types.tsbuildinfo +1 -0
- package/dist/types/build/ratchet-graphql-info.d.ts +5 -0
- package/dist/types/graphql/graphql-ratchet.d.ts +27 -0
- package/dist/types/graphql/provider/default-graphql-ratchet-error-handler.d.ts +8 -0
- package/dist/types/graphql/provider/graphql-ratchet-endpoint-provider.d.ts +3 -0
- package/dist/types/graphql/provider/graphql-ratchet-error-handler.d.ts +3 -0
- package/dist/types/graphql/provider/graphql-ratchet-jwt-token-provider.d.ts +3 -0
- package/dist/types/graphql/provider/graphql-ratchet-query-provider.d.ts +3 -0
- package/dist/types/graphql/provider/local-fetch-query-provider.d.ts +8 -0
- package/dist/types/index.d.ts +11 -0
- package/package.json +70 -0
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RatchetGraphqlInfo = void 0;
|
|
4
|
+
class RatchetGraphqlInfo {
|
|
5
|
+
constructor() { }
|
|
6
|
+
static buildInformation() {
|
|
7
|
+
const val = {
|
|
8
|
+
version: '80',
|
|
9
|
+
hash: '10fcb761c2fa4186df89e527e948f5780a4e14ac',
|
|
10
|
+
branch: 'alpha-2023-03-12-2',
|
|
11
|
+
tag: 'alpha-2023-03-12-2',
|
|
12
|
+
timeBuiltISO: '2023-03-12T20:00:51-0700',
|
|
13
|
+
notes: '',
|
|
14
|
+
};
|
|
15
|
+
return val;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
exports.RatchetGraphqlInfo = RatchetGraphqlInfo;
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.GraphqlRatchet = void 0;
|
|
4
|
+
const core_1 = require("@apollo/client/core");
|
|
5
|
+
const default_graphql_ratchet_error_handler_1 = require("./provider/default-graphql-ratchet-error-handler");
|
|
6
|
+
const ratchet_common_1 = require("@bitblit/ratchet-common");
|
|
7
|
+
class GraphqlRatchet {
|
|
8
|
+
constructor(queryProvider, endpointProvider, jwtTokenProvider, errorHandler = new default_graphql_ratchet_error_handler_1.DefaultGraphqlRatchetErrorHandler()) {
|
|
9
|
+
this.queryProvider = queryProvider;
|
|
10
|
+
this.endpointProvider = endpointProvider;
|
|
11
|
+
this.jwtTokenProvider = jwtTokenProvider;
|
|
12
|
+
this.errorHandler = errorHandler;
|
|
13
|
+
this.apolloCache = new Map();
|
|
14
|
+
ratchet_common_1.RequireRatchet.notNullOrUndefined(queryProvider, 'queryProvider');
|
|
15
|
+
ratchet_common_1.RequireRatchet.notNullOrUndefined(endpointProvider, 'endpointProvider');
|
|
16
|
+
ratchet_common_1.RequireRatchet.notNullOrUndefined(jwtTokenProvider, 'jwtTokenProvider');
|
|
17
|
+
ratchet_common_1.RequireRatchet.notNullOrUndefined(errorHandler, 'errorHandler');
|
|
18
|
+
this.cachedEndpoint = this.endpointProvider.fetchGraphqlEndpoint();
|
|
19
|
+
}
|
|
20
|
+
async fetchQueryAsGql(qry) {
|
|
21
|
+
let rval = null;
|
|
22
|
+
const text = await this.queryProvider.fetchQueryText(qry);
|
|
23
|
+
if (text) {
|
|
24
|
+
rval = (0, core_1.gql)(text);
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
ratchet_common_1.Logger.warn('Could not find requested query : %s', qry);
|
|
28
|
+
}
|
|
29
|
+
return rval;
|
|
30
|
+
}
|
|
31
|
+
createAnonymousApi() {
|
|
32
|
+
ratchet_common_1.Logger.info('Creating anonymous apollo client');
|
|
33
|
+
const httpLink = new core_1.HttpLink({ uri: this.cachedEndpoint });
|
|
34
|
+
const opts = {
|
|
35
|
+
link: httpLink,
|
|
36
|
+
cache: new core_1.InMemoryCache(),
|
|
37
|
+
};
|
|
38
|
+
const rval = new core_1.ApolloClient(opts);
|
|
39
|
+
return rval;
|
|
40
|
+
}
|
|
41
|
+
fetchApi(runAnonymous) {
|
|
42
|
+
let rval = null;
|
|
43
|
+
const jwtToken = this.jwtTokenProvider.fetchJwtToken();
|
|
44
|
+
this.checkIfEndpointChanged();
|
|
45
|
+
ratchet_common_1.Logger.info('Fetch auth apollo client %s', ratchet_common_1.StringRatchet.obscure(ratchet_common_1.StringRatchet.trimToEmpty(jwtToken), 2, 2));
|
|
46
|
+
if (ratchet_common_1.StringRatchet.trimToNull(jwtToken)) {
|
|
47
|
+
ratchet_common_1.Logger.debug('Fetching authd api');
|
|
48
|
+
if (!this.apolloCache.has(jwtToken)) {
|
|
49
|
+
const newValue = this.createAuthApi(jwtToken);
|
|
50
|
+
ratchet_common_1.Logger.debug('Setting apollo cache for this token to %s', newValue);
|
|
51
|
+
this.apolloCache.set(jwtToken, newValue);
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
ratchet_common_1.Logger.debug('Fetching apollo client from cache');
|
|
55
|
+
}
|
|
56
|
+
rval = this.apolloCache.get(jwtToken);
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
ratchet_common_1.Logger.debug('Fetching unauthd ap');
|
|
60
|
+
if (runAnonymous) {
|
|
61
|
+
if (!this.noAuthApollo) {
|
|
62
|
+
this.noAuthApollo = this.createAnonymousApi();
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
ratchet_common_1.Logger.debug('Fetching anonymous client from cache');
|
|
66
|
+
}
|
|
67
|
+
rval = this.noAuthApollo;
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
ratchet_common_1.ErrorRatchet.throwFormattedErr('Cannot fetch api - no token and runAnonymous is set to %s : -%s-', runAnonymous, jwtToken);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
ratchet_common_1.Logger.debug('FetchApi returning %s', rval);
|
|
74
|
+
return rval;
|
|
75
|
+
}
|
|
76
|
+
createAuthApi(jwtToken) {
|
|
77
|
+
ratchet_common_1.Logger.info('Creating a new authenticated api for %s : %s', this.cachedEndpoint, ratchet_common_1.StringRatchet.obscure(jwtToken, 2, 2));
|
|
78
|
+
let rval = null;
|
|
79
|
+
ratchet_common_1.RequireRatchet.notNullUndefinedOrOnlyWhitespaceString(jwtToken, 'jwtToken');
|
|
80
|
+
ratchet_common_1.Logger.info('Creating auth apollo client %s', ratchet_common_1.StringRatchet.obscure(jwtToken, 2, 2));
|
|
81
|
+
const httpLink = new core_1.HttpLink({ uri: this.cachedEndpoint });
|
|
82
|
+
const authLink = new core_1.ApolloLink((operation, forward) => {
|
|
83
|
+
operation.setContext({
|
|
84
|
+
headers: {
|
|
85
|
+
authorization: `Bearer ${jwtToken}`,
|
|
86
|
+
},
|
|
87
|
+
});
|
|
88
|
+
return forward(operation);
|
|
89
|
+
});
|
|
90
|
+
const opts = {
|
|
91
|
+
link: authLink.concat(httpLink),
|
|
92
|
+
cache: new core_1.InMemoryCache(),
|
|
93
|
+
};
|
|
94
|
+
rval = new core_1.ApolloClient(opts);
|
|
95
|
+
return rval;
|
|
96
|
+
}
|
|
97
|
+
checkIfEndpointChanged() {
|
|
98
|
+
const check = this.endpointProvider.fetchGraphqlEndpoint();
|
|
99
|
+
if (check !== this.cachedEndpoint) {
|
|
100
|
+
ratchet_common_1.Logger.info('Endpoint changed from %s to %s - clearing apollo caches');
|
|
101
|
+
this.apolloCache = new Map();
|
|
102
|
+
this.noAuthApollo = null;
|
|
103
|
+
this.cachedEndpoint = check;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
async executeQuery(queryName, variables, runAnonymous = false) {
|
|
107
|
+
let rval = null;
|
|
108
|
+
try {
|
|
109
|
+
const api = this.fetchApi(runAnonymous);
|
|
110
|
+
if (api) {
|
|
111
|
+
ratchet_common_1.Logger.debug('API fetched for %s, fetching gql', queryName);
|
|
112
|
+
const GQL = await this.fetchQueryAsGql(queryName);
|
|
113
|
+
ratchet_common_1.Logger.debug('API and GQL fetched for %s - running %s %s', queryName, GQL, api);
|
|
114
|
+
const newValues = await api.query({
|
|
115
|
+
query: GQL,
|
|
116
|
+
variables: variables,
|
|
117
|
+
fetchPolicy: 'no-cache',
|
|
118
|
+
});
|
|
119
|
+
ratchet_common_1.Logger.silly('Query returned: %j', newValues);
|
|
120
|
+
const keys = Object.keys(newValues.data);
|
|
121
|
+
if (keys.length !== 1) {
|
|
122
|
+
ratchet_common_1.ErrorRatchet.throwFormattedErr('Unexpected number of keys : %s : %j', keys.length, keys);
|
|
123
|
+
}
|
|
124
|
+
rval = newValues.data[keys[0]];
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
ratchet_common_1.ErrorRatchet.throwFormattedErr('Cannot run - no api fetched');
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
catch (err) {
|
|
131
|
+
ratchet_common_1.Logger.silly('Exception caught in executeQuery : %s %s %j %s', err, queryName, variables, runAnonymous, err);
|
|
132
|
+
this.errorHandler.handleError(err, queryName, variables, runAnonymous);
|
|
133
|
+
}
|
|
134
|
+
return rval;
|
|
135
|
+
}
|
|
136
|
+
async executeMutate(queryName, variables, runAnonymous = false) {
|
|
137
|
+
ratchet_common_1.Logger.info('Mutate : %s : %j', queryName, variables);
|
|
138
|
+
let rval = null;
|
|
139
|
+
const api = this.fetchApi(runAnonymous);
|
|
140
|
+
try {
|
|
141
|
+
if (api) {
|
|
142
|
+
const GQL = await this.fetchQueryAsGql(queryName);
|
|
143
|
+
const newValues = await api.mutate({
|
|
144
|
+
mutation: GQL,
|
|
145
|
+
variables: variables,
|
|
146
|
+
fetchPolicy: 'no-cache',
|
|
147
|
+
});
|
|
148
|
+
const keys = Object.keys(newValues.data);
|
|
149
|
+
if (keys.length !== 1) {
|
|
150
|
+
ratchet_common_1.ErrorRatchet.throwFormattedErr('Unexpected number of keys : %s : %j', keys.length, keys);
|
|
151
|
+
}
|
|
152
|
+
rval = newValues.data[keys[0]];
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
ratchet_common_1.ErrorRatchet.throwFormattedErr('Cannot run - no api fetched');
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
catch (err) {
|
|
159
|
+
this.errorHandler.handleError(err, queryName, variables, runAnonymous);
|
|
160
|
+
}
|
|
161
|
+
return rval;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
exports.GraphqlRatchet = GraphqlRatchet;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DefaultGraphqlRatchetErrorHandler = void 0;
|
|
4
|
+
const ratchet_common_1 = require("@bitblit/ratchet-common");
|
|
5
|
+
class DefaultGraphqlRatchetErrorHandler {
|
|
6
|
+
constructor(logLevel = ratchet_common_1.LoggerLevelName.warn, rethrow = false) {
|
|
7
|
+
this.logLevel = logLevel;
|
|
8
|
+
this.rethrow = rethrow;
|
|
9
|
+
}
|
|
10
|
+
handleError(error, queryName, variables, anonymous) {
|
|
11
|
+
ratchet_common_1.Logger.logByLevel(this.logLevel, 'Graphql failed : %s : %s : Anon-%s : %j', error, queryName, anonymous, variables);
|
|
12
|
+
if (this.rethrow) {
|
|
13
|
+
throw error;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
exports.DefaultGraphqlRatchetErrorHandler = DefaultGraphqlRatchetErrorHandler;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LocalFetchQueryProvider = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const ratchet_common_1 = require("@bitblit/ratchet-common");
|
|
6
|
+
const cross_fetch_1 = tslib_1.__importDefault(require("cross-fetch"));
|
|
7
|
+
class LocalFetchQueryProvider {
|
|
8
|
+
constructor(pathTemplate = 'assets/gql/{{QUERY_NAME}}.gql', forcePathToLowerCase = false) {
|
|
9
|
+
this.pathTemplate = pathTemplate;
|
|
10
|
+
this.forcePathToLowerCase = forcePathToLowerCase;
|
|
11
|
+
this.cacheMap = new Map();
|
|
12
|
+
}
|
|
13
|
+
async fetchQueryText(qry) {
|
|
14
|
+
let rval = this.cacheMap.get(qry.toLowerCase());
|
|
15
|
+
if (!rval) {
|
|
16
|
+
const tgt = this.forcePathToLowerCase ? qry.toLowerCase() : qry;
|
|
17
|
+
const pathInput = this.pathTemplate.split('{{QUERY_NAME}}').join(tgt);
|
|
18
|
+
ratchet_common_1.Logger.info('Cache miss, loading %s from %s', qry, pathInput);
|
|
19
|
+
const qryResp = await (0, cross_fetch_1.default)(pathInput);
|
|
20
|
+
rval = ratchet_common_1.StringRatchet.trimToNull(await qryResp.text());
|
|
21
|
+
if (rval) {
|
|
22
|
+
this.cacheMap.set(qry.toLowerCase(), rval);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return rval;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
exports.LocalFetchQueryProvider = LocalFetchQueryProvider;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
tslib_1.__exportStar(require("./build/ratchet-graphql-info"), exports);
|
|
5
|
+
tslib_1.__exportStar(require("./graphql/graphql-ratchet"), exports);
|
|
6
|
+
tslib_1.__exportStar(require("./graphql/provider/default-graphql-ratchet-error-handler"), exports);
|
|
7
|
+
tslib_1.__exportStar(require("./graphql/provider/graphql-ratchet-endpoint-provider"), exports);
|
|
8
|
+
tslib_1.__exportStar(require("./graphql/provider/graphql-ratchet-error-handler"), exports);
|
|
9
|
+
tslib_1.__exportStar(require("./graphql/provider/graphql-ratchet-jwt-token-provider"), exports);
|
|
10
|
+
tslib_1.__exportStar(require("./graphql/provider/graphql-ratchet-query-provider"), exports);
|
|
11
|
+
tslib_1.__exportStar(require("./graphql/provider/local-fetch-query-provider"), exports);
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export class RatchetGraphqlInfo {
|
|
2
|
+
constructor() { }
|
|
3
|
+
static buildInformation() {
|
|
4
|
+
const val = {
|
|
5
|
+
version: '80',
|
|
6
|
+
hash: '10fcb761c2fa4186df89e527e948f5780a4e14ac',
|
|
7
|
+
branch: 'alpha-2023-03-12-2',
|
|
8
|
+
tag: 'alpha-2023-03-12-2',
|
|
9
|
+
timeBuiltISO: '2023-03-12T20:00:51-0700',
|
|
10
|
+
notes: '',
|
|
11
|
+
};
|
|
12
|
+
return val;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { ApolloClient, ApolloLink, gql, HttpLink, InMemoryCache } from '@apollo/client/core';
|
|
2
|
+
import { DefaultGraphqlRatchetErrorHandler } from './provider/default-graphql-ratchet-error-handler';
|
|
3
|
+
import { RequireRatchet, Logger, StringRatchet, ErrorRatchet } from '@bitblit/ratchet-common';
|
|
4
|
+
export class GraphqlRatchet {
|
|
5
|
+
constructor(queryProvider, endpointProvider, jwtTokenProvider, errorHandler = new DefaultGraphqlRatchetErrorHandler()) {
|
|
6
|
+
this.queryProvider = queryProvider;
|
|
7
|
+
this.endpointProvider = endpointProvider;
|
|
8
|
+
this.jwtTokenProvider = jwtTokenProvider;
|
|
9
|
+
this.errorHandler = errorHandler;
|
|
10
|
+
this.apolloCache = new Map();
|
|
11
|
+
RequireRatchet.notNullOrUndefined(queryProvider, 'queryProvider');
|
|
12
|
+
RequireRatchet.notNullOrUndefined(endpointProvider, 'endpointProvider');
|
|
13
|
+
RequireRatchet.notNullOrUndefined(jwtTokenProvider, 'jwtTokenProvider');
|
|
14
|
+
RequireRatchet.notNullOrUndefined(errorHandler, 'errorHandler');
|
|
15
|
+
this.cachedEndpoint = this.endpointProvider.fetchGraphqlEndpoint();
|
|
16
|
+
}
|
|
17
|
+
async fetchQueryAsGql(qry) {
|
|
18
|
+
let rval = null;
|
|
19
|
+
const text = await this.queryProvider.fetchQueryText(qry);
|
|
20
|
+
if (text) {
|
|
21
|
+
rval = gql(text);
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
Logger.warn('Could not find requested query : %s', qry);
|
|
25
|
+
}
|
|
26
|
+
return rval;
|
|
27
|
+
}
|
|
28
|
+
createAnonymousApi() {
|
|
29
|
+
Logger.info('Creating anonymous apollo client');
|
|
30
|
+
const httpLink = new HttpLink({ uri: this.cachedEndpoint });
|
|
31
|
+
const opts = {
|
|
32
|
+
link: httpLink,
|
|
33
|
+
cache: new InMemoryCache(),
|
|
34
|
+
};
|
|
35
|
+
const rval = new ApolloClient(opts);
|
|
36
|
+
return rval;
|
|
37
|
+
}
|
|
38
|
+
fetchApi(runAnonymous) {
|
|
39
|
+
let rval = null;
|
|
40
|
+
const jwtToken = this.jwtTokenProvider.fetchJwtToken();
|
|
41
|
+
this.checkIfEndpointChanged();
|
|
42
|
+
Logger.info('Fetch auth apollo client %s', StringRatchet.obscure(StringRatchet.trimToEmpty(jwtToken), 2, 2));
|
|
43
|
+
if (StringRatchet.trimToNull(jwtToken)) {
|
|
44
|
+
Logger.debug('Fetching authd api');
|
|
45
|
+
if (!this.apolloCache.has(jwtToken)) {
|
|
46
|
+
const newValue = this.createAuthApi(jwtToken);
|
|
47
|
+
Logger.debug('Setting apollo cache for this token to %s', newValue);
|
|
48
|
+
this.apolloCache.set(jwtToken, newValue);
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
Logger.debug('Fetching apollo client from cache');
|
|
52
|
+
}
|
|
53
|
+
rval = this.apolloCache.get(jwtToken);
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
Logger.debug('Fetching unauthd ap');
|
|
57
|
+
if (runAnonymous) {
|
|
58
|
+
if (!this.noAuthApollo) {
|
|
59
|
+
this.noAuthApollo = this.createAnonymousApi();
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
Logger.debug('Fetching anonymous client from cache');
|
|
63
|
+
}
|
|
64
|
+
rval = this.noAuthApollo;
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
ErrorRatchet.throwFormattedErr('Cannot fetch api - no token and runAnonymous is set to %s : -%s-', runAnonymous, jwtToken);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
Logger.debug('FetchApi returning %s', rval);
|
|
71
|
+
return rval;
|
|
72
|
+
}
|
|
73
|
+
createAuthApi(jwtToken) {
|
|
74
|
+
Logger.info('Creating a new authenticated api for %s : %s', this.cachedEndpoint, StringRatchet.obscure(jwtToken, 2, 2));
|
|
75
|
+
let rval = null;
|
|
76
|
+
RequireRatchet.notNullUndefinedOrOnlyWhitespaceString(jwtToken, 'jwtToken');
|
|
77
|
+
Logger.info('Creating auth apollo client %s', StringRatchet.obscure(jwtToken, 2, 2));
|
|
78
|
+
const httpLink = new HttpLink({ uri: this.cachedEndpoint });
|
|
79
|
+
const authLink = new ApolloLink((operation, forward) => {
|
|
80
|
+
operation.setContext({
|
|
81
|
+
headers: {
|
|
82
|
+
authorization: `Bearer ${jwtToken}`,
|
|
83
|
+
},
|
|
84
|
+
});
|
|
85
|
+
return forward(operation);
|
|
86
|
+
});
|
|
87
|
+
const opts = {
|
|
88
|
+
link: authLink.concat(httpLink),
|
|
89
|
+
cache: new InMemoryCache(),
|
|
90
|
+
};
|
|
91
|
+
rval = new ApolloClient(opts);
|
|
92
|
+
return rval;
|
|
93
|
+
}
|
|
94
|
+
checkIfEndpointChanged() {
|
|
95
|
+
const check = this.endpointProvider.fetchGraphqlEndpoint();
|
|
96
|
+
if (check !== this.cachedEndpoint) {
|
|
97
|
+
Logger.info('Endpoint changed from %s to %s - clearing apollo caches');
|
|
98
|
+
this.apolloCache = new Map();
|
|
99
|
+
this.noAuthApollo = null;
|
|
100
|
+
this.cachedEndpoint = check;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
async executeQuery(queryName, variables, runAnonymous = false) {
|
|
104
|
+
let rval = null;
|
|
105
|
+
try {
|
|
106
|
+
const api = this.fetchApi(runAnonymous);
|
|
107
|
+
if (api) {
|
|
108
|
+
Logger.debug('API fetched for %s, fetching gql', queryName);
|
|
109
|
+
const GQL = await this.fetchQueryAsGql(queryName);
|
|
110
|
+
Logger.debug('API and GQL fetched for %s - running %s %s', queryName, GQL, api);
|
|
111
|
+
const newValues = await api.query({
|
|
112
|
+
query: GQL,
|
|
113
|
+
variables: variables,
|
|
114
|
+
fetchPolicy: 'no-cache',
|
|
115
|
+
});
|
|
116
|
+
Logger.silly('Query returned: %j', newValues);
|
|
117
|
+
const keys = Object.keys(newValues.data);
|
|
118
|
+
if (keys.length !== 1) {
|
|
119
|
+
ErrorRatchet.throwFormattedErr('Unexpected number of keys : %s : %j', keys.length, keys);
|
|
120
|
+
}
|
|
121
|
+
rval = newValues.data[keys[0]];
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
ErrorRatchet.throwFormattedErr('Cannot run - no api fetched');
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
catch (err) {
|
|
128
|
+
Logger.silly('Exception caught in executeQuery : %s %s %j %s', err, queryName, variables, runAnonymous, err);
|
|
129
|
+
this.errorHandler.handleError(err, queryName, variables, runAnonymous);
|
|
130
|
+
}
|
|
131
|
+
return rval;
|
|
132
|
+
}
|
|
133
|
+
async executeMutate(queryName, variables, runAnonymous = false) {
|
|
134
|
+
Logger.info('Mutate : %s : %j', queryName, variables);
|
|
135
|
+
let rval = null;
|
|
136
|
+
const api = this.fetchApi(runAnonymous);
|
|
137
|
+
try {
|
|
138
|
+
if (api) {
|
|
139
|
+
const GQL = await this.fetchQueryAsGql(queryName);
|
|
140
|
+
const newValues = await api.mutate({
|
|
141
|
+
mutation: GQL,
|
|
142
|
+
variables: variables,
|
|
143
|
+
fetchPolicy: 'no-cache',
|
|
144
|
+
});
|
|
145
|
+
const keys = Object.keys(newValues.data);
|
|
146
|
+
if (keys.length !== 1) {
|
|
147
|
+
ErrorRatchet.throwFormattedErr('Unexpected number of keys : %s : %j', keys.length, keys);
|
|
148
|
+
}
|
|
149
|
+
rval = newValues.data[keys[0]];
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
ErrorRatchet.throwFormattedErr('Cannot run - no api fetched');
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
catch (err) {
|
|
156
|
+
this.errorHandler.handleError(err, queryName, variables, runAnonymous);
|
|
157
|
+
}
|
|
158
|
+
return rval;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { LoggerLevelName, Logger } from '@bitblit/ratchet-common';
|
|
2
|
+
export class DefaultGraphqlRatchetErrorHandler {
|
|
3
|
+
constructor(logLevel = LoggerLevelName.warn, rethrow = false) {
|
|
4
|
+
this.logLevel = logLevel;
|
|
5
|
+
this.rethrow = rethrow;
|
|
6
|
+
}
|
|
7
|
+
handleError(error, queryName, variables, anonymous) {
|
|
8
|
+
Logger.logByLevel(this.logLevel, 'Graphql failed : %s : %s : Anon-%s : %j', error, queryName, anonymous, variables);
|
|
9
|
+
if (this.rethrow) {
|
|
10
|
+
throw error;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { StringRatchet, Logger } from '@bitblit/ratchet-common';
|
|
2
|
+
import fetch from 'cross-fetch';
|
|
3
|
+
export class LocalFetchQueryProvider {
|
|
4
|
+
constructor(pathTemplate = 'assets/gql/{{QUERY_NAME}}.gql', forcePathToLowerCase = false) {
|
|
5
|
+
this.pathTemplate = pathTemplate;
|
|
6
|
+
this.forcePathToLowerCase = forcePathToLowerCase;
|
|
7
|
+
this.cacheMap = new Map();
|
|
8
|
+
}
|
|
9
|
+
async fetchQueryText(qry) {
|
|
10
|
+
let rval = this.cacheMap.get(qry.toLowerCase());
|
|
11
|
+
if (!rval) {
|
|
12
|
+
const tgt = this.forcePathToLowerCase ? qry.toLowerCase() : qry;
|
|
13
|
+
const pathInput = this.pathTemplate.split('{{QUERY_NAME}}').join(tgt);
|
|
14
|
+
Logger.info('Cache miss, loading %s from %s', qry, pathInput);
|
|
15
|
+
const qryResp = await fetch(pathInput);
|
|
16
|
+
rval = StringRatchet.trimToNull(await qryResp.text());
|
|
17
|
+
if (rval) {
|
|
18
|
+
this.cacheMap.set(qry.toLowerCase(), rval);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return rval;
|
|
22
|
+
}
|
|
23
|
+
}
|
package/dist/es/index.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export * from './build/ratchet-graphql-info';
|
|
2
|
+
export * from './graphql/graphql-ratchet';
|
|
3
|
+
export * from './graphql/provider/default-graphql-ratchet-error-handler';
|
|
4
|
+
export * from './graphql/provider/graphql-ratchet-endpoint-provider';
|
|
5
|
+
export * from './graphql/provider/graphql-ratchet-error-handler';
|
|
6
|
+
export * from './graphql/provider/graphql-ratchet-jwt-token-provider';
|
|
7
|
+
export * from './graphql/provider/graphql-ratchet-query-provider';
|
|
8
|
+
export * from './graphql/provider/local-fetch-query-provider';
|