@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.
- package/dist/apollo/ApolloProviderWithFetchConfig.d.ts +15 -0
- package/dist/apollo/ApolloProviderWithFetchConfig.d.ts.map +1 -0
- package/dist/apollo/ApolloProviderWithFetchConfig.js +134 -0
- package/dist/apollo/CedarApolloProvider.d.ts +14 -0
- package/dist/apollo/CedarApolloProvider.d.ts.map +1 -0
- package/dist/apollo/CedarApolloProvider.js +35 -0
- package/dist/apollo/ErrorBoundary.d.ts +13 -0
- package/dist/apollo/ErrorBoundary.d.ts.map +1 -0
- package/dist/apollo/ErrorBoundary.js +13 -0
- package/dist/apollo/apolloLinkTypes.d.ts +60 -0
- package/dist/apollo/apolloLinkTypes.d.ts.map +1 -0
- package/dist/apollo/apolloLinkTypes.js +0 -0
- package/dist/apollo/index.d.ts +50 -69
- package/dist/apollo/index.d.ts.map +1 -1
- package/dist/apollo/index.js +2 -163
- package/dist/apollo/suspense.d.ts +16 -6
- package/dist/apollo/suspense.d.ts.map +1 -1
- package/dist/apollo/suspense.js +4 -2
- package/dist/cjs/apollo/ApolloProviderWithFetchConfig.d.ts +15 -0
- package/dist/cjs/apollo/ApolloProviderWithFetchConfig.d.ts.map +1 -0
- package/dist/cjs/apollo/ApolloProviderWithFetchConfig.js +165 -0
- package/dist/cjs/apollo/CedarApolloProvider.d.ts +14 -0
- package/dist/cjs/apollo/CedarApolloProvider.d.ts.map +1 -0
- package/dist/cjs/apollo/CedarApolloProvider.js +69 -0
- package/dist/cjs/apollo/ErrorBoundary.d.ts +13 -0
- package/dist/cjs/apollo/ErrorBoundary.d.ts.map +1 -0
- package/dist/cjs/apollo/ErrorBoundary.js +47 -0
- package/dist/cjs/apollo/apolloLinkTypes.d.ts +60 -0
- package/dist/cjs/apollo/apolloLinkTypes.d.ts.map +1 -0
- package/dist/cjs/apollo/apolloLinkTypes.js +16 -0
- package/dist/cjs/apollo/index.d.ts +50 -69
- package/dist/cjs/apollo/index.d.ts.map +1 -1
- package/dist/cjs/apollo/index.js +2 -167
- package/dist/cjs/apollo/suspense.d.ts +16 -6
- package/dist/cjs/apollo/suspense.d.ts.map +1 -1
- package/dist/cjs/apollo/suspense.js +5 -2
- package/dist/cjs/components/FetchConfigProvider.d.ts +3 -2
- package/dist/cjs/components/FetchConfigProvider.d.ts.map +1 -1
- package/dist/components/FetchConfigProvider.d.ts +3 -2
- package/dist/components/FetchConfigProvider.d.ts.map +1 -1
- 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,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
|
package/dist/apollo/index.d.ts
CHANGED
|
@@ -1,75 +1,56 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
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
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
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.
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/apollo/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAA;AAEhD,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAA;AAC9D,OAAO,KAAK,EACV,uBAAuB,IAAI,4BAA4B,EACvD,eAAe,EACf,sBAAsB,EACtB,mBAAmB,EACnB,gBAAgB,EAChB,uBAAuB,IAAI,4BAA4B,EACxD,MAAM,0BAA0B,CAAA;AACjC,OAAO,EACL,gBAAgB,EAChB,gBAAgB,EAChB,iBAAiB,EAClB,MAAM,uBAAuB,CAAA;AAC9B,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAA;AAExC,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;;;;;;;GAOG;AACH,MAAM,MAAM,uBAAuB,GAAG,4BAA4B,CAAA;AAElE;;;;;;;GAOG;AACH,MAAM,MAAM,uBAAuB,GAAG,4BAA4B,CAAA;AAElE;;;GAGG;AACH,MAAM,MAAM,qBAAqB,GAAG,mBAAmB,CAAA;AAEvD;;;GAGG;AACH,MAAM,MAAM,iBAAiB,CAC3B,IAAI,SAAS,mBAAmB,EAChC,IAAI,SAAS,UAAU,GAAG,UAAU,IAClC,eAAe,CAAC,IAAI,EAAE,IAAI,CAAC,CAAA;AAE/B;;;GAGG;AACH,MAAM,MAAM,kBAAkB,GAAG,gBAAgB,CAAA;AAEjD;;;GAGG;AACH,MAAM,MAAM,wBAAwB,GAAG,sBAAsB,CAAA;AAE7D;;;;;;;GAOG;AACH,eAAO,MAAM,qBAAqB,4BAAsB,CAAA"}
|
package/dist/apollo/index.js
CHANGED
|
@@ -1,172 +1,11 @@
|
|
|
1
|
-
import
|
|
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
|
|
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,
|