@jsb188/graphql 1.0.4 → 1.1.3

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/sse.ts CHANGED
@@ -1,5 +1,5 @@
1
+ import { addFragmentToCache, updateFragment, updateQuery } from './cache/';
1
2
  import type { UpdateObserversFn } from './types.d';
2
- import { loadFragment, updateQuery, updateFragment, addFragmentToCache } from './cache/';
3
3
 
4
4
  /**
5
5
  * Types
@@ -14,12 +14,13 @@ type ValidSSEType = (
14
14
  'DATA_APPEND' | 'DATA_PREPEND' | 'DATA_UPDATE'
15
15
  );
16
16
 
17
- interface PublishPayload {
17
+ export interface PublishPayload {
18
18
  __type: ValidSSEType;
19
19
  fragmentKey: string;
20
20
  fragmentNamespace?: string;
21
21
  dataFragmentKey?: string;
22
22
  data: Record<string, any>;
23
+ otherData?: Record<string, any>;
23
24
  }
24
25
 
25
26
  /**
@@ -28,10 +29,10 @@ interface PublishPayload {
28
29
 
29
30
  function handleQueryConcat(payload: PublishPayload, updateObservers?: UpdateObserversFn) {
30
31
 
31
- const { __type, fragmentKey, fragmentNamespace, dataFragmentKey, data } = payload;
32
+ const { __type, fragmentKey, fragmentNamespace, dataFragmentKey, data, otherData } = payload;
33
+
32
34
  if (fragmentKey && fragmentNamespace && dataFragmentKey && data) {
33
35
  const isAppend = __type === 'QUERY_APPEND';
34
-
35
36
  const updateData = (currentData: any) => {
36
37
  const currentList = currentData?.[fragmentNamespace]?.data;
37
38
  if (
@@ -41,9 +42,9 @@ function handleQueryConcat(payload: PublishPayload, updateObservers?: UpdateObse
41
42
  ) {
42
43
  addFragmentToCache(dataFragmentKey, data);
43
44
  const updatedList = isAppend ? [...currentList, [dataFragmentKey]] : [[dataFragmentKey], ...currentList];
44
-
45
45
  return {
46
46
  ...currentData,
47
+ ...otherData,
47
48
  [fragmentNamespace]: {
48
49
  ...currentData?.[fragmentNamespace],
49
50
  data: updatedList
@@ -60,6 +61,8 @@ function handleQueryConcat(payload: PublishPayload, updateObservers?: UpdateObse
60
61
  updateObservers
61
62
  );
62
63
  }
64
+
65
+ return null;
63
66
  }
64
67
 
65
68
  /**
@@ -67,30 +70,29 @@ function handleQueryConcat(payload: PublishPayload, updateObservers?: UpdateObse
67
70
  */
68
71
 
69
72
  function handleFragmentConcat(payload: PublishPayload, updateObservers?: UpdateObserversFn) {
70
- const { __type, fragmentKey, fragmentNamespace, data } = payload;
73
+ const { __type, fragmentKey, fragmentNamespace, data, otherData } = payload;
71
74
  const isAppend = __type === 'DATA_APPEND';
72
75
 
73
76
  if (fragmentKey && fragmentNamespace && data) {
74
77
  const namespaceType = typeof fragmentNamespace;
75
78
  const updateData = (currentData: any) => {
76
79
  if (typeof currentData[fragmentNamespace] === namespaceType) {
77
-
78
80
  let nextValue;
79
81
  if (namespaceType === 'string') {
80
82
  nextValue = isAppend ? `${currentData[fragmentNamespace]}${data}` : `${data}${currentData[fragmentNamespace]}`;
81
83
  } else if (Array.isArray(currentData[fragmentNamespace])) {
82
84
  nextValue = isAppend ? [...currentData[fragmentNamespace], data] : [data, ...currentData[fragmentNamespace]];
83
85
  }
84
-
85
86
  return {
86
87
  ...currentData,
88
+ ...otherData,
87
89
  [fragmentNamespace]: nextValue
88
90
  }
89
91
  }
90
92
  return currentData;
91
93
  };
92
94
 
93
- updateFragment(fragmentKey, updateData, null, isAppend, updateObservers);
95
+ updateFragment(fragmentKey, updateData, null, false, updateObservers);
94
96
  }
95
97
 
96
98
  }
@@ -102,11 +104,6 @@ function handleFragmentConcat(payload: PublishPayload, updateObservers?: UpdateO
102
104
  function handleFragmentUpdate(payload: PublishPayload, updateObservers?: UpdateObserversFn) {
103
105
  const { fragmentKey, dataFragmentKey, data } = payload;
104
106
  updateFragment(dataFragmentKey || fragmentKey, data, null, false, updateObservers);
105
-
106
- console.log(
107
- loadFragment(fragmentKey),
108
- );
109
-
110
107
  }
111
108
 
112
109
  /**
@@ -114,22 +111,18 @@ function handleFragmentUpdate(payload: PublishPayload, updateObservers?: UpdateO
114
111
  */
115
112
 
116
113
  function handleSSEData(payload: PublishPayload, updateObservers?: UpdateObserversFn) {
117
-
118
114
  switch (payload.__type) {
119
115
  case 'CONNECTED':
120
116
  // Do nothing
121
117
  break;
122
118
  case 'QUERY_APPEND':
123
119
  case 'QUERY_PREPEND':
124
- handleQueryConcat(payload, updateObservers)
125
- break;
120
+ return handleQueryConcat(payload, updateObservers)
126
121
  case 'DATA_APPEND':
127
122
  case 'DATA_PREPEND':
128
- handleFragmentConcat(payload, updateObservers)
129
- break;
123
+ return handleFragmentConcat(payload, updateObservers)
130
124
  case 'DATA_UPDATE':
131
- handleFragmentUpdate(payload, updateObservers)
132
- break;
125
+ return handleFragmentUpdate(payload, updateObservers)
133
126
  default:
134
127
  console.log('Unknown {__type} in handleSSEData():', payload.__type);
135
128
  break;
package/types.d.ts CHANGED
@@ -1,4 +1,8 @@
1
- import type { ServerErrorObj, SimpleErrorType } from '@jsb188/app/types/app';
1
+ import type { ServerErrorObj, SimpleErrorType } from '@jsb188/app/types/app.d';
2
+
3
+ /**
4
+ * GraphQL API requests
5
+ */
2
6
 
3
7
  export interface APIRequestOutput {
4
8
  aborted?: boolean;
@@ -18,12 +22,18 @@ export interface UpdateDataObserverArgs {
18
22
  forceRefetch?: boolean;
19
23
  }
20
24
 
25
+ /**
26
+ * GraphQL client handlers
27
+ */
28
+
21
29
  export type AnyModalPopUpFn = (data: any, err?: ServerErrorObj) => void;
22
30
  export type UpdateObserversFn = (args: UpdateDataObserverArgs) => void;
23
- export type OnCompletedGQLFn = (data: any, error: ServerErrorObj | null, variables: any) => void;
31
+ export type OnCompletedGQLFn = (data: any, error: ServerErrorObj | null, variables?: any) => void;
24
32
  export type OnErrorGQLFn = (error: ServerErrorObj | null, variables: any) => void;
25
33
 
26
- // GraphQL query hook options
34
+ /**
35
+ * GraphQL query hook params
36
+ */
27
37
 
28
38
  export interface GraphQLQueryOptions {
29
39
  name?: string;
@@ -39,19 +49,26 @@ export interface GraphQLQueryOptions {
39
49
  }
40
50
 
41
51
  // Params for most GraphQL (query) hooks
42
-
43
52
  export type UseQueryParams = Omit<GraphQLQueryOptions, 'name' | 'retryCount' | 'cacheMap'>;
44
53
 
45
54
  // Params for most GraphQL (mutation) hooks
46
-
47
55
  export type UseMutationParams = Partial<{
48
56
  onCompleted: OnCompletedGQLFn;
49
57
  onError: OnErrorGQLFn;
50
58
  }>;
51
59
 
52
-
53
60
  // Params for most GraphQL hooks with these options
54
-
55
61
  export type UseMutationParamsExtra = Partial<{
56
62
  checkMountedBeforeCallback?: boolean;
57
63
  }> & UseMutationParams;
64
+
65
+ /**
66
+ * Common query patterns
67
+ */
68
+
69
+ export type PaginationArgs = Partial<{
70
+ cursor: [number | null, number | null];
71
+ after: boolean;
72
+ limit: number;
73
+ doNotThrowError: boolean;
74
+ }>;
package/utils.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { getENVVariable } from '@jsb188/app';
2
- import type { ServerErrorObj } from '@jsb188/app/types/app';
2
+ import type { ServerErrorObj } from '@jsb188/app/types/app.d';
3
3
 
4
4
  const TIME_TRACKER = new Map();
5
5
 
@@ -60,25 +60,28 @@ export function isServerErrorGQL(apiResult: any): boolean {
60
60
  * Headers for all network requests
61
61
  */
62
62
 
63
- export function apiRequestHeaders(authorization: string | null | undefined): any {
64
- const headers = {
63
+ export function apiRequestHeaders(authorization?: string | null, ipAddress?: string | null): Headers {
64
+ const headers: Record<string, string> = {
65
65
  'Content-Type': 'application/json; charset=utf-8',
66
- api_key: getENVVariable('GQL_API_KEY'),
67
- origin: getENVVariable('APP_URL'),
68
- 'Access-Control-Allow-Origin': getENVVariable('APP_URL'),
66
+ api_key: String(getENVVariable('GQL_API_KEY') || ''),
67
+ origin: String(getENVVariable('APP_URL') || ''),
68
+ 'Access-Control-Allow-Origin': String(getENVVariable('APP_URL') || ''),
69
69
  // recaptcha: ? // Not sure if we're going to do this later any more
70
70
  };
71
71
 
72
72
  if (!getENVVariable('IS_BROWSER')) {
73
- // @ts-expect-error Can't allow undefined or null
74
73
  headers.ssr = '1';
74
+
75
+ if (ipAddress) {
76
+ // Do not allow undefined or null, it becomes a String type
77
+ headers['x-ssr-ip'] = ipAddress;
78
+ }
75
79
  }
76
80
 
77
81
  if (authorization) {
78
- // Do not send undefined or null, it will become a String type
79
- // @ts-expect-error Can't allow undefined or null
82
+ // Do not send undefined or null, it becomes a String type
80
83
  headers.authorization = authorization;
81
84
  }
82
85
 
83
- return headers;
86
+ return new Headers(headers);
84
87
  }
File without changes