@trackunit/react-core-contexts 1.3.103 → 1.3.106

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/index.cjs.js CHANGED
@@ -5,7 +5,10 @@ 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');
10
+ var graphql = require('graphql');
11
+ var graphqlSse = require('graphql-sse');
9
12
  var React = require('react');
10
13
  require('@js-temporal/polyfill');
11
14
  var i18nLibraryTranslation = require('@trackunit/i18n-library-translation');
@@ -152,11 +155,46 @@ const createApolloClient = ({ graphqlManagerUrl, graphqlPublicUrl, graphqlIntern
152
155
  },
153
156
  };
154
157
  const removeTypenameLink = removeTypename.removeTypenameFromVariables();
158
+ class SSELink extends client.ApolloLink {
159
+ constructor(options) {
160
+ super();
161
+ this.client = graphqlSse.createClient(options);
162
+ }
163
+ request(operation) {
164
+ return new utilities.Observable(sink => {
165
+ return this.client.subscribe({ ...operation, query: graphql.print(operation.query) }, {
166
+ next: sink.next.bind(sink),
167
+ complete: sink.complete.bind(sink),
168
+ error: sink.error.bind(sink),
169
+ });
170
+ });
171
+ }
172
+ }
173
+ const sseLink = new SSELink({
174
+ url: `${graphqlInternalUrl}/stream`,
175
+ headers: () => {
176
+ return {
177
+ Authorization: `Bearer ${token}`,
178
+ };
179
+ },
180
+ });
181
+ // Split links based on operation type
182
+ const splitLink = client.from([
183
+ authLink,
184
+ client.split(({ query }) => {
185
+ const definition = utilities.getMainDefinition(query);
186
+ return definition.kind === "OperationDefinition" && definition.operation === "subscription";
187
+ }, sseLink, client.from([
188
+ errorLink,
189
+ removeTypenameLink,
190
+ client.split(operation => operation.getContext().clientName === "manager", managerGraphQLLink, client.split(operation => operation.getContext().clientName === "report", reportGraphQLLink, client.split(() => global.gql === "internal", internalGraphQLLink, publicGraphQLLink))),
191
+ ])),
192
+ ]);
155
193
  return {
156
194
  client: new client.ApolloClient({
157
195
  name: "Manager",
158
196
  connectToDevTools: isDev,
159
- // LegacyServicePlan added by team SPIEX, now owned by team Luna, so Apollo Client doesn't merge service plans simply because they have the same id
197
+ // LegacyServicePlan added by team SPIEX so Apollo Client doesn't merge service plans simply because they have the same id
160
198
  // See more here: https://stackoverflow.com/questions/78189868/apollo-is-returning-data-wrong-but-is-correct-in-network-inspector
161
199
  cache: new client.InMemoryCache({
162
200
  typePolicies: {
@@ -166,12 +204,7 @@ const createApolloClient = ({ graphqlManagerUrl, graphqlPublicUrl, graphqlIntern
166
204
  },
167
205
  }),
168
206
  defaultOptions,
169
- link: client.from([
170
- errorLink,
171
- authLink,
172
- removeTypenameLink,
173
- client.split(operation => operation.getContext().clientName === "manager", managerGraphQLLink, client.split(operation => operation.getContext().clientName === "report", reportGraphQLLink, client.split(() => global.gql === "internal", internalGraphQLLink, publicGraphQLLink))),
174
- ]),
207
+ link: splitLink,
175
208
  }),
176
209
  setToken: (newToken) => {
177
210
  token = newToken;
package/index.esm.js CHANGED
@@ -1,9 +1,12 @@
1
1
  import { jsx, Fragment } from 'react/jsx-runtime';
2
- import { ApolloProvider, createHttpLink, 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, TimeRangeProvider, ToastProvider, TokenProvider, CurrentUserProvider, CurrentUserPreferenceProvider, UserSubscriptionProvider, WidgetConfigProvider } from '@trackunit/react-core-hooks';
8
+ import { print } from 'graphql';
9
+ import { createClient } from 'graphql-sse';
7
10
  import * as React from 'react';
8
11
  import { useState, useMemo, useEffect, Suspense } from 'react';
9
12
  import '@js-temporal/polyfill';
@@ -132,11 +135,46 @@ const createApolloClient = ({ graphqlManagerUrl, graphqlPublicUrl, graphqlIntern
132
135
  },
133
136
  };
134
137
  const removeTypenameLink = removeTypenameFromVariables();
138
+ class SSELink extends ApolloLink {
139
+ constructor(options) {
140
+ super();
141
+ this.client = createClient(options);
142
+ }
143
+ request(operation) {
144
+ return new Observable(sink => {
145
+ return this.client.subscribe({ ...operation, query: print(operation.query) }, {
146
+ next: sink.next.bind(sink),
147
+ complete: sink.complete.bind(sink),
148
+ error: sink.error.bind(sink),
149
+ });
150
+ });
151
+ }
152
+ }
153
+ const sseLink = new SSELink({
154
+ url: `${graphqlInternalUrl}/stream`,
155
+ headers: () => {
156
+ return {
157
+ Authorization: `Bearer ${token}`,
158
+ };
159
+ },
160
+ });
161
+ // Split links based on operation type
162
+ const splitLink = from([
163
+ authLink,
164
+ split(({ query }) => {
165
+ const definition = getMainDefinition(query);
166
+ return definition.kind === "OperationDefinition" && definition.operation === "subscription";
167
+ }, sseLink, from([
168
+ errorLink,
169
+ removeTypenameLink,
170
+ split(operation => operation.getContext().clientName === "manager", managerGraphQLLink, split(operation => operation.getContext().clientName === "report", reportGraphQLLink, split(() => global.gql === "internal", internalGraphQLLink, publicGraphQLLink))),
171
+ ])),
172
+ ]);
135
173
  return {
136
174
  client: new ApolloClient({
137
175
  name: "Manager",
138
176
  connectToDevTools: isDev,
139
- // LegacyServicePlan added by team SPIEX, now owned by team Luna, so Apollo Client doesn't merge service plans simply because they have the same id
177
+ // LegacyServicePlan added by team SPIEX so Apollo Client doesn't merge service plans simply because they have the same id
140
178
  // See more here: https://stackoverflow.com/questions/78189868/apollo-is-returning-data-wrong-but-is-correct-in-network-inspector
141
179
  cache: new InMemoryCache({
142
180
  typePolicies: {
@@ -146,12 +184,7 @@ const createApolloClient = ({ graphqlManagerUrl, graphqlPublicUrl, graphqlIntern
146
184
  },
147
185
  }),
148
186
  defaultOptions,
149
- link: from([
150
- errorLink,
151
- authLink,
152
- removeTypenameLink,
153
- split(operation => operation.getContext().clientName === "manager", managerGraphQLLink, split(operation => operation.getContext().clientName === "report", reportGraphQLLink, split(() => global.gql === "internal", internalGraphQLLink, publicGraphQLLink))),
154
- ]),
187
+ link: splitLink,
155
188
  }),
156
189
  setToken: (newToken) => {
157
190
  token = newToken;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trackunit/react-core-contexts",
3
- "version": "1.3.103",
3
+ "version": "1.3.106",
4
4
  "repository": "https://github.com/Trackunit/manager",
5
5
  "license": "SEE LICENSE IN LICENSE.txt",
6
6
  "engines": {
@@ -11,12 +11,14 @@
11
11
  "react": "19.0.0",
12
12
  "@js-temporal/polyfill": "^0.4.4",
13
13
  "jest-fetch-mock": "^3.0.3",
14
- "@trackunit/iris-app-api": "1.3.79",
15
- "@trackunit/react-core-contexts-api": "1.4.79",
16
- "@trackunit/react-core-hooks": "1.3.81",
17
- "@trackunit/i18n-library-translation": "1.3.84",
18
- "@trackunit/react-components": "1.4.92",
19
- "@trackunit/iris-app-runtime-core": "1.4.80"
14
+ "@trackunit/iris-app-api": "1.3.80",
15
+ "@trackunit/react-core-contexts-api": "1.4.80",
16
+ "@trackunit/react-core-hooks": "1.3.82",
17
+ "@trackunit/i18n-library-translation": "1.3.85",
18
+ "@trackunit/react-components": "1.4.93",
19
+ "@trackunit/iris-app-runtime-core": "1.4.81",
20
+ "graphql": "^16.10.0",
21
+ "graphql-sse": "^2.5.4"
20
22
  },
21
23
  "module": "./index.esm.js",
22
24
  "main": "./index.cjs.js",