@gambalabs/apollo 6.0.0-alpha.17 → 6.0.0-alpha.20

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.
@@ -1,44 +1,86 @@
1
1
  import { hash } from "ohash";
2
2
  import { print } from "graphql";
3
- import { ref, useCookie, useNuxtApp, useAsyncData } from "#imports";
4
- import NuxtApollo from "#build/apollo";
3
+ import { ref, unref, isRef, reactive, useCookie, useNuxtApp, useAsyncData } from "#imports";
4
+ import { NuxtApollo } from "#apollo";
5
5
  export function useAsyncQuery(...args) {
6
- const { key, fn } = prep(...args);
7
- return useAsyncData(key, fn);
6
+ const { key, fn, options } = prep(...args);
7
+ return useAsyncData(key, fn, options);
8
8
  }
9
9
  export function useLazyAsyncQuery(...args) {
10
- const { key, fn } = prep(...args);
11
- return useAsyncData(key, fn, { lazy: true });
10
+ const { key, fn, options } = prep(...args);
11
+ return useAsyncData(key, fn, { ...options, lazy: true });
12
12
  }
13
13
  const prep = (...args) => {
14
14
  const { clients } = useApollo();
15
- const query = args?.[0]?.query || args?.[0];
16
- const cache = args?.[0]?.cache ?? true;
17
- const variables = args?.[0]?.variables || typeof args?.[1] !== "string" && args?.[1] || void 0;
18
- let clientId = args?.[0]?.clientId || typeof args?.[1] === "string" && args?.[1] || void 0;
15
+ let query;
16
+ let variables;
17
+ let cache;
18
+ let clientId;
19
+ let context;
20
+ let options = {};
21
+ if (typeof args?.[0] === "object" && "query" in args[0]) {
22
+ query = args?.[0]?.query;
23
+ variables = args?.[0]?.variables;
24
+ cache = args?.[0]?.cache;
25
+ context = args?.[0]?.context;
26
+ clientId = args?.[0]?.clientId;
27
+ if (typeof args?.[1] === "object") {
28
+ options = args?.[1];
29
+ }
30
+ } else {
31
+ query = args?.[0];
32
+ variables = args?.[1];
33
+ clientId = args?.[2];
34
+ context = args?.[3];
35
+ if (typeof args?.[4] === "object") {
36
+ options = args?.[4];
37
+ }
38
+ }
39
+ if (!query) {
40
+ throw new Error("@nuxtjs/apollo: no query provided");
41
+ }
19
42
  if (!clientId || !clients?.[clientId]) {
20
- clientId = clients?.default ? "default" : Object.keys(clients)[0];
43
+ clientId = clients?.default ? "default" : Object.keys(clients)?.[0];
44
+ if (!clientId) {
45
+ throw new Error("@nuxtjs/apollo: no client found");
46
+ }
47
+ }
48
+ if (variables) {
49
+ variables = isRef(variables) ? variables : reactive(variables);
50
+ options.watch = options.watch || [];
51
+ options.watch.push(variables);
21
52
  }
22
- const key = args?.[0]?.key || hash({ query: print(query), variables, clientId });
23
- const fn = () => clients[clientId]?.query({ query, variables, fetchPolicy: "no-cache" }).then((r) => r.data);
24
- return { key, query, clientId, variables, fn };
53
+ const key = args?.[0]?.key || hash({ query: print(query), variables: unref(variables), clientId });
54
+ const fn = () => clients[clientId]?.query({
55
+ query,
56
+ variables: unref(variables) || void 0,
57
+ ...cache && { fetchPolicy: "cache-first" },
58
+ context
59
+ }).then((r) => r.data);
60
+ return { key, query, clientId, variables, fn, options };
25
61
  };
26
- export const useApollo = () => {
62
+ export function useApollo() {
27
63
  const nuxtApp = useNuxtApp();
28
64
  const getToken = async (client) => {
29
65
  client = client || "default";
30
66
  const conf = NuxtApollo?.clients?.[client];
67
+ if (!conf) {
68
+ return;
69
+ }
31
70
  const token = ref(null);
32
71
  await nuxtApp.callHook("apollo:auth", { token, client });
33
72
  if (token.value) {
34
73
  return token.value;
35
74
  }
36
75
  const tokenName = conf.tokenName;
37
- return conf?.tokenStorage === "cookie" ? useCookie(tokenName).value : process.client && localStorage.getItem(tokenName) || null;
76
+ return conf?.tokenStorage === "cookie" ? nuxtApp.runWithContext(() => useCookie(tokenName).value) : process.client && localStorage.getItem(tokenName) || null;
38
77
  };
39
78
  const updateAuth = async ({ token, client, mode, skipResetStore }) => {
40
79
  client = client || "default";
41
80
  const conf = NuxtApollo?.clients?.[client];
81
+ if (!conf) {
82
+ return;
83
+ }
42
84
  const tokenName = client && conf.tokenName;
43
85
  if (conf?.tokenStorage === "cookie") {
44
86
  const cookieOpts = client && conf?.cookieAttributes || NuxtApollo?.cookieAttributes;
@@ -63,30 +105,9 @@ export const useApollo = () => {
63
105
  await nuxtApp?._apolloClients?.[client].resetStore().catch((e) => console.log("%cError on cache reset", "color: orange;", e.message));
64
106
  };
65
107
  return {
66
- /**
67
- * Retrieve the auth token for the specified client. Adheres to the `apollo:auth` hook.
68
- *
69
- * @param {string} client The client who's token to retrieve. Defaults to `default`.
70
- */
71
108
  getToken,
72
- /**
73
- * Access the configured apollo clients.
74
- */
75
109
  clients: nuxtApp?._apolloClients,
76
- /**
77
- * Apply auth token to the specified Apollo client, and optionally reset it's cache.
78
- *
79
- * @param {string} token The token to be applied.
80
- * @param {string} client - Name of the Apollo client. Defaults to `default`.
81
- * @param {boolean} skipResetStore - If `true`, the cache will not be reset.
82
- * */
83
110
  onLogin: (token, client, skipResetStore) => updateAuth({ token, client, skipResetStore, mode: "login" }),
84
- /**
85
- * Remove the auth token from the Apollo client, and optionally reset it's cache.
86
- *
87
- * @param {string} client - Name of the Apollo client. Defaults to `default`.
88
- * @param {boolean} skipResetStore - If `true`, the cache will not be reset.
89
- * */
90
111
  onLogout: (client, skipResetStore) => updateAuth({ client, skipResetStore, mode: "logout" })
91
112
  };
92
- };
113
+ }
@@ -1,2 +1,34 @@
1
+ import { ApolloClient } from '@apollo/client/core';
2
+ import type { ErrorResponse } from '../types';
3
+ import { useApollo } from './composables';
4
+ import type { Ref } from '#imports';
5
+ import type { ApolloClientKeys } from '#apollo';
1
6
  declare const _default: any;
2
7
  export default _default;
8
+ export interface ModuleRuntimeHooks {
9
+ 'apollo:auth': (params: {
10
+ client: ApolloClientKeys;
11
+ token: Ref<string | null>;
12
+ }) => void;
13
+ 'apollo:error': (error: ErrorResponse) => void;
14
+ }
15
+ interface DollarApolloHelpers extends ReturnType<typeof useApollo> {
16
+ }
17
+ interface DollarApollo {
18
+ clients: Record<ApolloClientKeys, ApolloClient<any>>;
19
+ defaultClient: ApolloClient<any>;
20
+ }
21
+ declare module '#app' {
22
+ interface RuntimeNuxtHooks extends ModuleRuntimeHooks {
23
+ }
24
+ interface NuxtApp {
25
+ $apolloHelpers: DollarApolloHelpers;
26
+ $apollo: DollarApollo;
27
+ }
28
+ }
29
+ declare module 'vue' {
30
+ interface ComponentCustomProperties {
31
+ $apolloHelpers: DollarApolloHelpers;
32
+ $apollo: DollarApollo;
33
+ }
34
+ }
@@ -1,6 +1,7 @@
1
- import destr from "destr";
1
+ import { destr } from "destr";
2
2
  import { onError } from "@apollo/client/link/error";
3
3
  import { getMainDefinition } from "@apollo/client/utilities";
4
+ import { createApolloProvider } from "@vue/apollo-option";
4
5
  import { ApolloClients, provideApolloClients } from "@vue/apollo-composable";
5
6
  import { ApolloClient, ApolloLink, InMemoryCache, split } from "@apollo/client/core";
6
7
  import createUploadLink from "apollo-upload-client/createUploadLink.mjs";
@@ -13,20 +14,26 @@ import createRestartableClient from "./ws.mjs";
13
14
  import { useApollo } from "./composables.mjs";
14
15
  import PusherLink from "./pusher.mjs";
15
16
  import { ref, useCookie, defineNuxtPlugin, useRequestHeaders } from "#imports";
16
- import NuxtApollo from "#apollo";
17
+ import { NuxtApollo } from "#apollo";
17
18
  export default defineNuxtPlugin((nuxtApp) => {
18
19
  const requestCookies = process.server && NuxtApollo.proxyCookies && useRequestHeaders(["cookie"]) || void 0;
19
20
  const clients = {};
20
21
  for (const [key, clientConfig] of Object.entries(NuxtApollo.clients)) {
21
22
  const getAuth = async () => {
22
- const token = ref();
23
+ const token = ref(null);
23
24
  await nuxtApp.callHook("apollo:auth", { token, client: key });
24
25
  if (!token.value) {
25
26
  if (clientConfig.tokenStorage === "cookie") {
26
27
  if (process.client) {
27
- token.value = useCookie(clientConfig.tokenName).value;
28
+ const t = useCookie(clientConfig.tokenName).value;
29
+ if (t) {
30
+ token.value = t;
31
+ }
28
32
  } else if (requestCookies?.cookie) {
29
- token.value = requestCookies.cookie.split(";").find((c) => c.trim().startsWith(`${clientConfig.tokenName}=`))?.split("=")?.[1];
33
+ const t = requestCookies.cookie.split(";").find((c) => c.trim().startsWith(`${clientConfig.tokenName}=`))?.split("=")?.[1];
34
+ if (t) {
35
+ token.value = t;
36
+ }
30
37
  }
31
38
  } else if (process.client && clientConfig.tokenStorage === "localStorage") {
32
39
  token.value = localStorage.getItem(clientConfig.tokenName);
@@ -54,9 +61,9 @@ export default defineNuxtPlugin((nuxtApp) => {
54
61
  }
55
62
  };
56
63
  });
57
- const getCsrfToken = async () => {
64
+ const getCsrfToken = async (forceUpdate = false) => {
58
65
  const token = ref();
59
- await nuxtApp.callHook("apollo:csrf", { token, client: key });
66
+ await nuxtApp.callHook("apollo:csrf", { token, client: key, forceUpdate });
60
67
  return token.value;
61
68
  };
62
69
  const csrfLink = setContext(async (_, { headers }) => {
@@ -85,10 +92,24 @@ export default defineNuxtPlugin((nuxtApp) => {
85
92
  if (clientConfig.persistedQueries) {
86
93
  persistedLink = createPersistedQueryLink({ sha256, useGETForHashedQueries: true });
87
94
  }
95
+ const customFetch = async (uri, options, blocked1stCall = false) => {
96
+ const response = await fetch(uri, options);
97
+ if (response.status === 419) {
98
+ const token = await getCsrfToken(true);
99
+ if (token && !blocked1stCall) {
100
+ return customFetch(uri, options, !blocked1stCall);
101
+ } else {
102
+ nuxtApp.callHook("apollo:error", { networkError: { bodyText: "Session Expired", statusCode: 419 } });
103
+ }
104
+ }
105
+ return response;
106
+ };
88
107
  const httpEndLink = createUploadLink({
89
108
  ...clientConfig?.httpLinkOptions && clientConfig.httpLinkOptions,
90
109
  uri: process.client && clientConfig.browserHttpEndpoint || clientConfig.httpEndpoint,
91
- headers: { ...clientConfig?.httpLinkOptions?.headers || {} }
110
+ headers: { ...clientConfig?.httpLinkOptions?.headers || {} },
111
+ fetch: customFetch
112
+ // use custom fetch instead of default fetch to handle status code
92
113
  });
93
114
  const httpLink = baseLink.concat(httpEndLink);
94
115
  let wsLink = null;
@@ -114,23 +135,34 @@ export default defineNuxtPlugin((nuxtApp) => {
114
135
  }
115
136
  let pusherLink = null;
116
137
  if (process.client && clientConfig.pusher) {
117
- pusherLink = new PusherLink({
118
- pusher: new Pusher(clientConfig.pusher.pusherAppKey, {
119
- wsHost: clientConfig.pusher.wsHost,
120
- wsPort: clientConfig.pusher.wsPort,
121
- forceTLS: clientConfig.pusher.forceTLS,
122
- disableStats: true,
123
- enabledTransports: ["ws", "wss"],
124
- cluster: clientConfig.pusher.cluster,
125
- channelAuthorization: {
126
- endpoint: clientConfig.pusher.channelEndpoint,
127
- headersProvider() {
128
- const { token: csrfToken } = nuxtApp.$csrfToken();
129
- const { token: authToken } = nuxtApp.$authToken();
130
- return { "X-CSRF-Token": csrfToken.value, authorization: `Bearer ${authToken.value}` };
131
- }
138
+ const pusherObj = new Pusher(clientConfig.pusher.pusherAppKey, {
139
+ wsHost: clientConfig.pusher.wsHost,
140
+ wsPort: clientConfig.pusher.wsPort,
141
+ forceTLS: clientConfig.pusher.forceTLS,
142
+ disableStats: true,
143
+ enabledTransports: ["ws", "wss"],
144
+ cluster: clientConfig.pusher.cluster,
145
+ activityTimeout: clientConfig.pusher.activityTimeout,
146
+ reconnect: {
147
+ auto: true
148
+ },
149
+ channelAuthorization: {
150
+ endpoint: clientConfig.pusher.channelEndpoint,
151
+ headersProvider() {
152
+ const { token: csrfToken } = nuxtApp.$csrfToken();
153
+ const { token: authToken } = nuxtApp.$authToken();
154
+ return { "X-CSRF-Token": csrfToken.value, authorization: `Bearer ${authToken.value}` };
132
155
  }
133
- })
156
+ }
157
+ });
158
+ const handleVisibilityChange = () => {
159
+ if (document.visibilityState === "visible" && pusherObj.connection.state !== "connected") {
160
+ pusherObj.connect();
161
+ }
162
+ };
163
+ document.addEventListener("visibilitychange", handleVisibilityChange);
164
+ pusherLink = new PusherLink({
165
+ pusher: pusherObj
134
166
  });
135
167
  }
136
168
  const errorLink = onError((err) => {
@@ -187,6 +219,7 @@ export default defineNuxtPlugin((nuxtApp) => {
187
219
  }
188
220
  provideApolloClients(clients);
189
221
  nuxtApp.vueApp.provide(ApolloClients, clients);
222
+ nuxtApp.vueApp.use(createApolloProvider({ defaultClient: clients?.default }));
190
223
  nuxtApp._apolloClients = clients;
191
224
  const defaultClient = clients?.default;
192
225
  return {
@@ -0,0 +1,202 @@
1
+ import { ClientOptions } from 'graphql-ws';
2
+ import { HttpOptions, DefaultOptions, InMemoryCacheConfig } from '@apollo/client';
3
+ import { CookieOptions } from 'nuxt/app';
4
+
5
+ type CookieAttributes = Omit< CookieOptions, 'encode' | 'decode' | 'expires' | 'default'>;
6
+
7
+ type Pusher = {
8
+ cluster: string;
9
+ wsHost: string;
10
+ wsPort: number;
11
+ forceTLS: boolean;
12
+ channelEndpoint: string;
13
+ pusherAppKey: string;
14
+ activityTimeout: number;
15
+ }
16
+
17
+ type ClientConfig = {
18
+ /**
19
+ * The GraphQL endpoint.
20
+ * @type {string}
21
+ */
22
+ httpEndpoint: string;
23
+
24
+ /**
25
+ * Provide a GraphQL endpoint to be used client-side. Overrides `httpEndpoint`.
26
+ * @type {string}
27
+ **/
28
+ browserHttpEndpoint?: string;
29
+
30
+ /**
31
+ * Provide additional configuration for the `HttpLink`.
32
+ * See https://www.apollographql.com/docs/link/links/http.html#options
33
+ * @type {HttpOptions}
34
+ **/
35
+ httpLinkOptions?: Omit<HttpOptions, 'uri'>;
36
+
37
+ /**
38
+ * Provide additional configuration for the `GraphQLWsLink`.
39
+ * See https://github.com/enisdenjo/graphql-ws/blob/master/docs/interfaces/client.ClientOptions.md
40
+ **/
41
+ wsLinkOptions?: Omit<ClientOptions, 'url' | 'connectionParams'>;
42
+
43
+ /**
44
+ * Specify a websocket endpoint to be used for subscriptions.
45
+ * The `wss` protocol is recommended in production.
46
+ * @type {string}
47
+ **/
48
+ wsEndpoint?: string;
49
+
50
+ /**
51
+ * Specify if the client should solely use WebSocket.
52
+ * requires `wsEndpoint`.
53
+ * @type {boolean}
54
+ * @default false
55
+ **/
56
+ websocketsOnly?: boolean;
57
+
58
+ /**
59
+ * Specify a pusher config to be used for subscriptions.
60
+ * @type {Pusher}
61
+ **/
62
+ pusher?: Pusher
63
+
64
+ /**
65
+ * Specify if the client should be able to connect to the Apollo Client Devtools in production mode.
66
+ * @type {boolean}
67
+ * @default false
68
+ **/
69
+ connectToDevTools?: boolean;
70
+
71
+ /**
72
+ * Configure default options to be applied to the apollo client.
73
+ **/
74
+ defaultOptions?: DefaultOptions;
75
+
76
+ /**
77
+ * Configure the in-memory cache.
78
+ **/
79
+ inMemoryCacheOptions?: InMemoryCacheConfig;
80
+
81
+ /**
82
+ * Specify the name under which the token will be stored.
83
+ * as in either a cookie or localStorage.
84
+ * @type {string}
85
+ * @default "apollo:<client-name>.token"
86
+ */
87
+ tokenName?: string;
88
+
89
+ /**
90
+ * Specify if the auth token should be stored in `cookie` or `localStorage`.
91
+ * `Cookie` storage is required for SSR.
92
+ * @type {string}
93
+ * @default "cookie"
94
+ **/
95
+ tokenStorage?: 'cookie' | 'localStorage';
96
+
97
+ /**
98
+ * Specify the Authentication scheme.
99
+ * @type {string}
100
+ * @default "Bearer"
101
+ **/
102
+ authType?: string | null;
103
+
104
+ /**
105
+ * Name of the Authentication token header.
106
+ * @type {string}
107
+ * @default "Authorization"
108
+ */
109
+ authHeader?: string;
110
+
111
+ /**
112
+ * Configuration for the auth cookie.
113
+ **/
114
+ cookieAttributes?: CookieAttributes;
115
+
116
+ /**
117
+ * Name of the CSRF token header.
118
+ * @type {string}
119
+ * @default "X-CSRF-TOKEN"
120
+ */
121
+ csrfHeader?: string
122
+
123
+ /**
124
+ * Enable automatic persisted queries.
125
+ * @type {boolean}
126
+ * @default false
127
+ */
128
+ persistedQueries?: boolean
129
+ };
130
+
131
+ interface NuxtApolloConfig<T = false> {
132
+ /**
133
+ * Determine if vue-apollo composables should be automatically imported.
134
+ * @type {boolean}
135
+ * @default true
136
+ **/
137
+ autoImports?: boolean;
138
+
139
+ /**
140
+ * Configuration of the Apollo clients.
141
+ **/
142
+ clients?: Record< string, T extends false ? string | ClientConfig : ClientConfig >;
143
+
144
+ /**
145
+ * Default options to be applied to all Apollo clients.
146
+ * This is useful for setting global defaults, and is overridden by `defaultOptions` passed directly to clients.
147
+ **/
148
+ defaultOptions?: DefaultOptions;
149
+
150
+ /**
151
+ * Pass cookies from the browser to the GraphQL API in SSR mode.
152
+ *
153
+ * @type boolean
154
+ * @default true
155
+ * */
156
+ proxyCookies?: boolean;
157
+
158
+ /**
159
+ * Specify the Authentication scheme.
160
+ * @type {string}
161
+ * @default 'Bearer'
162
+ **/
163
+ authType?: string;
164
+
165
+ /**
166
+ * Name of the Authentication token header.
167
+ * @type {string}
168
+ * @default "Authorization"
169
+ */
170
+ authHeader?: string;
171
+
172
+ /**
173
+ * Name of the CSRF token header.
174
+ * @type {string}
175
+ * @default "X-CSRF-TOKEN"
176
+ */
177
+ csrfHeader?: string;
178
+
179
+ /**
180
+ * Specify if the auth token should be stored in `cookie` or `localStorage`.
181
+ * `Cookie` storage is required for SSR.
182
+ * @type {string}
183
+ * @default "cookie"
184
+ **/
185
+ tokenStorage?: 'cookie' | 'localStorage';
186
+
187
+ /**
188
+ * Configuration for the auth cookie.
189
+ **/
190
+ cookieAttributes?: CookieAttributes;
191
+
192
+ /**
193
+ * Apollo client awareness instructs the client to send two additional headers
194
+ * `apollographql-client-name` and `apollographql-client-version` in each HTTP request.
195
+ * This behavior is disabled by default.
196
+ * @type {boolean}
197
+ * @default false
198
+ */
199
+ clientAwareness?: boolean
200
+ }
201
+
202
+ export type { ClientConfig as C, NuxtApolloConfig as N };