@trackunit/react-core-contexts 0.4.666 → 0.4.668

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 (3) hide show
  1. package/index.cjs.js +49 -13
  2. package/index.esm.js +50 -14
  3. package/package.json +5 -3
package/index.cjs.js CHANGED
@@ -5,8 +5,11 @@ var client = require('@apollo/client');
5
5
  var context = require('@apollo/client/link/context');
6
6
  var error = require('@apollo/client/link/error');
7
7
  var removeTypename = require('@apollo/client/link/remove-typename');
8
+ var utilities = require('@apollo/client/utilities');
8
9
  var reactCoreHooks = require('@trackunit/react-core-hooks');
9
10
  var apolloUploadClient = require('apollo-upload-client');
11
+ var graphql = require('graphql');
12
+ var graphqlSse = require('graphql-sse');
10
13
  var React = require('react');
11
14
  require('@js-temporal/polyfill');
12
15
  var i18nLibraryTranslation = require('@trackunit/i18n-library-translation');
@@ -37,19 +40,19 @@ const createApolloClient = ({ graphqlManagerUrl, graphqlPublicUrl, graphqlManage
37
40
  if (!token) {
38
41
  token = firstToken;
39
42
  }
40
- const managerGraphQLLink = apolloUploadClient.createUploadLink({
43
+ const managerGraphQLLink = client.createHttpLink({
41
44
  uri: request => graphqlManagerUrl + "/" + request.operationName,
42
45
  });
43
- const publicGraphQLLink = apolloUploadClient.createUploadLink({
46
+ const publicGraphQLLink = client.createHttpLink({
44
47
  uri: request => graphqlPublicUrl + "/" + request.operationName,
45
48
  });
46
49
  const imageUploadGraphQLLink = apolloUploadClient.createUploadLink({
47
50
  uri: request => graphqlManagerImageUploadUrl + "/" + request.operationName,
48
51
  });
49
- const internalGraphQLLink = apolloUploadClient.createUploadLink({
52
+ const internalGraphQLLink = client.createHttpLink({
50
53
  uri: request => graphqlInternalUrl + "/" + request.operationName,
51
54
  });
52
- const reportGraphQLLink = apolloUploadClient.createUploadLink({
55
+ const reportGraphQLLink = client.createHttpLink({
53
56
  uri: request => graphqlReportUrl + "/" + request.operationName,
54
57
  });
55
58
  const authLink = context.setContext(async (_, { headers }) => {
@@ -85,7 +88,7 @@ const createApolloClient = ({ graphqlManagerUrl, graphqlPublicUrl, graphqlManage
85
88
  window.location.reload();
86
89
  }
87
90
  // eslint-disable-next-line no-console
88
- console.error(`Error calling: '${errorResponse.operation.getContext().clientAwareness.name}' fetching Data for: ${errorResponse.operation.operationName}`, errorResponse.graphQLErrors);
91
+ console.error(`Error calling: '${errorResponse.operation.getContext().response.url}'`, errorResponse.graphQLErrors);
89
92
  errorResponse.graphQLErrors.forEach(error => {
90
93
  var _a, _b;
91
94
  if ("extensions" in error) {
@@ -112,7 +115,10 @@ const createApolloClient = ({ graphqlManagerUrl, graphqlPublicUrl, graphqlManage
112
115
  },
113
116
  });
114
117
  const invalidToken = errorResponse.graphQLErrors.some(x => {
115
- return (x.extensions.code === "UNAUTHENTICATED" ||
118
+ return (
119
+ // saw it not unnecessary
120
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
121
+ (x.extensions && x.extensions.code === "UNAUTHENTICATED") ||
116
122
  x.message.includes("Invalid token specified") ||
117
123
  x.message.includes("Access denied! You need to be authorized to perform this action!"));
118
124
  });
@@ -150,6 +156,42 @@ const createApolloClient = ({ graphqlManagerUrl, graphqlPublicUrl, graphqlManage
150
156
  },
151
157
  };
152
158
  const removeTypenameLink = removeTypename.removeTypenameFromVariables();
159
+ class SSELink extends client.ApolloLink {
160
+ constructor(options) {
161
+ super();
162
+ this.client = graphqlSse.createClient(options);
163
+ }
164
+ request(operation) {
165
+ return new utilities.Observable(sink => {
166
+ return this.client.subscribe({ ...operation, query: graphql.print(operation.query) }, {
167
+ next: sink.next.bind(sink),
168
+ complete: sink.complete.bind(sink),
169
+ error: sink.error.bind(sink),
170
+ });
171
+ });
172
+ }
173
+ }
174
+ const sseLink = new SSELink({
175
+ url: `${graphqlInternalUrl}/stream`,
176
+ headers: () => {
177
+ return {
178
+ Authorization: `Bearer ${token}`,
179
+ };
180
+ },
181
+ });
182
+ // Split links based on operation type
183
+ const splitLink = client.from([
184
+ authLink,
185
+ client.split(({ query }) => {
186
+ const definition = utilities.getMainDefinition(query);
187
+ return definition.kind === "OperationDefinition" && definition.operation === "subscription";
188
+ }, sseLink, client.from([
189
+ errorLink,
190
+ removeTypenameLink,
191
+ client.split(operation => operation.getContext().clientName === "imageupload", imageUploadGraphQLLink),
192
+ client.split(operation => operation.getContext().clientName === "manager", managerGraphQLLink, client.split(operation => operation.getContext().clientName === "report", reportGraphQLLink, client.split(() => global.gql === "internal", internalGraphQLLink, publicGraphQLLink))),
193
+ ])),
194
+ ]);
153
195
  return {
154
196
  client: new client.ApolloClient({
155
197
  name: "Manager",
@@ -164,13 +206,7 @@ const createApolloClient = ({ graphqlManagerUrl, graphqlPublicUrl, graphqlManage
164
206
  },
165
207
  }),
166
208
  defaultOptions,
167
- link: client.from([
168
- errorLink,
169
- authLink,
170
- removeTypenameLink,
171
- client.split(operation => operation.getContext().clientName === "imageupload", imageUploadGraphQLLink),
172
- client.split(operation => operation.getContext().clientName === "manager", managerGraphQLLink, client.split(operation => operation.getContext().clientName === "report", reportGraphQLLink, client.split(() => global.gql === "internal", internalGraphQLLink, publicGraphQLLink))),
173
- ]),
209
+ link: splitLink,
174
210
  }),
175
211
  setToken: (newToken) => {
176
212
  token = newToken;
package/index.esm.js CHANGED
@@ -1,10 +1,13 @@
1
1
  import { jsx, Fragment } from 'react/jsx-runtime';
2
- import { ApolloProvider, ApolloClient, InMemoryCache, from, split } from '@apollo/client';
2
+ import { ApolloProvider, createHttpLink, from, split, ApolloClient, InMemoryCache, ApolloLink } from '@apollo/client';
3
3
  import { setContext } from '@apollo/client/link/context';
4
4
  import { onError } from '@apollo/client/link/error';
5
5
  import { removeTypenameFromVariables } from '@apollo/client/link/remove-typename';
6
+ import { getMainDefinition, Observable } from '@apollo/client/utilities';
6
7
  import { useEnvironment, useToken, useErrorHandler, AnalyticsContextProvider, AssetSortingProvider, ConfirmationDialogProvider, EnvironmentContextProvider, ErrorHandlingContextProvider, FilterBarProvider, ModalDialogContextProvider, NavigationContextProvider, OemBrandingContextProvider, ToastProvider, TokenProvider, CurrentUserProvider, CurrentUserPreferenceProvider, UserSubscriptionProvider } from '@trackunit/react-core-hooks';
7
8
  import { createUploadLink } from 'apollo-upload-client';
9
+ import { print } from 'graphql';
10
+ import { createClient } from 'graphql-sse';
8
11
  import * as React from 'react';
9
12
  import { useRef, useMemo, useEffect, useState } from 'react';
10
13
  import '@js-temporal/polyfill';
@@ -17,19 +20,19 @@ const createApolloClient = ({ graphqlManagerUrl, graphqlPublicUrl, graphqlManage
17
20
  if (!token) {
18
21
  token = firstToken;
19
22
  }
20
- const managerGraphQLLink = createUploadLink({
23
+ const managerGraphQLLink = createHttpLink({
21
24
  uri: request => graphqlManagerUrl + "/" + request.operationName,
22
25
  });
23
- const publicGraphQLLink = createUploadLink({
26
+ const publicGraphQLLink = createHttpLink({
24
27
  uri: request => graphqlPublicUrl + "/" + request.operationName,
25
28
  });
26
29
  const imageUploadGraphQLLink = createUploadLink({
27
30
  uri: request => graphqlManagerImageUploadUrl + "/" + request.operationName,
28
31
  });
29
- const internalGraphQLLink = createUploadLink({
32
+ const internalGraphQLLink = createHttpLink({
30
33
  uri: request => graphqlInternalUrl + "/" + request.operationName,
31
34
  });
32
- const reportGraphQLLink = createUploadLink({
35
+ const reportGraphQLLink = createHttpLink({
33
36
  uri: request => graphqlReportUrl + "/" + request.operationName,
34
37
  });
35
38
  const authLink = setContext(async (_, { headers }) => {
@@ -65,7 +68,7 @@ const createApolloClient = ({ graphqlManagerUrl, graphqlPublicUrl, graphqlManage
65
68
  window.location.reload();
66
69
  }
67
70
  // eslint-disable-next-line no-console
68
- console.error(`Error calling: '${errorResponse.operation.getContext().clientAwareness.name}' fetching Data for: ${errorResponse.operation.operationName}`, errorResponse.graphQLErrors);
71
+ console.error(`Error calling: '${errorResponse.operation.getContext().response.url}'`, errorResponse.graphQLErrors);
69
72
  errorResponse.graphQLErrors.forEach(error => {
70
73
  var _a, _b;
71
74
  if ("extensions" in error) {
@@ -92,7 +95,10 @@ const createApolloClient = ({ graphqlManagerUrl, graphqlPublicUrl, graphqlManage
92
95
  },
93
96
  });
94
97
  const invalidToken = errorResponse.graphQLErrors.some(x => {
95
- return (x.extensions.code === "UNAUTHENTICATED" ||
98
+ return (
99
+ // saw it not unnecessary
100
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
101
+ (x.extensions && x.extensions.code === "UNAUTHENTICATED") ||
96
102
  x.message.includes("Invalid token specified") ||
97
103
  x.message.includes("Access denied! You need to be authorized to perform this action!"));
98
104
  });
@@ -130,6 +136,42 @@ const createApolloClient = ({ graphqlManagerUrl, graphqlPublicUrl, graphqlManage
130
136
  },
131
137
  };
132
138
  const removeTypenameLink = removeTypenameFromVariables();
139
+ class SSELink extends ApolloLink {
140
+ constructor(options) {
141
+ super();
142
+ this.client = createClient(options);
143
+ }
144
+ request(operation) {
145
+ return new Observable(sink => {
146
+ return this.client.subscribe({ ...operation, query: print(operation.query) }, {
147
+ next: sink.next.bind(sink),
148
+ complete: sink.complete.bind(sink),
149
+ error: sink.error.bind(sink),
150
+ });
151
+ });
152
+ }
153
+ }
154
+ const sseLink = new SSELink({
155
+ url: `${graphqlInternalUrl}/stream`,
156
+ headers: () => {
157
+ return {
158
+ Authorization: `Bearer ${token}`,
159
+ };
160
+ },
161
+ });
162
+ // Split links based on operation type
163
+ const splitLink = from([
164
+ authLink,
165
+ split(({ query }) => {
166
+ const definition = getMainDefinition(query);
167
+ return definition.kind === "OperationDefinition" && definition.operation === "subscription";
168
+ }, sseLink, from([
169
+ errorLink,
170
+ removeTypenameLink,
171
+ split(operation => operation.getContext().clientName === "imageupload", imageUploadGraphQLLink),
172
+ split(operation => operation.getContext().clientName === "manager", managerGraphQLLink, split(operation => operation.getContext().clientName === "report", reportGraphQLLink, split(() => global.gql === "internal", internalGraphQLLink, publicGraphQLLink))),
173
+ ])),
174
+ ]);
133
175
  return {
134
176
  client: new ApolloClient({
135
177
  name: "Manager",
@@ -144,13 +186,7 @@ const createApolloClient = ({ graphqlManagerUrl, graphqlPublicUrl, graphqlManage
144
186
  },
145
187
  }),
146
188
  defaultOptions,
147
- link: from([
148
- errorLink,
149
- authLink,
150
- removeTypenameLink,
151
- split(operation => operation.getContext().clientName === "imageupload", imageUploadGraphQLLink),
152
- split(operation => operation.getContext().clientName === "manager", managerGraphQLLink, split(operation => operation.getContext().clientName === "report", reportGraphQLLink, split(() => global.gql === "internal", internalGraphQLLink, publicGraphQLLink))),
153
- ]),
189
+ link: splitLink,
154
190
  }),
155
191
  setToken: (newToken) => {
156
192
  token = newToken;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trackunit/react-core-contexts",
3
- "version": "0.4.666",
3
+ "version": "0.4.668",
4
4
  "repository": "https://github.com/Trackunit/manager",
5
5
  "license": "SEE LICENSE IN LICENSE.txt",
6
6
  "engines": {
@@ -10,14 +10,16 @@
10
10
  "@apollo/client": "3.10.4",
11
11
  "@trackunit/react-core-contexts-api": "*",
12
12
  "@trackunit/react-core-hooks": "*",
13
- "apollo-upload-client": "^17.0.0",
14
13
  "react": "18.3.1",
15
14
  "@js-temporal/polyfill": "^0.4.4",
16
15
  "@trackunit/iris-app-runtime-core": "*",
17
16
  "jest-fetch-mock": "^3.0.3",
17
+ "graphql-sse": "^2.5.3",
18
+ "graphql": "^16.9.0",
18
19
  "@trackunit/i18n-library-translation": "*",
19
20
  "@trackunit/react-components": "*",
20
- "@trackunit/iris-app-api": "*"
21
+ "@trackunit/iris-app-api": "*",
22
+ "apollo-upload-client": "^17.0.0"
21
23
  },
22
24
  "module": "./index.esm.js",
23
25
  "main": "./index.cjs.js",