@common-stack/mobile-stack-react 6.0.6-alpha.7 → 6.0.6-alpha.8
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/config/base-apollo-client.d.ts +2 -1
- package/lib/config/base-apollo-client.js +67 -42
- package/lib/config/base-redux-config.d.ts +7 -9
- package/lib/config/base-redux-config.js +33 -30
- package/lib/config/client.service.js +2 -2
- package/lib/config/mobile-env-config.js +2 -2
- package/lib/config/redux-config.d.ts +1 -1
- package/lib/config/redux-config.js +1 -1
- package/lib/utils/index.js +0 -1
- package/package.json +2 -2
- package/lib/config/base-apollo-cache.js +0 -35
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { ApolloClient } from '@apollo/client';
|
|
2
2
|
import { InMemoryCache } from '@apollo/client/cache';
|
|
3
|
+
import { IClientState } from '@common-stack/client-core';
|
|
3
4
|
import { CdmLogger } from '@cdm-logger/core';
|
|
4
5
|
interface IApolloClientParams {
|
|
5
6
|
initialState?: any;
|
|
6
7
|
scope: 'browser' | 'server' | 'native';
|
|
7
8
|
getDataIdFromObject: (x?: any) => string;
|
|
8
|
-
clientState:
|
|
9
|
+
clientState: IClientState;
|
|
9
10
|
isDebug: boolean;
|
|
10
11
|
isDev: boolean;
|
|
11
12
|
isSSR: boolean;
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import {ApolloLink,ApolloClient}from'@apollo/client';import {HttpLink}from'@apollo/client/link/http';import {BatchHttpLink}from'@apollo/client/link/batch-http';import {onError}from'@apollo/client/link/error
|
|
2
|
-
// version 09/18/2021
|
|
1
|
+
import {ApolloLink,ApolloClient}from'@apollo/client';import {InMemoryCache}from'@apollo/client/cache';import {HttpLink,createHttpLink}from'@apollo/client/link/http';import {BatchHttpLink}from'@apollo/client/link/batch-http';import {onError}from'@apollo/client/link/error';import {GraphQLWsLink}from'@apollo/client/link/subscriptions';import {getOperationAST}from'graphql';import {invariant}from'ts-invariant';import fetch from'cross-fetch';import {isBoolean,merge}from'lodash-es';import {RetryLink}from'@apollo/client/link/retry';import {createClient}from'graphql-ws';// version 09/18/2021
|
|
3
2
|
/* eslint-disable import/no-extraneous-dependencies */
|
|
4
3
|
/* eslint-disable no-underscore-dangle */
|
|
5
4
|
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
|
@@ -8,50 +7,59 @@ const schema = `
|
|
|
8
7
|
`;
|
|
9
8
|
const errorLink = onError(({ graphQLErrors, networkError }) => {
|
|
10
9
|
if (graphQLErrors) {
|
|
11
|
-
graphQLErrors.map(({ message, locations, path }) =>
|
|
10
|
+
graphQLErrors.map(({ message, locations, path }) =>
|
|
11
|
+
// tslint:disable-next-line
|
|
12
|
+
invariant.warn(`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`));
|
|
12
13
|
}
|
|
13
14
|
if (networkError) {
|
|
15
|
+
// tslint:disable-next-line
|
|
14
16
|
invariant.warn(`[Network error]: ${networkError}`);
|
|
15
17
|
}
|
|
16
18
|
});
|
|
19
|
+
let _apolloClient;
|
|
20
|
+
let _memoryCache;
|
|
17
21
|
const createApolloClient = ({ scope, isDev, isDebug, isSSR, getDataIdFromObject, clientState, httpGraphqlURL, httpLocalGraphqlURL, initialState, logger, }) => {
|
|
18
|
-
logger.debug('Initializing Apollo Client with parameters, {%o}', {
|
|
19
|
-
scope,
|
|
20
|
-
isDev,
|
|
21
|
-
isDebug,
|
|
22
|
-
isSSR,
|
|
23
|
-
httpGraphqlURL,
|
|
24
|
-
httpLocalGraphqlURL,
|
|
25
|
-
});
|
|
26
22
|
const isBrowser = scope === 'browser';
|
|
27
23
|
const isServer = scope === 'server';
|
|
28
|
-
const cache = createCache({ getDataIdFromObject, clientState, logger });
|
|
29
|
-
logger.debug('Created new Apollo memory cache');
|
|
30
|
-
const retryLink = new RetryLink({
|
|
31
|
-
attempts: async (count, operation, error) => {
|
|
32
|
-
logger.debug('Retrying link attempt', { count, operation, error });
|
|
33
|
-
const promises = (clientState.retryLinkAttemptFuncs || []).map((func) => func(count, operation, error));
|
|
34
|
-
try {
|
|
35
|
-
const result = await Promise.all(promises);
|
|
36
|
-
return !!result.find((item) => item && isBoolean(item));
|
|
37
|
-
}
|
|
38
|
-
catch (e) {
|
|
39
|
-
logger.error('Error occurred in retryLink attempt condition', e);
|
|
40
|
-
throw e;
|
|
41
|
-
}
|
|
42
|
-
},
|
|
43
|
-
});
|
|
44
24
|
let link;
|
|
25
|
+
const cache = new InMemoryCache({
|
|
26
|
+
dataIdFromObject: getDataIdFromObject,
|
|
27
|
+
possibleTypes: clientState.possibleTypes,
|
|
28
|
+
typePolicies: clientState.typePolicies,
|
|
29
|
+
});
|
|
30
|
+
const attemptConditions = async (count, operation, error) => {
|
|
31
|
+
const promises = (clientState.retryLinkAttemptFuncs || []).map((func) => {
|
|
32
|
+
return func(count, operation, error);
|
|
33
|
+
});
|
|
34
|
+
try {
|
|
35
|
+
const result = await promises;
|
|
36
|
+
return !!result.find((item) => item && isBoolean(item));
|
|
37
|
+
}
|
|
38
|
+
catch (e) {
|
|
39
|
+
logger.trace('Error occured in retryLink Attempt condition', e);
|
|
40
|
+
throw e;
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
const retrylink = new RetryLink({
|
|
44
|
+
attempts: attemptConditions,
|
|
45
|
+
});
|
|
46
|
+
if (_apolloClient && _memoryCache) {
|
|
47
|
+
// return quickly if client is already created.
|
|
48
|
+
return {
|
|
49
|
+
apolloClient: _apolloClient,
|
|
50
|
+
cache: _memoryCache,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
_memoryCache = cache;
|
|
45
54
|
if (isBrowser) {
|
|
46
55
|
const connectionParams = async () => {
|
|
47
|
-
logger.debug('Getting WebSocket connection parameters');
|
|
48
56
|
const param = {};
|
|
49
57
|
for (const connectionParam of clientState.connectionParams) {
|
|
50
|
-
|
|
51
|
-
merge(param, await result());
|
|
58
|
+
merge(param, await connectionParam);
|
|
52
59
|
}
|
|
53
60
|
return param;
|
|
54
61
|
};
|
|
62
|
+
let timedOut;
|
|
55
63
|
const wsLink = new GraphQLWsLink(createClient({
|
|
56
64
|
url: httpGraphqlURL.replace(/^http/, 'ws'),
|
|
57
65
|
retryAttempts: 10,
|
|
@@ -60,21 +68,28 @@ const createApolloClient = ({ scope, isDev, isDebug, isSSR, getDataIdFromObject,
|
|
|
60
68
|
keepAlive: 10000,
|
|
61
69
|
connectionParams,
|
|
62
70
|
on: {
|
|
63
|
-
connected: (socket) =>
|
|
71
|
+
connected: (socket) => {
|
|
72
|
+
},
|
|
64
73
|
error: async (error) => {
|
|
65
74
|
logger.error(error, '[WS connectionCallback error] %j');
|
|
66
75
|
const promises = (clientState.connectionCallbackFuncs || []).map((func) => func(wsLink, error, {}));
|
|
67
76
|
try {
|
|
68
|
-
await
|
|
77
|
+
await promises;
|
|
69
78
|
}
|
|
70
79
|
catch (err) {
|
|
71
|
-
logger.trace('Error occurred in
|
|
80
|
+
logger.trace('Error occurred in connectionCallback condition', err);
|
|
72
81
|
throw err;
|
|
73
82
|
}
|
|
74
83
|
},
|
|
75
84
|
// connected: (socket, payload) => {}
|
|
76
|
-
ping: () =>
|
|
77
|
-
|
|
85
|
+
ping: () => {
|
|
86
|
+
logger.trace('Pinged Server');
|
|
87
|
+
},
|
|
88
|
+
pong: (received) => {
|
|
89
|
+
logger.trace('Pong received');
|
|
90
|
+
if (received)
|
|
91
|
+
clearTimeout(timedOut); // pong is received, clear connection close timeout
|
|
92
|
+
},
|
|
78
93
|
// inactivityTimeout: 10000,
|
|
79
94
|
},
|
|
80
95
|
}));
|
|
@@ -92,16 +107,20 @@ const createApolloClient = ({ scope, isDev, isDebug, isSSR, getDataIdFromObject,
|
|
|
92
107
|
link = new BatchHttpLink({ uri: httpLocalGraphqlURL, fetch: fetch });
|
|
93
108
|
}
|
|
94
109
|
else {
|
|
95
|
-
link =
|
|
110
|
+
link = createHttpLink({ uri: httpLocalGraphqlURL, fetch: fetch });
|
|
111
|
+
}
|
|
112
|
+
const links = [errorLink, retrylink, ...(clientState.preLinks || []), link];
|
|
113
|
+
// Add apollo logger during development only
|
|
114
|
+
if (isBrowser && (isDev || isDebug)) {
|
|
115
|
+
const apolloLogger = require('apollo-link-logger');
|
|
116
|
+
links.unshift(apolloLogger.default);
|
|
96
117
|
}
|
|
97
|
-
const links = [errorLink, retryLink, ...(clientState.preLinks || []), link];
|
|
98
118
|
const params = {
|
|
99
119
|
queryDeduplication: true,
|
|
100
120
|
typeDefs: schema.concat(clientState.typeDefs),
|
|
101
121
|
resolvers: clientState.resolvers,
|
|
102
122
|
link: ApolloLink.from(links),
|
|
103
123
|
cache,
|
|
104
|
-
credentials: 'include',
|
|
105
124
|
connectToDevTools: isBrowser && (isDev || isDebug),
|
|
106
125
|
};
|
|
107
126
|
if (isSSR) {
|
|
@@ -115,8 +134,14 @@ const createApolloClient = ({ scope, isDev, isDebug, isSSR, getDataIdFromObject,
|
|
|
115
134
|
params.ssrMode = true;
|
|
116
135
|
}
|
|
117
136
|
}
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
137
|
+
_apolloClient = new ApolloClient(params);
|
|
138
|
+
clientState?.defaults?.forEach((x) => {
|
|
139
|
+
if (x.type === 'query') {
|
|
140
|
+
cache.writeQuery(x);
|
|
141
|
+
}
|
|
142
|
+
else if (x.type === 'fragment') {
|
|
143
|
+
cache.writeFragment(x);
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
return { apolloClient: _apolloClient, cache };
|
|
122
147
|
};export{createApolloClient};
|
|
@@ -1,24 +1,22 @@
|
|
|
1
|
-
import { Middleware,
|
|
1
|
+
import { Middleware, Action, ReducersMapObject, PreloadedState } from 'redux';
|
|
2
2
|
import { EpicMiddleware, Epic } from 'redux-observable';
|
|
3
3
|
import { PersistConfig } from 'redux-persist';
|
|
4
4
|
interface IReduxStore<S = any> {
|
|
5
5
|
scope: 'browser' | 'server' | 'native' | 'ElectronMain';
|
|
6
6
|
isDebug: boolean;
|
|
7
7
|
isDev: boolean;
|
|
8
|
-
reducers:
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
epicMiddleware?: EpicMiddleware<any, any, S, any>;
|
|
8
|
+
reducers: ReducersMapObject<S>;
|
|
9
|
+
rootEpic?: Epic<Action<S>, Action<any>, void, any>;
|
|
10
|
+
epicMiddleware?: EpicMiddleware<Action<S>, Action<any>>;
|
|
12
11
|
preMiddleware?: Middleware[];
|
|
13
12
|
postMiddleware?: Middleware[];
|
|
14
13
|
middleware?: Middleware[];
|
|
15
|
-
initialState
|
|
16
|
-
persistConfig?: PersistConfig<any>;
|
|
17
|
-
reduxConfig?: any;
|
|
14
|
+
initialState: PreloadedState<S>;
|
|
15
|
+
persistConfig?: PersistConfig<S, any>;
|
|
18
16
|
}
|
|
19
17
|
/**
|
|
20
18
|
* Add any reducers required for this app dirctly in to
|
|
21
19
|
* `combineReducers`
|
|
22
20
|
*/
|
|
23
|
-
export declare const createReduxStore: ({ scope, isDebug, isDev, reducers, rootEpic,
|
|
21
|
+
export declare const createReduxStore: ({ scope, isDebug, isDev, reducers, rootEpic, epicMiddleware, preMiddleware, postMiddleware, middleware, initialState, persistConfig, }: IReduxStore<any>) => import("redux").Store<any, import("redux").AnyAction>;
|
|
24
22
|
export {};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {combineReducers,
|
|
1
|
+
import {combineReducers,createStore,applyMiddleware,compose}from'redux';import {persistReducer}from'redux-persist';import thunkMiddleware from'redux-thunk';// version 11/12/2021
|
|
2
2
|
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
|
3
3
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
4
4
|
/* eslint-disable @typescript-eslint/no-var-requires */
|
|
@@ -8,41 +8,44 @@ import {combineReducers,configureStore}from'@reduxjs/toolkit';import {persistRed
|
|
|
8
8
|
* Add any reducers required for this app dirctly in to
|
|
9
9
|
* `combineReducers`
|
|
10
10
|
*/
|
|
11
|
-
const createReduxStore = ({ scope, isDebug, isDev, reducers, rootEpic,
|
|
11
|
+
const createReduxStore = ({ scope, isDebug, isDev, reducers, rootEpic, epicMiddleware, preMiddleware, postMiddleware, middleware, initialState = {}, persistConfig, }) => {
|
|
12
12
|
const isBrowser = scope === 'browser';
|
|
13
13
|
const isElectronMain = scope === 'ElectronMain';
|
|
14
14
|
/**
|
|
15
15
|
* Add middleware that required for this app.
|
|
16
16
|
*/
|
|
17
|
+
const middlewares = [thunkMiddleware];
|
|
18
|
+
// add epicMiddleware
|
|
19
|
+
if (epicMiddleware) {
|
|
20
|
+
middlewares.push(epicMiddleware);
|
|
21
|
+
}
|
|
22
|
+
if (preMiddleware) {
|
|
23
|
+
middlewares.unshift(...preMiddleware);
|
|
24
|
+
}
|
|
25
|
+
// Add redux logger during development only
|
|
26
|
+
if ((isDev || isDebug) && isBrowser) {
|
|
27
|
+
const { createLogger } = require('redux-logger');
|
|
28
|
+
middlewares.push(createLogger({
|
|
29
|
+
level: 'info',
|
|
30
|
+
collapsed: true,
|
|
31
|
+
}));
|
|
32
|
+
}
|
|
33
|
+
if (middleware) {
|
|
34
|
+
middlewares.push(...middleware);
|
|
35
|
+
}
|
|
36
|
+
if (postMiddleware) {
|
|
37
|
+
middlewares.push(...postMiddleware);
|
|
38
|
+
}
|
|
39
|
+
const enhancers = () => [applyMiddleware(...middlewares)];
|
|
40
|
+
const composeEnhancers = ((isDev || isDebug) && isBrowser && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__) || compose;
|
|
17
41
|
const rootReducer = combineReducers(reducers);
|
|
18
|
-
const persistedReducer = persistConfig
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
const store = configureStore({
|
|
26
|
-
reducer: persistedReducer,
|
|
27
|
-
middleware: (getDefaultMiddleware) => getDefaultMiddleware({
|
|
28
|
-
serializableCheck: {
|
|
29
|
-
ignoredActions: [
|
|
30
|
-
FLUSH,
|
|
31
|
-
REHYDRATE,
|
|
32
|
-
PAUSE,
|
|
33
|
-
PERSIST,
|
|
34
|
-
PURGE,
|
|
35
|
-
REGISTER,
|
|
36
|
-
...(reduxConfig?.ignoredActions || []),
|
|
37
|
-
],
|
|
38
|
-
},
|
|
39
|
-
}).concat(...middlewares),
|
|
40
|
-
devTools: isDev || isDebug,
|
|
41
|
-
preloadedState: initialState,
|
|
42
|
-
enhancers: (getDefaultEnhancers) => getDefaultEnhancers().concat(...enhancers),
|
|
43
|
-
});
|
|
44
|
-
if ((isBrowser || isElectronMain) && epicMiddleware && rootEpic) {
|
|
45
|
-
epicMiddleware.run(rootEpic);
|
|
42
|
+
const persistedReducer = persistConfig ? persistReducer(persistConfig, rootReducer) : rootReducer;
|
|
43
|
+
const store = createStore(persistedReducer, initialState, composeEnhancers(...enhancers()));
|
|
44
|
+
if (isBrowser || isElectronMain) {
|
|
45
|
+
// no SSR for now
|
|
46
|
+
if (epicMiddleware) {
|
|
47
|
+
epicMiddleware.run(rootEpic);
|
|
48
|
+
}
|
|
46
49
|
}
|
|
47
50
|
return store;
|
|
48
51
|
};export{createReduxStore};
|
|
@@ -30,8 +30,8 @@ const createClientContainer = (req, res) => {
|
|
|
30
30
|
httpGraphqlURL: config.GRAPHQL_URL,
|
|
31
31
|
httpLocalGraphqlURL: config.LOCAL_GRAPHQL_URL,
|
|
32
32
|
isDev: process.env.NODE_ENV === 'development',
|
|
33
|
-
isDebug:
|
|
34
|
-
isSSR:
|
|
33
|
+
isDebug: false,
|
|
34
|
+
isSSR: true,
|
|
35
35
|
scope: typeof window !== 'undefined' ? 'browser' : 'server',
|
|
36
36
|
clientState,
|
|
37
37
|
getDataIdFromObject: (result) => features.getDataIdFromObject(result),
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import {cleanEnv,str}from'envalid';import {getEnvironment}from'@common-stack/core';const env = getEnvironment();
|
|
2
2
|
const config = cleanEnv(env, {
|
|
3
3
|
GA_ID: str({ devDefault: 'G-xxxxxxx' }),
|
|
4
|
-
GRAPHQL_URL: str({
|
|
5
|
-
LOCAL_GRAPHQL_URL: str({
|
|
4
|
+
GRAPHQL_URL: str({}),
|
|
5
|
+
LOCAL_GRAPHQL_URL: str({}),
|
|
6
6
|
GRAPHQL_SUBSCRIPTION_URL: str({ default: env?.GRAPHQL_URL?.replace(/^http/, 'ws') }),
|
|
7
7
|
LOG_LEVEL: str({ devDefault: 'debug' }),
|
|
8
8
|
});export{config};
|
|
@@ -22,5 +22,5 @@ export declare const persistConfig: {
|
|
|
22
22
|
* `combineReducers`
|
|
23
23
|
*/
|
|
24
24
|
export declare const createReduxStore: (history: any, apolloClient: any, services: any, container: any) => {
|
|
25
|
-
store: import("
|
|
25
|
+
store: import("redux").Store<any, import("redux").AnyAction>;
|
|
26
26
|
};
|
|
@@ -24,7 +24,7 @@ const createReduxStore = (history, apolloClient, services, container) => {
|
|
|
24
24
|
// middleware
|
|
25
25
|
const store = createReduxStore$1({
|
|
26
26
|
scope: 'browser',
|
|
27
|
-
isDebug:
|
|
27
|
+
isDebug: false,
|
|
28
28
|
isDev: process.env.NODE_ENV === 'development',
|
|
29
29
|
initialState: {},
|
|
30
30
|
persistConfig,
|
package/lib/utils/index.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import {ClientLogger}from'@cdm-logger/client';export{default}from'../modules.js';import {navigate}from'./navigator.js';/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
2
|
/* eslint-disable import/no-cycle */
|
|
3
3
|
/* eslint-disable @typescript-eslint/no-var-requires */
|
|
4
|
-
Object.assign(global, require('../../build.config'));
|
|
5
4
|
class UtilityClass {
|
|
6
5
|
modules;
|
|
7
6
|
// tslint:disable-next-line:no-shadowed-variable
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@common-stack/mobile-stack-react",
|
|
3
|
-
"version": "6.0.6-alpha.
|
|
3
|
+
"version": "6.0.6-alpha.8",
|
|
4
4
|
"description": "Client Module for mobile app",
|
|
5
5
|
"homepage": "https://github.com/cdmbase/fullstack-pro#readme",
|
|
6
6
|
"bugs": {
|
|
@@ -131,5 +131,5 @@
|
|
|
131
131
|
"typescript": {
|
|
132
132
|
"definition": "lib/index.d.ts"
|
|
133
133
|
},
|
|
134
|
-
"gitHead": "
|
|
134
|
+
"gitHead": "c1149e5c09ea4f3d84ff840b19588a1348aba561"
|
|
135
135
|
}
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import {InMemoryCache}from'@apollo/client/cache/index.js';// cache.ts
|
|
2
|
-
const createCache = ({ getDataIdFromObject, clientState }) => {
|
|
3
|
-
const cache = new InMemoryCache({
|
|
4
|
-
dataIdFromObject: getDataIdFromObject,
|
|
5
|
-
possibleTypes: clientState.possibleTypes,
|
|
6
|
-
typePolicies: clientState.typePolicies,
|
|
7
|
-
});
|
|
8
|
-
return cache;
|
|
9
|
-
};
|
|
10
|
-
const initializeCache = ({ cache, initialState, clientState, logger, }) => {
|
|
11
|
-
if (initialState) {
|
|
12
|
-
try {
|
|
13
|
-
cache.restore(initialState);
|
|
14
|
-
logger.debug('Cache restored with initial state');
|
|
15
|
-
}
|
|
16
|
-
catch (err) {
|
|
17
|
-
logger.error('Error restoring cache', err);
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
else {
|
|
21
|
-
clientState.defaults?.forEach((x) => {
|
|
22
|
-
try {
|
|
23
|
-
if (x.type === 'query') {
|
|
24
|
-
cache.writeQuery({ query: x.query, data: x.data });
|
|
25
|
-
}
|
|
26
|
-
else if (x.type === 'fragment') {
|
|
27
|
-
cache.writeFragment({ id: x.id, fragment: x.fragment, data: x.data });
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
catch (err) {
|
|
31
|
-
logger.error('Error writing to cache', err);
|
|
32
|
-
}
|
|
33
|
-
});
|
|
34
|
-
}
|
|
35
|
-
};export{createCache,initializeCache};
|