@cedarjs/web 6.0.0-canary.2661 → 6.0.0-canary.2663

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 (41) hide show
  1. package/dist/apollo/ApolloProviderWithFetchConfig.d.ts +15 -0
  2. package/dist/apollo/ApolloProviderWithFetchConfig.d.ts.map +1 -0
  3. package/dist/apollo/ApolloProviderWithFetchConfig.js +134 -0
  4. package/dist/apollo/CedarApolloProvider.d.ts +14 -0
  5. package/dist/apollo/CedarApolloProvider.d.ts.map +1 -0
  6. package/dist/apollo/CedarApolloProvider.js +35 -0
  7. package/dist/apollo/ErrorBoundary.d.ts +13 -0
  8. package/dist/apollo/ErrorBoundary.d.ts.map +1 -0
  9. package/dist/apollo/ErrorBoundary.js +13 -0
  10. package/dist/apollo/apolloLinkTypes.d.ts +60 -0
  11. package/dist/apollo/apolloLinkTypes.d.ts.map +1 -0
  12. package/dist/apollo/apolloLinkTypes.js +0 -0
  13. package/dist/apollo/index.d.ts +50 -69
  14. package/dist/apollo/index.d.ts.map +1 -1
  15. package/dist/apollo/index.js +2 -163
  16. package/dist/apollo/suspense.d.ts +16 -6
  17. package/dist/apollo/suspense.d.ts.map +1 -1
  18. package/dist/apollo/suspense.js +4 -2
  19. package/dist/cjs/apollo/ApolloProviderWithFetchConfig.d.ts +15 -0
  20. package/dist/cjs/apollo/ApolloProviderWithFetchConfig.d.ts.map +1 -0
  21. package/dist/cjs/apollo/ApolloProviderWithFetchConfig.js +165 -0
  22. package/dist/cjs/apollo/CedarApolloProvider.d.ts +14 -0
  23. package/dist/cjs/apollo/CedarApolloProvider.d.ts.map +1 -0
  24. package/dist/cjs/apollo/CedarApolloProvider.js +69 -0
  25. package/dist/cjs/apollo/ErrorBoundary.d.ts +13 -0
  26. package/dist/cjs/apollo/ErrorBoundary.d.ts.map +1 -0
  27. package/dist/cjs/apollo/ErrorBoundary.js +47 -0
  28. package/dist/cjs/apollo/apolloLinkTypes.d.ts +60 -0
  29. package/dist/cjs/apollo/apolloLinkTypes.d.ts.map +1 -0
  30. package/dist/cjs/apollo/apolloLinkTypes.js +16 -0
  31. package/dist/cjs/apollo/index.d.ts +50 -69
  32. package/dist/cjs/apollo/index.d.ts.map +1 -1
  33. package/dist/cjs/apollo/index.js +2 -167
  34. package/dist/cjs/apollo/suspense.d.ts +16 -6
  35. package/dist/cjs/apollo/suspense.d.ts.map +1 -1
  36. package/dist/cjs/apollo/suspense.js +5 -2
  37. package/dist/cjs/components/FetchConfigProvider.d.ts +3 -2
  38. package/dist/cjs/components/FetchConfigProvider.d.ts.map +1 -1
  39. package/dist/components/FetchConfigProvider.d.ts +3 -2
  40. package/dist/components/FetchConfigProvider.d.ts.map +1 -1
  41. package/package.json +15 -5
@@ -0,0 +1,15 @@
1
+ import React from 'react';
2
+ import type { ApolloCache, setLogVerbosity } from '@apollo/client';
3
+ import type { UseAuth } from '@cedarjs/auth';
4
+ import type { GraphQLClientConfigProp } from './apolloLinkTypes.js';
5
+ interface Props {
6
+ config: Omit<GraphQLClientConfigProp, 'cacheConfig' | 'cache'> & {
7
+ cache: ApolloCache<unknown>;
8
+ };
9
+ useAuth?: UseAuth;
10
+ logLevel: ReturnType<typeof setLogVerbosity>;
11
+ children: React.ReactNode;
12
+ }
13
+ export declare function ApolloProviderWithFetchConfig({ config, children, useAuth, logLevel, }: Props): React.JSX.Element;
14
+ export {};
15
+ //# sourceMappingURL=ApolloProviderWithFetchConfig.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ApolloProviderWithFetchConfig.d.ts","sourceRoot":"","sources":["../../src/apollo/ApolloProviderWithFetchConfig.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAA;AAIzB,OAAO,KAAK,EAAE,WAAW,EAAgB,eAAe,EAAE,MAAM,gBAAgB,CAAA;AAYhF,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAM5C,OAAO,KAAK,EAEV,uBAAuB,EACxB,MAAM,sBAAsB,CAAA;AAoC7B,UAAU,KAAK;IACb,MAAM,EAAE,IAAI,CAAC,uBAAuB,EAAE,aAAa,GAAG,OAAO,CAAC,GAAG;QAC/D,KAAK,EAAE,WAAW,CAAC,OAAO,CAAC,CAAA;KAC5B,CAAA;IACD,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB,QAAQ,EAAE,UAAU,CAAC,OAAO,eAAe,CAAC,CAAA;IAC5C,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAA;CAC1B;AAED,wBAAgB,6BAA6B,CAAC,EAC5C,MAAM,EACN,QAAQ,EACR,OAAmB,EACnB,QAAQ,GACT,EAAE,KAAK,qBAgKP"}
@@ -0,0 +1,134 @@
1
+ import React from "react";
2
+ import {
3
+ ApolloClient,
4
+ setLogVerbosity as apolloSetLogVerbosity
5
+ } from "@apollo/client/core/core.cjs";
6
+ import { setContext } from "@apollo/client/link/context/context.cjs";
7
+ import { ApolloLink, split } from "@apollo/client/link/core/core.cjs";
8
+ import { createPersistedQueryLink } from "@apollo/client/link/persisted-queries/persisted-queries.cjs";
9
+ import { ApolloProvider } from "@apollo/client/react/react.cjs";
10
+ import { getMainDefinition } from "@apollo/client/utilities/utilities.cjs";
11
+ import { print } from "graphql/language/printer.js";
12
+ import { useNoAuth } from "@cedarjs/auth";
13
+ import { createUploadLink } from "../bundled/apollo-upload-client.js";
14
+ import { useFetchConfig } from "../components/FetchConfigProvider.js";
15
+ import { ErrorBoundary } from "./ErrorBoundary.js";
16
+ import * as SSELinkExports from "./sseLink.js";
17
+ const { SSELink, isSubscription, isLiveQuery } = SSELinkExports;
18
+ function ApolloProviderWithFetchConfig({
19
+ config,
20
+ children,
21
+ useAuth = useNoAuth,
22
+ logLevel
23
+ }) {
24
+ apolloSetLogVerbosity(logLevel);
25
+ const { getToken, type: authProviderType } = useAuth();
26
+ const data = {
27
+ mostRecentRequest: void 0,
28
+ mostRecentResponse: void 0
29
+ };
30
+ const updateDataApolloLink = new ApolloLink((operation, forward) => {
31
+ const { operationName, query, variables } = operation;
32
+ data.mostRecentRequest = {};
33
+ data.mostRecentRequest.operationName = operationName;
34
+ data.mostRecentRequest.operationKind = query?.kind.toString();
35
+ data.mostRecentRequest.variables = variables;
36
+ data.mostRecentRequest.query = query && print(operation.query);
37
+ return forward(operation).map((result) => {
38
+ data.mostRecentResponse = result;
39
+ return result;
40
+ });
41
+ });
42
+ const withToken = setContext(async () => {
43
+ const token = await getToken();
44
+ return { token };
45
+ });
46
+ const { headers, uri } = useFetchConfig();
47
+ const authMiddleware = new ApolloLink((operation, forward) => {
48
+ const { token } = operation.getContext();
49
+ const authHeaders = token ? {
50
+ "auth-provider": authProviderType,
51
+ authorization: `Bearer ${token}`
52
+ } : {};
53
+ operation.setContext(() => ({
54
+ headers: {
55
+ ...operation.getContext().headers,
56
+ ...headers,
57
+ // Duped auth headers, because we may remove the `FetchConfigProvider` at a later date.
58
+ ...authHeaders
59
+ }
60
+ }));
61
+ return forward(operation);
62
+ });
63
+ const { httpLinkConfig, link: cedarApolloLink, ...rest } = config ?? {};
64
+ const uploadLink = createUploadLink({
65
+ uri,
66
+ ...httpLinkConfig
67
+ });
68
+ const uploadOrSSELink = typeof SSELink !== "undefined" ? split(
69
+ ({ query }) => {
70
+ const definition = getMainDefinition(query);
71
+ return isSubscription(definition) || isLiveQuery(definition);
72
+ },
73
+ new SSELink({
74
+ url: uri,
75
+ auth: { authProviderType, tokenFn: getToken },
76
+ httpLinkConfig,
77
+ headers
78
+ }),
79
+ uploadLink
80
+ ) : uploadLink;
81
+ const terminatingLink = split(
82
+ ({ query }) => {
83
+ const documentQuery = query;
84
+ return documentQuery?.["__meta__"]?.["hash"] !== void 0;
85
+ },
86
+ createPersistedQueryLink({
87
+ generateHash: (document) => document["__meta__"]["hash"]
88
+ }).concat(uploadOrSSELink),
89
+ uploadOrSSELink
90
+ );
91
+ const cedarApolloLinks = [
92
+ { name: "withToken", link: withToken },
93
+ { name: "authMiddleware", link: authMiddleware },
94
+ { name: "updateDataApolloLink", link: updateDataApolloLink },
95
+ { name: "httpLink", link: terminatingLink }
96
+ ];
97
+ let link = cedarApolloLink;
98
+ link ??= ApolloLink.from(cedarApolloLinks.map((l) => l.link));
99
+ if (typeof link === "function") {
100
+ link = link(cedarApolloLinks);
101
+ }
102
+ const client = new ApolloClient({
103
+ // Default options for every Cell. Better to specify them here than in
104
+ // `beforeQuery` where it's too easy to overwrite them.
105
+ // See https://www.apollographql.com/docs/react/api/core/ApolloClient/#example-defaultoptions-object.
106
+ defaultOptions: {
107
+ watchQuery: {
108
+ // The `fetchPolicy` we expect:
109
+ //
110
+ // > Apollo Client executes the full query against both the cache and
111
+ // > your GraphQL server.
112
+ // > The query automatically updates if the result of the server-side
113
+ // > query modifies cached fields.
114
+ //
115
+ // See https://www.apollographql.com/docs/react/data/queries/#cache-and-network.
116
+ fetchPolicy: "cache-and-network",
117
+ // So that Cells rerender when refetching.
118
+ // See https://www.apollographql.com/docs/react/data/queries/#inspecting-loading-states.
119
+ notifyOnNetworkStatusChange: true
120
+ }
121
+ },
122
+ link,
123
+ ...rest
124
+ });
125
+ const extendErrorAndRethrow = (error, _errorInfo) => {
126
+ error["mostRecentRequest"] = data.mostRecentRequest;
127
+ error["mostRecentResponse"] = data.mostRecentResponse;
128
+ throw error;
129
+ };
130
+ return /* @__PURE__ */ React.createElement(ApolloProvider, { client }, /* @__PURE__ */ React.createElement(ErrorBoundary, { onError: extendErrorAndRethrow }, children));
131
+ }
132
+ export {
133
+ ApolloProviderWithFetchConfig
134
+ };
@@ -0,0 +1,14 @@
1
+ import React from 'react';
2
+ import type { DocumentNode, setLogVerbosity } from '@apollo/client';
3
+ import type { UseAuth } from '@cedarjs/auth';
4
+ import type { GraphQLClientConfigProp } from './apolloLinkTypes.js';
5
+ export type { ApolloClientCacheConfig, CedarApolloLink, CedarApolloLinkFactory, CedarApolloLinkName, CedarApolloLinks, GraphQLClientConfigProp, } from './apolloLinkTypes.js';
6
+ interface Props {
7
+ graphQLClientConfig?: GraphQLClientConfigProp;
8
+ fragments?: DocumentNode[];
9
+ useAuth?: UseAuth;
10
+ logLevel?: ReturnType<typeof setLogVerbosity>;
11
+ children: React.ReactNode;
12
+ }
13
+ export declare function CedarApolloProvider({ graphQLClientConfig, fragments, useAuth, logLevel, children, }: Props): React.JSX.Element;
14
+ //# sourceMappingURL=CedarApolloProvider.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CedarApolloProvider.d.ts","sourceRoot":"","sources":["../../src/apollo/CedarApolloProvider.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAA;AAEzB,OAAO,KAAK,EAAE,YAAY,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAA;AAGnE,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAK5C,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,sBAAsB,CAAA;AAInE,YAAY,EACV,uBAAuB,EACvB,eAAe,EACf,sBAAsB,EACtB,mBAAmB,EACnB,gBAAgB,EAChB,uBAAuB,GACxB,MAAM,sBAAsB,CAAA;AAE7B,UAAU,KAAK;IACb,mBAAmB,CAAC,EAAE,uBAAuB,CAAA;IAC7C,SAAS,CAAC,EAAE,YAAY,EAAE,CAAA;IAC1B,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB,QAAQ,CAAC,EAAE,UAAU,CAAC,OAAO,eAAe,CAAC,CAAA;IAC7C,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAA;CAC1B;AAED,wBAAgB,mBAAmB,CAAC,EAClC,mBAAmB,EACnB,SAAS,EACT,OAAmB,EACnB,QAAkB,EAClB,QAAQ,GACT,EAAE,KAAK,qBA4BP"}
@@ -0,0 +1,35 @@
1
+ import React from "react";
2
+ import { InMemoryCache } from "@apollo/client/cache/cache.cjs";
3
+ import { useNoAuth } from "@cedarjs/auth";
4
+ import { FetchConfigProvider } from "../components/FetchConfigProvider.js";
5
+ import { ApolloProviderWithFetchConfig } from "./ApolloProviderWithFetchConfig.js";
6
+ import { fragmentRegistry } from "./fragmentRegistry.js";
7
+ function CedarApolloProvider({
8
+ graphQLClientConfig,
9
+ fragments,
10
+ useAuth = useNoAuth,
11
+ logLevel = "debug",
12
+ children
13
+ }) {
14
+ const { cacheConfig, ...config } = graphQLClientConfig ?? {};
15
+ if (fragments) {
16
+ fragmentRegistry.register(...fragments);
17
+ }
18
+ const cache = new InMemoryCache({
19
+ fragments: fragmentRegistry,
20
+ possibleTypes: cacheConfig?.possibleTypes,
21
+ ...cacheConfig
22
+ }).restore(globalThis?.__CEDAR__APOLLO_STATE ?? {});
23
+ return /* @__PURE__ */ React.createElement(FetchConfigProvider, { useAuth }, /* @__PURE__ */ React.createElement(
24
+ ApolloProviderWithFetchConfig,
25
+ {
26
+ config: { cache, ...config },
27
+ useAuth,
28
+ logLevel
29
+ },
30
+ children
31
+ ));
32
+ }
33
+ export {
34
+ CedarApolloProvider
35
+ };
@@ -0,0 +1,13 @@
1
+ import React from 'react';
2
+ type ComponentDidCatch = React.ComponentLifecycle<any, any>['componentDidCatch'];
3
+ interface ErrorBoundaryProps {
4
+ error?: unknown;
5
+ onError: NonNullable<ComponentDidCatch>;
6
+ children: React.ReactNode;
7
+ }
8
+ export declare class ErrorBoundary extends React.Component<ErrorBoundaryProps> {
9
+ componentDidCatch(...args: Parameters<NonNullable<ComponentDidCatch>>): void;
10
+ render(): React.ReactNode;
11
+ }
12
+ export {};
13
+ //# sourceMappingURL=ErrorBoundary.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ErrorBoundary.d.ts","sourceRoot":"","sources":["../../src/apollo/ErrorBoundary.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAA;AAEzB,KAAK,iBAAiB,GAAG,KAAK,CAAC,kBAAkB,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,mBAAmB,CAAC,CAAA;AAEhF,UAAU,kBAAkB;IAC1B,KAAK,CAAC,EAAE,OAAO,CAAA;IACf,OAAO,EAAE,WAAW,CAAC,iBAAiB,CAAC,CAAA;IACvC,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAA;CAC1B;AAED,qBAAa,aAAc,SAAQ,KAAK,CAAC,SAAS,CAAC,kBAAkB,CAAC;IACpE,iBAAiB,CAAC,GAAG,IAAI,EAAE,UAAU,CAAC,WAAW,CAAC,iBAAiB,CAAC,CAAC;IAKrE,MAAM;CAGP"}
@@ -0,0 +1,13 @@
1
+ import React from "react";
2
+ class ErrorBoundary extends React.Component {
3
+ componentDidCatch(...args) {
4
+ this.setState({});
5
+ this.props.onError(...args);
6
+ }
7
+ render() {
8
+ return this.props.children;
9
+ }
10
+ }
11
+ export {
12
+ ErrorBoundary
13
+ };
@@ -0,0 +1,60 @@
1
+ import type { ApolloCache, ApolloClientOptions, ApolloLink, HttpLink, HttpOptions, InMemoryCacheConfig } from '@apollo/client';
2
+ export type ApolloClientCacheConfig = InMemoryCacheConfig;
3
+ export type CedarApolloLinkName = 'withToken' | 'authMiddleware' | 'updateDataApolloLink' | 'httpLink';
4
+ export type CedarApolloLink<Name extends CedarApolloLinkName, Link extends ApolloLink = ApolloLink> = {
5
+ name: Name;
6
+ link: Link;
7
+ };
8
+ export type CedarApolloLinks = [
9
+ CedarApolloLink<'withToken'>,
10
+ CedarApolloLink<'authMiddleware'>,
11
+ CedarApolloLink<'updateDataApolloLink'>,
12
+ CedarApolloLink<'httpLink', ApolloLink | HttpLink>
13
+ ];
14
+ export type CedarApolloLinkFactory = (links: CedarApolloLinks) => ApolloLink;
15
+ export type GraphQLClientConfigProp = Omit<ApolloClientOptions<unknown>, 'cache' | 'link'> & {
16
+ cache?: ApolloCache<unknown>;
17
+ /**
18
+ * Configuration for Apollo Client's `InMemoryCache`.
19
+ * See https://www.apollographql.com/docs/react/caching/cache-configuration/.
20
+ */
21
+ cacheConfig?: ApolloClientCacheConfig;
22
+ /**
23
+ * Configuration for the terminating `HttpLink`.
24
+ * See https://www.apollographql.com/docs/react/api/link/apollo-link-http/#httplink-constructor-options.
25
+ *
26
+ * For example, you can use this prop to set the credentials policy so that cookies can be sent to other domains:
27
+ *
28
+ * ```js
29
+ * <CedarApolloProvider graphQLClientConfig={{
30
+ * httpLinkConfig: { credentials: 'include' }
31
+ * }}>
32
+ * ```
33
+ */
34
+ httpLinkConfig?: HttpOptions;
35
+ /**
36
+ * Extend or overwrite `CedarApolloProvider`'s Apollo Link.
37
+ *
38
+ * To overwrite Cedar's Apollo Link, just provide your own `ApolloLink`.
39
+ *
40
+ * To extend Cedar's Apollo Link, provide a function—it'll get passed an array of Cedar's Apollo Links
41
+ * which are objects with a name and link property:
42
+ *
43
+ * ```js
44
+ * const link = (cedarApolloLinks) => {
45
+ * const consoleLink = new ApolloLink((operation, forward) => {
46
+ * console.log(operation.operationName)
47
+ * return forward(operation)
48
+ * })
49
+ *
50
+ * return ApolloLink.from([consoleLink, ...cedarApolloLinks.map(({ link }) => link)])
51
+ * }
52
+ * ```
53
+ *
54
+ * If you do this, there's a few things you should keep in mind:
55
+ * - your function should return a single link (e.g., using `ApolloLink.from`; see https://www.apollographql.com/docs/react/api/link/introduction/#additive-composition)
56
+ * - the `HttpLink` should come last (https://www.apollographql.com/docs/react/api/link/introduction/#the-terminating-link)
57
+ */
58
+ link?: ApolloLink | CedarApolloLinkFactory;
59
+ };
60
+ //# sourceMappingURL=apolloLinkTypes.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"apolloLinkTypes.d.ts","sourceRoot":"","sources":["../../src/apollo/apolloLinkTypes.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,WAAW,EACX,mBAAmB,EACnB,UAAU,EACV,QAAQ,EACR,WAAW,EACX,mBAAmB,EACpB,MAAM,gBAAgB,CAAA;AAEvB,MAAM,MAAM,uBAAuB,GAAG,mBAAmB,CAAA;AAEzD,MAAM,MAAM,mBAAmB,GAC3B,WAAW,GACX,gBAAgB,GAChB,sBAAsB,GACtB,UAAU,CAAA;AAEd,MAAM,MAAM,eAAe,CACzB,IAAI,SAAS,mBAAmB,EAChC,IAAI,SAAS,UAAU,GAAG,UAAU,IAClC;IACF,IAAI,EAAE,IAAI,CAAA;IACV,IAAI,EAAE,IAAI,CAAA;CACX,CAAA;AAED,MAAM,MAAM,gBAAgB,GAAG;IAC7B,eAAe,CAAC,WAAW,CAAC;IAC5B,eAAe,CAAC,gBAAgB,CAAC;IACjC,eAAe,CAAC,sBAAsB,CAAC;IACvC,eAAe,CAAC,UAAU,EAAE,UAAU,GAAG,QAAQ,CAAC;CACnD,CAAA;AAED,MAAM,MAAM,sBAAsB,GAAG,CAAC,KAAK,EAAE,gBAAgB,KAAK,UAAU,CAAA;AAE5E,MAAM,MAAM,uBAAuB,GAAG,IAAI,CACxC,mBAAmB,CAAC,OAAO,CAAC,EAC5B,OAAO,GAAG,MAAM,CACjB,GAAG;IACF,KAAK,CAAC,EAAE,WAAW,CAAC,OAAO,CAAC,CAAA;IAC5B;;;OAGG;IACH,WAAW,CAAC,EAAE,uBAAuB,CAAA;IACrC;;;;;;;;;;;OAWG;IACH,cAAc,CAAC,EAAE,WAAW,CAAA;IAC5B;;;;;;;;;;;;;;;;;;;;;;OAsBG;IACH,IAAI,CAAC,EAAE,UAAU,GAAG,sBAAsB,CAAA;CAC3C,CAAA"}
File without changes
@@ -1,75 +1,56 @@
1
- import React from 'react';
2
- import type { ApolloClientOptions, setLogVerbosity, ApolloCache, InMemoryCacheConfig, HttpOptions, DocumentNode, HttpLink } from '@apollo/client';
3
- import { ApolloLink } from '@apollo/client/link/core/core.cjs';
4
- import type { UseAuth } from '@cedarjs/auth';
1
+ import type { ApolloLink } from '@apollo/client';
2
+ import { CedarApolloProvider } from './CedarApolloProvider.js';
3
+ import type { ApolloClientCacheConfig as CedarApolloClientCacheConfig, CedarApolloLink, CedarApolloLinkFactory, CedarApolloLinkName, CedarApolloLinks, GraphQLClientConfigProp as CedarGraphQLClientConfigProp } from './CedarApolloProvider.js';
5
4
  import { fragmentRegistry, registerFragment, registerFragments } from './fragmentRegistry.js';
6
5
  import { useCache } from './useCache.js';
7
6
  export type { CacheKey, FragmentIdentifier, RegisterFragmentResult, } from './fragmentRegistry.js';
8
7
  export { useCache };
9
8
  export { fragmentRegistry, registerFragment, registerFragments };
10
- export type ApolloClientCacheConfig = InMemoryCacheConfig;
11
- export type RedwoodApolloLinkName = 'withToken' | 'authMiddleware' | 'updateDataApolloLink' | 'httpLink';
12
- export type RedwoodApolloLink<Name extends RedwoodApolloLinkName, Link extends ApolloLink = ApolloLink> = {
13
- name: Name;
14
- link: Link;
15
- };
16
- export type RedwoodApolloLinks = [
17
- RedwoodApolloLink<'withToken'>,
18
- RedwoodApolloLink<'authMiddleware'>,
19
- RedwoodApolloLink<'updateDataApolloLink'>,
20
- RedwoodApolloLink<'httpLink', ApolloLink | HttpLink>
21
- ];
22
- export type RedwoodApolloLinkFactory = (links: RedwoodApolloLinks) => ApolloLink;
23
- export type GraphQLClientConfigProp = Omit<ApolloClientOptions<unknown>, 'cache' | 'link'> & {
24
- cache?: ApolloCache<unknown>;
25
- /**
26
- * Configuration for Apollo Client's `InMemoryCache`.
27
- * See https://www.apollographql.com/docs/react/caching/cache-configuration/.
28
- */
29
- cacheConfig?: ApolloClientCacheConfig;
30
- /**
31
- * Configuration for the terminating `HttpLink`.
32
- * See https://www.apollographql.com/docs/react/api/link/apollo-link-http/#httplink-constructor-options.
33
- *
34
- * For example, you can use this prop to set the credentials policy so that cookies can be sent to other domains:
35
- *
36
- * ```js
37
- * <RedwoodApolloProvider graphQLClientConfig={{
38
- * httpLinkConfig: { credentials: 'include' }
39
- * }}>
40
- * ```
41
- */
42
- httpLinkConfig?: HttpOptions;
43
- /**
44
- * Extend or overwrite `RedwoodApolloProvider`'s Apollo Link.
45
- *
46
- * To overwrite Redwood's Apollo Link, just provide your own `ApolloLink`.
47
- *
48
- * To extend Redwood's Apollo Link, provide a function—it'll get passed an array of Redwood's Apollo Links
49
- * which are objects with a name and link property:
50
- *
51
- * ```js
52
- * const link = (redwoodApolloLinks) => {
53
- * const consoleLink = new ApolloLink((operation, forward) => {
54
- * console.log(operation.operationName)
55
- * return forward(operation)
56
- * })
57
- *
58
- * return ApolloLink.from([consoleLink, ...redwoodApolloLinks.map(({ link }) => link)])
59
- * }
60
- * ```
61
- *
62
- * If you do this, there's a few things you should keep in mind:
63
- * - your function should return a single link (e.g., using `ApolloLink.from`; see https://www.apollographql.com/docs/react/api/link/introduction/#additive-composition)
64
- * - the `HttpLink` should come last (https://www.apollographql.com/docs/react/api/link/introduction/#the-terminating-link)
65
- */
66
- link?: ApolloLink | RedwoodApolloLinkFactory;
67
- };
68
- export declare const RedwoodApolloProvider: React.FunctionComponent<{
69
- graphQLClientConfig?: GraphQLClientConfigProp;
70
- fragments?: DocumentNode[];
71
- useAuth?: UseAuth;
72
- logLevel?: ReturnType<typeof setLogVerbosity>;
73
- children: React.ReactNode;
74
- }>;
9
+ /**
10
+ * @deprecated Import from `@cedarjs/web/apollo/CedarApolloProvider` instead.
11
+ * This re-export will be removed in a future release.
12
+ *
13
+ * ```js
14
+ * import type { ApolloClientCacheConfig } from '@cedarjs/web/apollo/CedarApolloProvider'
15
+ * ```
16
+ */
17
+ export type ApolloClientCacheConfig = CedarApolloClientCacheConfig;
18
+ /**
19
+ * @deprecated Import from `@cedarjs/web/apollo/CedarApolloProvider` instead.
20
+ * This re-export will be removed in a future release.
21
+ *
22
+ * ```js
23
+ * import type { GraphQLClientConfigProp } from '@cedarjs/web/apollo/CedarApolloProvider'
24
+ * ```
25
+ */
26
+ export type GraphQLClientConfigProp = CedarGraphQLClientConfigProp;
27
+ /**
28
+ * @deprecated Use `CedarApolloLinkName` instead. `RedwoodApolloLinkName` will
29
+ * be removed in a future release.
30
+ */
31
+ export type RedwoodApolloLinkName = CedarApolloLinkName;
32
+ /**
33
+ * @deprecated Use `CedarApolloLink` instead. `RedwoodApolloLink` will be
34
+ * removed in a future release.
35
+ */
36
+ export type RedwoodApolloLink<Name extends CedarApolloLinkName, Link extends ApolloLink = ApolloLink> = CedarApolloLink<Name, Link>;
37
+ /**
38
+ * @deprecated Use `CedarApolloLinks` instead. `RedwoodApolloLinks` will be
39
+ * removed in a future release.
40
+ */
41
+ export type RedwoodApolloLinks = CedarApolloLinks;
42
+ /**
43
+ * @deprecated Use `CedarApolloLinkFactory` instead. `RedwoodApolloLinkFactory`
44
+ * will be removed in a future release.
45
+ */
46
+ export type RedwoodApolloLinkFactory = CedarApolloLinkFactory;
47
+ /**
48
+ * @deprecated Use `CedarApolloProvider` instead. `RedwoodApolloProvider` will
49
+ * be removed in a future release.
50
+ *
51
+ * ```js
52
+ * import { CedarApolloProvider } from '@cedarjs/web/apollo/CedarApolloProvider'
53
+ * ```
54
+ */
55
+ export declare const RedwoodApolloProvider: typeof CedarApolloProvider;
75
56
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/apollo/index.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAA;AAIzB,OAAO,KAAK,EACV,mBAAmB,EACnB,eAAe,EACf,WAAW,EACX,mBAAmB,EACnB,WAAW,EACX,YAAY,EACZ,QAAQ,EACT,MAAM,gBAAgB,CAAA;AAOvB,OAAO,EAAE,UAAU,EAAS,MAAM,mCAAmC,CAAA;AAMrE,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAS5C,OAAO,EACL,gBAAgB,EAChB,gBAAgB,EAChB,iBAAiB,EAClB,MAAM,uBAAuB,CAAA;AAE9B,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAA;AAKxC,YAAY,EACV,QAAQ,EACR,kBAAkB,EAClB,sBAAsB,GACvB,MAAM,uBAAuB,CAAA;AAE9B,OAAO,EAAE,QAAQ,EAAE,CAAA;AAEnB,OAAO,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,CAAA;AAEhE,MAAM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React from "react";
2
- import { InMemoryCache } from "@apollo/client/cache/cache.cjs";
3
- import {
4
- ApolloClient,
5
- setLogVerbosity as apolloSetLogVerbosity
6
- } from "@apollo/client/core/core.cjs";
7
- import { setContext } from "@apollo/client/link/context/context.cjs";
8
- import { ApolloLink, split } from "@apollo/client/link/core/core.cjs";
9
- import { createPersistedQueryLink } from "@apollo/client/link/persisted-queries/persisted-queries.cjs";
10
- import { ApolloProvider } from "@apollo/client/react/react.cjs";
11
- import { getMainDefinition } from "@apollo/client/utilities/utilities.cjs";
12
- import { print } from "graphql/language/printer.js";
13
- import { useNoAuth } from "@cedarjs/auth";
14
- import { createUploadLink } from "../bundled/apollo-upload-client.js";
15
- import {
16
- FetchConfigProvider,
17
- useFetchConfig
18
- } from "../components/FetchConfigProvider.js";
1
+ import { CedarApolloProvider } from "./CedarApolloProvider.js";
19
2
  import {
20
3
  fragmentRegistry,
21
4
  registerFragment,
22
5
  registerFragments
23
6
  } from "./fragmentRegistry.js";
24
- import * as SSELinkExports from "./sseLink.js";
25
7
  import { useCache } from "./useCache.js";
26
- const { SSELink, isSubscription, isLiveQuery } = SSELinkExports;
27
- const ApolloProviderWithFetchConfig = ({ config, children, useAuth = useNoAuth, logLevel }) => {
28
- apolloSetLogVerbosity(logLevel);
29
- const { getToken, type: authProviderType } = useAuth();
30
- const data = {
31
- mostRecentRequest: void 0,
32
- mostRecentResponse: void 0
33
- };
34
- const updateDataApolloLink = new ApolloLink((operation, forward) => {
35
- const { operationName, query, variables } = operation;
36
- data.mostRecentRequest = {};
37
- data.mostRecentRequest.operationName = operationName;
38
- data.mostRecentRequest.operationKind = query?.kind.toString();
39
- data.mostRecentRequest.variables = variables;
40
- data.mostRecentRequest.query = query && print(operation.query);
41
- return forward(operation).map((result) => {
42
- data.mostRecentResponse = result;
43
- return result;
44
- });
45
- });
46
- const withToken = setContext(async () => {
47
- const token = await getToken();
48
- return { token };
49
- });
50
- const { headers, uri } = useFetchConfig();
51
- const authMiddleware = new ApolloLink((operation, forward) => {
52
- const { token } = operation.getContext();
53
- const authHeaders = token ? {
54
- "auth-provider": authProviderType,
55
- authorization: `Bearer ${token}`
56
- } : {};
57
- operation.setContext(() => ({
58
- headers: {
59
- ...operation.getContext().headers,
60
- ...headers,
61
- // Duped auth headers, because we may remove the `FetchConfigProvider` at a later date.
62
- ...authHeaders
63
- }
64
- }));
65
- return forward(operation);
66
- });
67
- const { httpLinkConfig, link: redwoodApolloLink, ...rest } = config ?? {};
68
- const uploadLink = createUploadLink({
69
- uri,
70
- ...httpLinkConfig
71
- // The upload link types don't match the ApolloLink types, even though it comes from Apollo
72
- // because they use ESM imports and we're using the default ones.
73
- });
74
- const uploadOrSSELink = typeof SSELink !== "undefined" ? split(
75
- ({ query }) => {
76
- const definition = getMainDefinition(query);
77
- return isSubscription(definition) || isLiveQuery(definition);
78
- },
79
- new SSELink({
80
- url: uri,
81
- auth: { authProviderType, tokenFn: getToken },
82
- httpLinkConfig,
83
- headers
84
- }),
85
- uploadLink
86
- ) : uploadLink;
87
- const terminatingLink = split(
88
- ({ query }) => {
89
- const documentQuery = query;
90
- return documentQuery?.["__meta__"]?.["hash"] !== void 0;
91
- },
92
- createPersistedQueryLink({
93
- generateHash: (document) => document["__meta__"]["hash"]
94
- }).concat(uploadOrSSELink),
95
- uploadOrSSELink
96
- );
97
- const redwoodApolloLinks = [
98
- { name: "withToken", link: withToken },
99
- { name: "authMiddleware", link: authMiddleware },
100
- { name: "updateDataApolloLink", link: updateDataApolloLink },
101
- { name: "httpLink", link: terminatingLink }
102
- ];
103
- let link = redwoodApolloLink;
104
- link ??= ApolloLink.from(redwoodApolloLinks.map((l) => l.link));
105
- if (typeof link === "function") {
106
- link = link(redwoodApolloLinks);
107
- }
108
- const client = new ApolloClient({
109
- // Default options for every Cell. Better to specify them here than in `beforeQuery` where it's too easy to overwrite them.
110
- // See https://www.apollographql.com/docs/react/api/core/ApolloClient/#example-defaultoptions-object.
111
- defaultOptions: {
112
- watchQuery: {
113
- // The `fetchPolicy` we expect:
114
- //
115
- // > Apollo Client executes the full query against both the cache and your GraphQL server.
116
- // > The query automatically updates if the result of the server-side query modifies cached fields.
117
- //
118
- // See https://www.apollographql.com/docs/react/data/queries/#cache-and-network.
119
- fetchPolicy: "cache-and-network",
120
- // So that Cells rerender when refetching.
121
- // See https://www.apollographql.com/docs/react/data/queries/#inspecting-loading-states.
122
- notifyOnNetworkStatusChange: true
123
- }
124
- },
125
- link,
126
- ...rest
127
- });
128
- const extendErrorAndRethrow = (error, _errorInfo) => {
129
- error["mostRecentRequest"] = data.mostRecentRequest;
130
- error["mostRecentResponse"] = data.mostRecentResponse;
131
- throw error;
132
- };
133
- return /* @__PURE__ */ React.createElement(ApolloProvider, { client }, /* @__PURE__ */ React.createElement(ErrorBoundary, { onError: extendErrorAndRethrow }, children));
134
- };
135
- class ErrorBoundary extends React.Component {
136
- componentDidCatch(...args) {
137
- this.setState({});
138
- this.props.onError(...args);
139
- }
140
- render() {
141
- return this.props.children;
142
- }
143
- }
144
- const RedwoodApolloProvider = ({
145
- graphQLClientConfig,
146
- fragments,
147
- useAuth = useNoAuth,
148
- logLevel = "debug",
149
- children
150
- }) => {
151
- const { cacheConfig, ...config } = graphQLClientConfig ?? {};
152
- if (fragments) {
153
- fragmentRegistry.register(...fragments);
154
- }
155
- const cache = new InMemoryCache({
156
- fragments: fragmentRegistry,
157
- possibleTypes: cacheConfig?.possibleTypes,
158
- ...cacheConfig
159
- }).restore(globalThis?.__REDWOOD__APOLLO_STATE ?? {});
160
- return /* @__PURE__ */ React.createElement(FetchConfigProvider, { useAuth }, /* @__PURE__ */ React.createElement(
161
- ApolloProviderWithFetchConfig,
162
- {
163
- config: { cache, ...config },
164
- useAuth,
165
- logLevel
166
- },
167
- children
168
- ));
169
- };
8
+ const RedwoodApolloProvider = CedarApolloProvider;
170
9
  export {
171
10
  RedwoodApolloProvider,
172
11
  fragmentRegistry,