@gambalabs/apollo 6.0.0-alpha.10
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/LICENSE +21 -0
- package/README.md +47 -0
- package/dist/module.cjs +5 -0
- package/dist/module.d.ts +235 -0
- package/dist/module.json +8 -0
- package/dist/module.mjs +161 -0
- package/dist/runtime/composables.d.ts +45 -0
- package/dist/runtime/composables.mjs +92 -0
- package/dist/runtime/plugin.d.ts +2 -0
- package/dist/runtime/plugin.mjs +189 -0
- package/dist/runtime/pusher.d.ts +10 -0
- package/dist/runtime/pusher.mjs +100 -0
- package/dist/runtime/ws.d.ts +5 -0
- package/dist/runtime/ws.mjs +31 -0
- package/dist/types.d.ts +10 -0
- package/package.json +58 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2017 Sebastien Chopin (@Atinux)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+

|
|
2
|
+
|
|
3
|
+
<h1 align="center">Nuxt Apollo</h1>
|
|
4
|
+
|
|
5
|
+
<p align="center">Effortlessly integrate GraphQL into your Nuxt 3 projects.</p>
|
|
6
|
+
|
|
7
|
+
<p align="center">
|
|
8
|
+
<!-- <a href="https://github.com/nuxt-community/apollo-module/actions?query=branch%3Amain+event%3Apush">
|
|
9
|
+
<img alt="CI" src="https://github.com/nuxt-community/apollo-module/actions/workflows/ci.yml/badge.svg?branch=main"/>
|
|
10
|
+
</a> -->
|
|
11
|
+
|
|
12
|
+
<a href="https://npmjs.com/package/@nuxtjs/apollo/v/next">
|
|
13
|
+
<img alt="Version" src="https://img.shields.io/npm/v/@nuxtjs/apollo/next?color=blue&style=flat-square"/>
|
|
14
|
+
</a>
|
|
15
|
+
|
|
16
|
+
<a href="https://npmjs.com/package/@nuxtjs/apollo/v/next">
|
|
17
|
+
<img alt="Downloads" src="https://img.shields.io/npm/dt/@nuxtjs/apollo/next?color=blue&style=flat-square"/>
|
|
18
|
+
</a>
|
|
19
|
+
|
|
20
|
+
<a href="https://opensource.org/licenses/MIT">
|
|
21
|
+
<img alt="MIT" src="https://img.shields.io/badge/License-MIT-blue.svg?style=flat-square"/>
|
|
22
|
+
</a>
|
|
23
|
+
</p>
|
|
24
|
+
|
|
25
|
+
❯ [📖 Documentation](https://apollo.nuxtjs.org)
|
|
26
|
+
|
|
27
|
+
---
|
|
28
|
+
|
|
29
|
+
**Note:** This branch is for **Nuxt 3** compatible module. Checkout the [`v4` branch](https://github.com/nuxt-modules/apollo/tree/v4) for **Nuxt 2** support.
|
|
30
|
+
|
|
31
|
+
---
|
|
32
|
+
|
|
33
|
+
## Preview
|
|
34
|
+
|
|
35
|
+
[](https://stackblitz.com/edit/nuxt-apollo-demo)
|
|
36
|
+
|
|
37
|
+
## Development
|
|
38
|
+
|
|
39
|
+
- Clone this repository
|
|
40
|
+
- Ensure you have the latest LTS version of Node.js installed
|
|
41
|
+
- Run `corepack enable`
|
|
42
|
+
- Install dependencies using `pnpm install`
|
|
43
|
+
- Start playground with `pnpm dev`
|
|
44
|
+
|
|
45
|
+
## License
|
|
46
|
+
|
|
47
|
+
[MIT License](./LICENSE)
|
package/dist/module.cjs
ADDED
package/dist/module.d.ts
ADDED
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
import * as _nuxt_schema from '@nuxt/schema';
|
|
2
|
+
import { Ref } from 'vue';
|
|
3
|
+
import { DefaultContext } from '@apollo/client/core';
|
|
4
|
+
import { ClientOptions } from 'graphql-ws';
|
|
5
|
+
import { HttpOptions, DefaultOptions, InMemoryCacheConfig } from '@apollo/client';
|
|
6
|
+
import { CookieOptions } from 'nuxt/dist/app/composables';
|
|
7
|
+
import { ErrorResponse } from '@apollo/client/link/error';
|
|
8
|
+
export { ErrorResponse } from '@apollo/client/link/error';
|
|
9
|
+
|
|
10
|
+
type CookieAttributes = Omit< CookieOptions, 'encode' | 'decode' | 'expires' | 'default'>;
|
|
11
|
+
|
|
12
|
+
type Pusher = {
|
|
13
|
+
cluster: string;
|
|
14
|
+
wsHost: string;
|
|
15
|
+
wsPort: number;
|
|
16
|
+
forceTLS: boolean;
|
|
17
|
+
channelEndpoint: string;
|
|
18
|
+
pusherAppKey: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
type ClientConfig = {
|
|
22
|
+
/**
|
|
23
|
+
* The GraphQL endpoint.
|
|
24
|
+
* @type {string}
|
|
25
|
+
*/
|
|
26
|
+
httpEndpoint: string;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Provide a GraphQL endpoint to be used client-side. Overrides `httpEndpoint`.
|
|
30
|
+
* @type {string}
|
|
31
|
+
**/
|
|
32
|
+
browserHttpEndpoint?: string;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Provide additional configuration for the `HttpLink`.
|
|
36
|
+
* See https://www.apollographql.com/docs/link/links/http.html#options
|
|
37
|
+
* @type {HttpOptions}
|
|
38
|
+
**/
|
|
39
|
+
httpLinkOptions?: Omit<HttpOptions, 'uri'>;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Provide additional configuration for the `GraphQLWsLink`.
|
|
43
|
+
* See https://github.com/enisdenjo/graphql-ws/blob/master/docs/interfaces/client.ClientOptions.md
|
|
44
|
+
**/
|
|
45
|
+
wsLinkOptions?: Omit<ClientOptions, 'url' | 'connectionParams'>;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Specify a websocket endpoint to be used for subscriptions.
|
|
49
|
+
* The `wss` protocol is recommended in production.
|
|
50
|
+
* @type {string}
|
|
51
|
+
**/
|
|
52
|
+
wsEndpoint?: string;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Specify if the client should solely use WebSocket.
|
|
56
|
+
* requires `wsEndpoint`.
|
|
57
|
+
* @type {boolean}
|
|
58
|
+
* @default false
|
|
59
|
+
**/
|
|
60
|
+
websocketsOnly?: boolean;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Specify a pusher config to be used for subscriptions.
|
|
64
|
+
* @type {Pusher}
|
|
65
|
+
**/
|
|
66
|
+
pusher?: Pusher
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Specify if the client should be able to connect to the Apollo Client Devtools in production mode.
|
|
70
|
+
* @type {boolean}
|
|
71
|
+
* @default false
|
|
72
|
+
**/
|
|
73
|
+
connectToDevTools?: boolean;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Configure default options to be applied to the apollo client.
|
|
77
|
+
**/
|
|
78
|
+
defaultOptions?: DefaultOptions;
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Configure the in-memory cache.
|
|
82
|
+
**/
|
|
83
|
+
inMemoryCacheOptions?: InMemoryCacheConfig;
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Specify the name under which the token will be stored.
|
|
87
|
+
* as in either a cookie or localStorage.
|
|
88
|
+
* @type {string}
|
|
89
|
+
* @default "apollo:<client-name>.token"
|
|
90
|
+
*/
|
|
91
|
+
tokenName?: string;
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Specify if the auth token should be stored in `cookie` or `localStorage`.
|
|
95
|
+
* `Cookie` storage is required for SSR.
|
|
96
|
+
* @type {string}
|
|
97
|
+
* @default "cookie"
|
|
98
|
+
**/
|
|
99
|
+
tokenStorage?: 'cookie' | 'localStorage';
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Specify the Authentication scheme.
|
|
103
|
+
* @type {string}
|
|
104
|
+
* @default "Bearer"
|
|
105
|
+
**/
|
|
106
|
+
authType?: string | null;
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Name of the Authentication token header.
|
|
110
|
+
* @type {string}
|
|
111
|
+
* @default "Authorization"
|
|
112
|
+
*/
|
|
113
|
+
authHeader?: string;
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Configuration for the auth cookie.
|
|
117
|
+
**/
|
|
118
|
+
cookieAttributes?: CookieAttributes;
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Name of the CSRF token header.
|
|
122
|
+
* @type {string}
|
|
123
|
+
* @default "X-CSRF-TOKEN"
|
|
124
|
+
*/
|
|
125
|
+
csrfHeader?: string
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Enable automatic persisted queries.
|
|
129
|
+
* @type {boolean}
|
|
130
|
+
* @default false
|
|
131
|
+
*/
|
|
132
|
+
persistedQueries?: boolean
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
interface NuxtApolloConfig<T = ClientConfig> {
|
|
136
|
+
/**
|
|
137
|
+
* Determine if vue-apollo composables should be automatically imported.
|
|
138
|
+
* @type {boolean}
|
|
139
|
+
* @default true
|
|
140
|
+
**/
|
|
141
|
+
autoImports?: boolean;
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Configuration of the Apollo clients.
|
|
145
|
+
**/
|
|
146
|
+
clients?: Record< string, T extends boolean ? string | ClientConfig : ClientConfig >;
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Default options to be applied to all Apollo clients.
|
|
150
|
+
* This is useful for setting global defaults, and is overridden by `defaultOptions` passed directly to clients.
|
|
151
|
+
**/
|
|
152
|
+
defaultOptions?: DefaultOptions;
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Pass cookies from the browser to the GraphQL API in SSR mode.
|
|
156
|
+
*
|
|
157
|
+
* @type boolean
|
|
158
|
+
* @default true
|
|
159
|
+
* */
|
|
160
|
+
proxyCookies?: boolean;
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Specify the Authentication scheme.
|
|
164
|
+
* @type {string}
|
|
165
|
+
* @default 'Bearer'
|
|
166
|
+
**/
|
|
167
|
+
authType?: string;
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Name of the Authentication token header.
|
|
171
|
+
* @type {string}
|
|
172
|
+
* @default "Authorization"
|
|
173
|
+
*/
|
|
174
|
+
authHeader?: string;
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Name of the CSRF token header.
|
|
178
|
+
* @type {string}
|
|
179
|
+
* @default "X-CSRF-TOKEN"
|
|
180
|
+
*/
|
|
181
|
+
csrfHeader?: string;
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Specify if the auth token should be stored in `cookie` or `localStorage`.
|
|
185
|
+
* `Cookie` storage is required for SSR.
|
|
186
|
+
* @type {string}
|
|
187
|
+
* @default "cookie"
|
|
188
|
+
**/
|
|
189
|
+
tokenStorage?: 'cookie' | 'localStorage';
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Configuration for the auth cookie.
|
|
193
|
+
**/
|
|
194
|
+
cookieAttributes?: CookieAttributes;
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Apollo client awareness instructs the client to send two additional headers
|
|
198
|
+
* `apollographql-client-name` and `apollographql-client-version` in each HTTP request.
|
|
199
|
+
* This behavior is disabled by default.
|
|
200
|
+
* @type {boolean}
|
|
201
|
+
* @default false
|
|
202
|
+
*/
|
|
203
|
+
clientAwareness?: boolean
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
type ModuleOptions = NuxtApolloConfig;
|
|
207
|
+
declare const _default: _nuxt_schema.NuxtModule<NuxtApolloConfig<any>>;
|
|
208
|
+
|
|
209
|
+
declare const defineApolloClient: (config: ClientConfig) => ClientConfig;
|
|
210
|
+
declare module '#app' {
|
|
211
|
+
interface RuntimeConfig {
|
|
212
|
+
apollo: NuxtApolloConfig<any>;
|
|
213
|
+
public: {
|
|
214
|
+
apollo: NuxtApolloConfig<any>;
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
interface RuntimeNuxtHooks {
|
|
218
|
+
'apollo:auth': (params: {
|
|
219
|
+
client: string;
|
|
220
|
+
token: Ref<string | null>;
|
|
221
|
+
}) => void;
|
|
222
|
+
'apollo:error': (error: ErrorResponse) => void;
|
|
223
|
+
'apollo:csrf': (params: {
|
|
224
|
+
client: string;
|
|
225
|
+
token: Ref<string | null>;
|
|
226
|
+
}) => void;
|
|
227
|
+
'apollo:link': (params: {
|
|
228
|
+
client: string;
|
|
229
|
+
context: Ref<DefaultContext | null>;
|
|
230
|
+
prevContextContext: DefaultContext | null;
|
|
231
|
+
}) => void;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
export { ClientConfig, ModuleOptions, _default as default, defineApolloClient };
|
package/dist/module.json
ADDED
package/dist/module.mjs
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { existsSync } from 'fs';
|
|
2
|
+
import jiti from 'jiti';
|
|
3
|
+
import { defu } from 'defu';
|
|
4
|
+
import { useLogger, defineNuxtModule, createResolver, addTemplate, addPlugin, addImports } from '@nuxt/kit';
|
|
5
|
+
import GraphQLPlugin from '@rollup/plugin-graphql';
|
|
6
|
+
|
|
7
|
+
const name = "@gambalabs/apollo";
|
|
8
|
+
const version = "6.0.0-alpha.10";
|
|
9
|
+
|
|
10
|
+
const logger = useLogger(name);
|
|
11
|
+
async function readConfigFile(path) {
|
|
12
|
+
return await jiti(import.meta.url, { esmResolve: true, interopDefault: true, requireCache: false })(path);
|
|
13
|
+
}
|
|
14
|
+
const module = defineNuxtModule({
|
|
15
|
+
meta: {
|
|
16
|
+
name,
|
|
17
|
+
version,
|
|
18
|
+
configKey: "apollo",
|
|
19
|
+
compatibility: {
|
|
20
|
+
nuxt: "^3.0.0-rc.9"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
defaults: {
|
|
24
|
+
autoImports: true,
|
|
25
|
+
authType: "Bearer",
|
|
26
|
+
authHeader: "Authorization",
|
|
27
|
+
csrfHeader: "X-CSRF-TOKEN",
|
|
28
|
+
tokenStorage: "cookie",
|
|
29
|
+
proxyCookies: true,
|
|
30
|
+
cookieAttributes: {
|
|
31
|
+
maxAge: 60 * 60 * 24 * 7,
|
|
32
|
+
secure: process.env.NODE_ENV === "production"
|
|
33
|
+
},
|
|
34
|
+
clientAwareness: false
|
|
35
|
+
},
|
|
36
|
+
async setup(options, nuxt) {
|
|
37
|
+
if (!options.clients || !Object.keys(options.clients).length) {
|
|
38
|
+
throw new Error("[@nuxtjs/apollo] Atleast one client must be configured.");
|
|
39
|
+
}
|
|
40
|
+
const { resolve } = createResolver(import.meta.url);
|
|
41
|
+
const rootResolver = createResolver(nuxt.options.rootDir);
|
|
42
|
+
nuxt.options.build.transpile = nuxt.options.build.transpile || [];
|
|
43
|
+
nuxt.options.build.transpile.push(
|
|
44
|
+
resolve("runtime"),
|
|
45
|
+
"@wry/context",
|
|
46
|
+
"@apollo/client",
|
|
47
|
+
"@vue/apollo-composable",
|
|
48
|
+
"ts-invariant/process"
|
|
49
|
+
);
|
|
50
|
+
const clients = {};
|
|
51
|
+
const configPaths = {};
|
|
52
|
+
async function prepareClients() {
|
|
53
|
+
for (let [k, v] of Object.entries(options.clients || {})) {
|
|
54
|
+
if (typeof v === "string") {
|
|
55
|
+
const path = rootResolver.resolve(v);
|
|
56
|
+
const resolvedConfig = existsSync(path) && await readConfigFile(path);
|
|
57
|
+
if (!resolvedConfig) {
|
|
58
|
+
logger.warn(`Unable to resolve Apollo config for client: ${k}`);
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
v = resolvedConfig;
|
|
62
|
+
if (!configPaths[k]) {
|
|
63
|
+
configPaths[k] = path;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
v.authType = v?.authType || (v?.authType === "" || v?.authType === null) ? null : options.authType;
|
|
67
|
+
v.authHeader = v?.authHeader || options.authHeader;
|
|
68
|
+
v.csrfHeader = v?.csrfHeader || options.csrfHeader;
|
|
69
|
+
v.tokenName = v?.tokenName || `apollo:${k}.token`;
|
|
70
|
+
v.tokenStorage = v?.tokenStorage || options.tokenStorage;
|
|
71
|
+
if (v.cookieAttributes) {
|
|
72
|
+
v.cookieAttributes = defu(v?.cookieAttributes, options.cookieAttributes);
|
|
73
|
+
}
|
|
74
|
+
v.defaultOptions = v?.defaultOptions || options.defaultOptions;
|
|
75
|
+
if (!v?.httpEndpoint && !v?.wsEndpoint) {
|
|
76
|
+
logger.error(`Either \`httpEndpoint\` or \`wsEndpoint\` must be provided for client: \`${k}\``);
|
|
77
|
+
}
|
|
78
|
+
clients[k] = v;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
addTemplate({
|
|
82
|
+
filename: "apollo.d.ts",
|
|
83
|
+
getContents: () => [
|
|
84
|
+
'import type { ClientConfig } from "@nuxtjs/apollo"',
|
|
85
|
+
"declare const clients: Record<string, ClientConfig>",
|
|
86
|
+
"declare const clientAwareness: boolean",
|
|
87
|
+
"declare const proxyCookies: boolean",
|
|
88
|
+
"declare const cookieAttributes: ClientConfig['cookieAttributes']",
|
|
89
|
+
"export default { clients, clientAwareness, proxyCookies, cookieAttributes }"
|
|
90
|
+
].join("\n")
|
|
91
|
+
});
|
|
92
|
+
nuxt.options.alias["#apollo"] = addTemplate({
|
|
93
|
+
filename: "apollo.mjs",
|
|
94
|
+
getContents: () => [
|
|
95
|
+
"export default {",
|
|
96
|
+
` proxyCookies: ${options.proxyCookies},`,
|
|
97
|
+
` clientAwareness: ${options.clientAwareness},`,
|
|
98
|
+
` cookieAttributes: ${JSON.stringify(options.cookieAttributes)},`,
|
|
99
|
+
` clients: ${JSON.stringify(clients)}`,
|
|
100
|
+
"}"
|
|
101
|
+
].join("\n")
|
|
102
|
+
}).dst;
|
|
103
|
+
addPlugin(resolve("runtime/plugin"));
|
|
104
|
+
addImports([
|
|
105
|
+
{ name: "gql", from: "graphql-tag" },
|
|
106
|
+
...[
|
|
107
|
+
"useApollo",
|
|
108
|
+
"useAsyncQuery",
|
|
109
|
+
"useLazyAsyncQuery"
|
|
110
|
+
].map((n) => ({ name: n, from: resolve("runtime/composables") })),
|
|
111
|
+
...!options?.autoImports ? [] : [
|
|
112
|
+
"useQuery",
|
|
113
|
+
"useLazyQuery",
|
|
114
|
+
"useMutation",
|
|
115
|
+
"useSubscription",
|
|
116
|
+
"useApolloClient",
|
|
117
|
+
"useQueryLoading",
|
|
118
|
+
"useMutationLoading",
|
|
119
|
+
"useSubscriptionLoading",
|
|
120
|
+
"useGlobalQueryLoading",
|
|
121
|
+
"useGlobalMutationLoading",
|
|
122
|
+
"useGlobalSubscriptionLoading"
|
|
123
|
+
].map((n) => ({ name: n, from: "@vue/apollo-composable" }))
|
|
124
|
+
]);
|
|
125
|
+
nuxt.hook("vite:extendConfig", (config) => {
|
|
126
|
+
config.optimizeDeps = config.optimizeDeps || {};
|
|
127
|
+
config.optimizeDeps.exclude = config.optimizeDeps.exclude || [];
|
|
128
|
+
config.optimizeDeps.exclude.push("@vue/apollo-composable");
|
|
129
|
+
config.plugins = config.plugins || [];
|
|
130
|
+
config.plugins.push(GraphQLPlugin());
|
|
131
|
+
if (!nuxt.options.dev) {
|
|
132
|
+
config.define = { ...config.define, __DEV__: false };
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
nuxt.hook("webpack:config", (configs) => {
|
|
136
|
+
for (const config of configs) {
|
|
137
|
+
const hasGqlLoader = config.module.rules.some((rule) => rule?.use === "graphql-tag/loader");
|
|
138
|
+
if (hasGqlLoader) {
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
config.module.rules.push({
|
|
142
|
+
test: /\.(graphql|gql)$/,
|
|
143
|
+
use: "graphql-tag/loader",
|
|
144
|
+
exclude: /(node_modules)/
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
nuxt.hook("builder:watch", async (_event, path) => {
|
|
149
|
+
if (!Object.values(configPaths).some((p) => p.includes(path))) {
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
logger.log("[@nuxtjs/apollo] Reloading Apollo configuration");
|
|
153
|
+
await prepareClients();
|
|
154
|
+
await nuxt.callHook("builder:generateApp");
|
|
155
|
+
});
|
|
156
|
+
await prepareClients();
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
const defineApolloClient = (config) => config;
|
|
160
|
+
|
|
161
|
+
export { module as default, defineApolloClient };
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { OperationVariables, QueryOptions } from '@apollo/client';
|
|
2
|
+
import type { AsyncData } from 'nuxt/dist/app/composables';
|
|
3
|
+
type TQuery<T> = QueryOptions<OperationVariables, T>['query'];
|
|
4
|
+
type TVariables<T> = QueryOptions<OperationVariables, T>['variables'];
|
|
5
|
+
type TAsyncQuery<T> = {
|
|
6
|
+
query: TQuery<T>;
|
|
7
|
+
variables?: TVariables<T>;
|
|
8
|
+
key?: string;
|
|
9
|
+
cache?: boolean;
|
|
10
|
+
clientId?: string;
|
|
11
|
+
};
|
|
12
|
+
export declare function useAsyncQuery<T>(opts: TAsyncQuery<T>): AsyncData<T, Error>;
|
|
13
|
+
export declare function useAsyncQuery<T>(query: TQuery<T>, clientId?: string): AsyncData<T, Error>;
|
|
14
|
+
export declare function useAsyncQuery<T>(query: TQuery<T>, variables?: TVariables<T>, clientId?: string): AsyncData<T, Error>;
|
|
15
|
+
export declare function useLazyAsyncQuery<T>(opts: TAsyncQuery<T>): AsyncData<T, Error>;
|
|
16
|
+
export declare function useLazyAsyncQuery<T>(query: TQuery<T>, clientId?: string): AsyncData<T, Error>;
|
|
17
|
+
export declare function useLazyAsyncQuery<T>(query: TQuery<T>, variables?: TVariables<T>, clientId?: string): AsyncData<T, Error>;
|
|
18
|
+
export declare const useApollo: () => {
|
|
19
|
+
/**
|
|
20
|
+
* Retrieve the auth token for the specified client. Adheres to the `apollo:auth` hook.
|
|
21
|
+
*
|
|
22
|
+
* @param {string} client The client who's token to retrieve. Defaults to `default`.
|
|
23
|
+
*/
|
|
24
|
+
getToken: (client?: string) => Promise<any>;
|
|
25
|
+
/**
|
|
26
|
+
* Access the configured apollo clients.
|
|
27
|
+
*/
|
|
28
|
+
clients: Record<string, import("@apollo/client").ApolloClient<any>> | undefined;
|
|
29
|
+
/**
|
|
30
|
+
* Apply auth token to the specified Apollo client, and optionally reset it's cache.
|
|
31
|
+
*
|
|
32
|
+
* @param {string} token The token to be applied.
|
|
33
|
+
* @param {string} client - Name of the Apollo client. Defaults to `default`.
|
|
34
|
+
* @param {boolean} skipResetStore - If `true`, the cache will not be reset.
|
|
35
|
+
* */
|
|
36
|
+
onLogin: (token?: string, client?: string, skipResetStore?: boolean) => Promise<void>;
|
|
37
|
+
/**
|
|
38
|
+
* Remove the auth token from the Apollo client, and optionally reset it's cache.
|
|
39
|
+
*
|
|
40
|
+
* @param {string} client - Name of the Apollo client. Defaults to `default`.
|
|
41
|
+
* @param {boolean} skipResetStore - If `true`, the cache will not be reset.
|
|
42
|
+
* */
|
|
43
|
+
onLogout: (client?: string, skipResetStore?: boolean) => Promise<void>;
|
|
44
|
+
};
|
|
45
|
+
export {};
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { hash } from "ohash";
|
|
2
|
+
import { print } from "graphql";
|
|
3
|
+
import { ref, useCookie, useNuxtApp, useAsyncData } from "#imports";
|
|
4
|
+
import NuxtApollo from "#build/apollo";
|
|
5
|
+
export function useAsyncQuery(...args) {
|
|
6
|
+
const { key, fn } = prep(...args);
|
|
7
|
+
return useAsyncData(key, fn);
|
|
8
|
+
}
|
|
9
|
+
export function useLazyAsyncQuery(...args) {
|
|
10
|
+
const { key, fn } = prep(...args);
|
|
11
|
+
return useAsyncData(key, fn, { lazy: true });
|
|
12
|
+
}
|
|
13
|
+
const prep = (...args) => {
|
|
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;
|
|
19
|
+
if (!clientId || !clients?.[clientId]) {
|
|
20
|
+
clientId = clients?.default ? "default" : Object.keys(clients)[0];
|
|
21
|
+
}
|
|
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 };
|
|
25
|
+
};
|
|
26
|
+
export const useApollo = () => {
|
|
27
|
+
const nuxtApp = useNuxtApp();
|
|
28
|
+
const getToken = async (client) => {
|
|
29
|
+
client = client || "default";
|
|
30
|
+
const conf = NuxtApollo?.clients?.[client];
|
|
31
|
+
const token = ref(null);
|
|
32
|
+
await nuxtApp.callHook("apollo:auth", { token, client });
|
|
33
|
+
if (token.value) {
|
|
34
|
+
return token.value;
|
|
35
|
+
}
|
|
36
|
+
const tokenName = conf.tokenName;
|
|
37
|
+
return conf?.tokenStorage === "cookie" ? useCookie(tokenName).value : process.client && localStorage.getItem(tokenName) || null;
|
|
38
|
+
};
|
|
39
|
+
const updateAuth = async ({ token, client, mode, skipResetStore }) => {
|
|
40
|
+
client = client || "default";
|
|
41
|
+
const conf = NuxtApollo?.clients?.[client];
|
|
42
|
+
const tokenName = client && conf.tokenName;
|
|
43
|
+
if (conf?.tokenStorage === "cookie") {
|
|
44
|
+
const cookieOpts = client && conf?.cookieAttributes || NuxtApollo?.cookieAttributes;
|
|
45
|
+
const cookie = useCookie(tokenName, cookieOpts);
|
|
46
|
+
if (!cookie.value && mode === "logout") {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
cookie.value = mode === "login" && token || null;
|
|
50
|
+
} else if (process.client && conf?.tokenStorage === "localStorage") {
|
|
51
|
+
if (mode === "login" && token) {
|
|
52
|
+
localStorage.setItem(tokenName, token);
|
|
53
|
+
} else if (mode === "logout") {
|
|
54
|
+
localStorage.removeItem(tokenName);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
if (nuxtApp?._apolloWsClients?.[client]) {
|
|
58
|
+
nuxtApp._apolloWsClients[client].restart();
|
|
59
|
+
}
|
|
60
|
+
if (skipResetStore) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
await nuxtApp?._apolloClients?.[client].resetStore().catch((e) => console.log("%cError on cache reset", "color: orange;", e.message));
|
|
64
|
+
};
|
|
65
|
+
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
|
+
getToken,
|
|
72
|
+
/**
|
|
73
|
+
* Access the configured apollo clients.
|
|
74
|
+
*/
|
|
75
|
+
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
|
+
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
|
+
onLogout: (client, skipResetStore) => updateAuth({ client, skipResetStore, mode: "logout" })
|
|
91
|
+
};
|
|
92
|
+
};
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import destr from "destr";
|
|
2
|
+
import { onError } from "@apollo/client/link/error";
|
|
3
|
+
import { getMainDefinition } from "@apollo/client/utilities";
|
|
4
|
+
import { ApolloClients, provideApolloClients } from "@vue/apollo-composable";
|
|
5
|
+
import { ApolloClient, ApolloLink, InMemoryCache, split } from "@apollo/client/core";
|
|
6
|
+
import createUploadLink from "apollo-upload-client/createUploadLink.mjs";
|
|
7
|
+
import { createPersistedQueryLink } from "@apollo/client/link/persisted-queries";
|
|
8
|
+
import { sha256 } from "crypto-hash";
|
|
9
|
+
import { GraphQLWsLink } from "@apollo/client/link/subscriptions";
|
|
10
|
+
import { setContext } from "@apollo/client/link/context";
|
|
11
|
+
import Pusher from "pusher-js";
|
|
12
|
+
import createRestartableClient from "./ws.mjs";
|
|
13
|
+
import { useApollo } from "./composables.mjs";
|
|
14
|
+
import PusherLink from "./pusher.mjs";
|
|
15
|
+
import { ref, useCookie, defineNuxtPlugin, useRequestHeaders } from "#imports";
|
|
16
|
+
import NuxtApollo from "#apollo";
|
|
17
|
+
export default defineNuxtPlugin((nuxtApp) => {
|
|
18
|
+
const requestCookies = process.server && NuxtApollo.proxyCookies && useRequestHeaders(["cookie"]) || void 0;
|
|
19
|
+
const clients = {};
|
|
20
|
+
for (const [key, clientConfig] of Object.entries(NuxtApollo.clients)) {
|
|
21
|
+
const getAuth = async () => {
|
|
22
|
+
const token = ref();
|
|
23
|
+
await nuxtApp.callHook("apollo:auth", { token, client: key });
|
|
24
|
+
if (!token.value) {
|
|
25
|
+
if (clientConfig.tokenStorage === "cookie") {
|
|
26
|
+
if (process.client) {
|
|
27
|
+
token.value = useCookie(clientConfig.tokenName).value;
|
|
28
|
+
} else if (requestCookies?.cookie) {
|
|
29
|
+
token.value = requestCookies.cookie.split(";").find((c) => c.trim().startsWith(`${clientConfig.tokenName}=`))?.split("=")?.[1];
|
|
30
|
+
}
|
|
31
|
+
} else if (process.client && clientConfig.tokenStorage === "localStorage") {
|
|
32
|
+
token.value = localStorage.getItem(clientConfig.tokenName);
|
|
33
|
+
}
|
|
34
|
+
if (!token.value) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
const authScheme = !!token.value?.match(/^[a-zA-Z]+\s/)?.[0];
|
|
39
|
+
if (authScheme || clientConfig?.authType === null) {
|
|
40
|
+
return token.value;
|
|
41
|
+
}
|
|
42
|
+
return `${clientConfig?.authType} ${token.value}`;
|
|
43
|
+
};
|
|
44
|
+
const authLink = setContext(async (_, { headers }) => {
|
|
45
|
+
const auth = await getAuth();
|
|
46
|
+
if (!auth) {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
headers: {
|
|
51
|
+
...headers,
|
|
52
|
+
...requestCookies && requestCookies,
|
|
53
|
+
[clientConfig.authHeader]: auth
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
});
|
|
57
|
+
const getCsrfToken = async () => {
|
|
58
|
+
const token = ref();
|
|
59
|
+
await nuxtApp.callHook("apollo:csrf", { token, client: key });
|
|
60
|
+
return token.value;
|
|
61
|
+
};
|
|
62
|
+
const csrfLink = setContext(async (_, { headers }) => {
|
|
63
|
+
const token = await getCsrfToken();
|
|
64
|
+
if (!token) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
return {
|
|
68
|
+
headers: {
|
|
69
|
+
...headers,
|
|
70
|
+
...requestCookies && requestCookies,
|
|
71
|
+
[clientConfig.csrfHeader]: token
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
});
|
|
75
|
+
const contextLink = setContext(async (_, prevContext) => {
|
|
76
|
+
const context = ref(null);
|
|
77
|
+
await nuxtApp.callHook("apollo:link", { prevContext, context, client: key });
|
|
78
|
+
if (!context.value) {
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
return context.value;
|
|
82
|
+
});
|
|
83
|
+
let baseLink = csrfLink.concat(authLink).concat(contextLink);
|
|
84
|
+
if (clientConfig.persistedQueries) {
|
|
85
|
+
const persistedLink = createPersistedQueryLink({ sha256, useGETForHashedQueries: true });
|
|
86
|
+
baseLink = baseLink.concat(persistedLink);
|
|
87
|
+
}
|
|
88
|
+
const httpEndLink = createUploadLink({
|
|
89
|
+
...clientConfig?.httpLinkOptions && clientConfig.httpLinkOptions,
|
|
90
|
+
uri: process.client && clientConfig.browserHttpEndpoint || clientConfig.httpEndpoint,
|
|
91
|
+
headers: { ...clientConfig?.httpLinkOptions?.headers || {} }
|
|
92
|
+
});
|
|
93
|
+
const httpLink = baseLink.concat(httpEndLink);
|
|
94
|
+
let wsLink = null;
|
|
95
|
+
if (process.client && clientConfig.wsEndpoint) {
|
|
96
|
+
const wsClient = createRestartableClient({
|
|
97
|
+
...clientConfig.wsLinkOptions,
|
|
98
|
+
url: clientConfig.wsEndpoint,
|
|
99
|
+
connectionParams: async () => {
|
|
100
|
+
const auth = await getAuth();
|
|
101
|
+
const csrf = await getCsrfToken();
|
|
102
|
+
if (!auth && !csrf) {
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
return {
|
|
106
|
+
...auth ? { [clientConfig.authHeader]: auth } : {},
|
|
107
|
+
...csrf ? { [clientConfig.csrfHeader]: csrf } : {}
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
wsLink = new GraphQLWsLink(wsClient);
|
|
112
|
+
nuxtApp._apolloWsClients = nuxtApp._apolloWsClients || {};
|
|
113
|
+
nuxtApp._apolloWsClients[key] = wsClient;
|
|
114
|
+
}
|
|
115
|
+
let pusherLink = null;
|
|
116
|
+
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
|
+
}
|
|
132
|
+
}
|
|
133
|
+
})
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
const errorLink = onError((err) => {
|
|
137
|
+
nuxtApp.callHook("apollo:error", err);
|
|
138
|
+
});
|
|
139
|
+
const link = pusherLink ? ApolloLink.from([
|
|
140
|
+
errorLink,
|
|
141
|
+
baseLink,
|
|
142
|
+
pusherLink,
|
|
143
|
+
httpEndLink
|
|
144
|
+
]) : ApolloLink.from([
|
|
145
|
+
errorLink,
|
|
146
|
+
...!wsLink ? [httpLink] : [
|
|
147
|
+
...clientConfig?.websocketsOnly ? [wsLink] : [
|
|
148
|
+
split(
|
|
149
|
+
({ query }) => {
|
|
150
|
+
const definition = getMainDefinition(query);
|
|
151
|
+
return definition.kind === "OperationDefinition" && definition.operation === "subscription";
|
|
152
|
+
},
|
|
153
|
+
wsLink,
|
|
154
|
+
httpLink
|
|
155
|
+
)
|
|
156
|
+
]
|
|
157
|
+
]
|
|
158
|
+
]);
|
|
159
|
+
const cache = new InMemoryCache(clientConfig.inMemoryCacheOptions);
|
|
160
|
+
clients[key] = new ApolloClient({
|
|
161
|
+
link,
|
|
162
|
+
cache,
|
|
163
|
+
...NuxtApollo.clientAwareness && { name: key },
|
|
164
|
+
...process.server ? { ssrMode: true } : { ssrForceFetchDelay: 100 },
|
|
165
|
+
connectToDevTools: clientConfig.connectToDevTools || false,
|
|
166
|
+
defaultOptions: clientConfig?.defaultOptions
|
|
167
|
+
});
|
|
168
|
+
if (!clients?.default && !NuxtApollo?.clients?.default && key === Object.keys(NuxtApollo.clients)[0]) {
|
|
169
|
+
clients.default = clients[key];
|
|
170
|
+
}
|
|
171
|
+
const cacheKey = `_apollo:${key}`;
|
|
172
|
+
nuxtApp.hook("app:rendered", () => {
|
|
173
|
+
nuxtApp.payload.data[cacheKey] = cache.extract();
|
|
174
|
+
});
|
|
175
|
+
if (process.client && nuxtApp.payload.data[cacheKey]) {
|
|
176
|
+
cache.restore(destr(JSON.stringify(nuxtApp.payload.data[cacheKey])));
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
provideApolloClients(clients);
|
|
180
|
+
nuxtApp.vueApp.provide(ApolloClients, clients);
|
|
181
|
+
nuxtApp._apolloClients = clients;
|
|
182
|
+
const defaultClient = clients?.default;
|
|
183
|
+
return {
|
|
184
|
+
provide: {
|
|
185
|
+
apolloHelpers: useApollo(),
|
|
186
|
+
apollo: { clients, defaultClient }
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
});
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export default PusherLink;
|
|
2
|
+
declare class PusherLink extends ApolloLink {
|
|
3
|
+
constructor(options: any);
|
|
4
|
+
pusher: any;
|
|
5
|
+
request(operation: any, forward: any): Observable<any>;
|
|
6
|
+
subscribeToChannel(subscriptionChannel: any, event: string | undefined, observer: any): void;
|
|
7
|
+
unsubscribeFromChannel(subscriptionChannel: any): void;
|
|
8
|
+
}
|
|
9
|
+
import { ApolloLink } from '@apollo/client/core';
|
|
10
|
+
import { Observable } from '@apollo/client/core';
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { ApolloLink, Observable } from '@apollo/client/core'
|
|
2
|
+
|
|
3
|
+
// Inspired by https://github.com/rmosolgo/graphql-ruby/blob/master/javascript_client/src/subscriptions/PusherLink.ts
|
|
4
|
+
class PusherLink extends ApolloLink {
|
|
5
|
+
constructor (options) {
|
|
6
|
+
super()
|
|
7
|
+
|
|
8
|
+
this.pusher = options.pusher
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
request (operation, forward) {
|
|
12
|
+
const subscribeObservable = new Observable((_observer) => {
|
|
13
|
+
//
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
// Capture the super method
|
|
17
|
+
const prevSubscribe =
|
|
18
|
+
subscribeObservable.subscribe.bind(subscribeObservable)
|
|
19
|
+
|
|
20
|
+
// Override subscribe to return an `unsubscribe` object, see
|
|
21
|
+
// https://github.com/apollographql/subscriptions-transport-ws/blob/master/src/client.ts#L182-L212
|
|
22
|
+
subscribeObservable.subscribe = (observerOrNext, onError, onComplete) => {
|
|
23
|
+
prevSubscribe(observerOrNext, onError, onComplete)
|
|
24
|
+
|
|
25
|
+
const observer = getObserver(observerOrNext, onError, onComplete)
|
|
26
|
+
|
|
27
|
+
let subscriptionChannel
|
|
28
|
+
|
|
29
|
+
forward(operation).subscribe({
|
|
30
|
+
next: (data) => {
|
|
31
|
+
// If the operation has the subscription channel, it's a subscription
|
|
32
|
+
subscriptionChannel =
|
|
33
|
+
data?.extensions?.lighthouse_subscriptions.channel ?? null
|
|
34
|
+
// No subscription found in the response, pipe data through
|
|
35
|
+
if (!subscriptionChannel) {
|
|
36
|
+
observer.next(data)
|
|
37
|
+
observer.complete()
|
|
38
|
+
|
|
39
|
+
return
|
|
40
|
+
}
|
|
41
|
+
const event = data?.extensions?.lighthouse_subscriptions.event ?? 'lighthouse-subscription'
|
|
42
|
+
this.subscribeToChannel(subscriptionChannel, event, observer)
|
|
43
|
+
}
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
// Return an object that will unsubscribe_if the query was a subscription
|
|
47
|
+
return {
|
|
48
|
+
closed: false,
|
|
49
|
+
unsubscribe: () => {
|
|
50
|
+
subscriptionChannel &&
|
|
51
|
+
this.unsubscribeFromChannel(subscriptionChannel)
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return subscribeObservable
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
subscribeToChannel (subscriptionChannel, event = 'lighthouse-subscription', observer) {
|
|
60
|
+
this.pusher
|
|
61
|
+
.subscribe(subscriptionChannel)
|
|
62
|
+
.bind(event, (payload) => {
|
|
63
|
+
const { result } = payload
|
|
64
|
+
if (result) {
|
|
65
|
+
observer.next(result)
|
|
66
|
+
}
|
|
67
|
+
if (!payload.more) {
|
|
68
|
+
this.unsubscribeFromChannel(subscriptionChannel)
|
|
69
|
+
|
|
70
|
+
observer.complete()
|
|
71
|
+
}
|
|
72
|
+
})
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
unsubscribeFromChannel (subscriptionChannel) {
|
|
76
|
+
this.pusher.unsubscribe(subscriptionChannel)
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Turn `subscribe` arguments into an observer-like thing, see getObserver
|
|
81
|
+
// https://github.com/apollographql/subscriptions-transport-ws/blob/master/src/client.ts#L329-L343
|
|
82
|
+
function getObserver (observerOrNext, onError, onComplete) {
|
|
83
|
+
if (typeof observerOrNext === 'function') {
|
|
84
|
+
// Duck-type an observer
|
|
85
|
+
return {
|
|
86
|
+
next: v => observerOrNext(v),
|
|
87
|
+
error: e => onError && onError(e),
|
|
88
|
+
complete: () => onComplete && onComplete()
|
|
89
|
+
}
|
|
90
|
+
} else {
|
|
91
|
+
// Make an object that calls to the given object, with safety checks
|
|
92
|
+
return {
|
|
93
|
+
next: v => observerOrNext.next && observerOrNext.next(v),
|
|
94
|
+
error: e => observerOrNext.error && observerOrNext.error(e),
|
|
95
|
+
complete: () => observerOrNext.complete && observerOrNext.complete()
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export default PusherLink
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { createClient } from "graphql-ws";
|
|
2
|
+
export default function createRestartableClient(options) {
|
|
3
|
+
let restartRequested = false;
|
|
4
|
+
let restart = () => {
|
|
5
|
+
restartRequested = true;
|
|
6
|
+
};
|
|
7
|
+
const client = createClient({
|
|
8
|
+
...options,
|
|
9
|
+
on: {
|
|
10
|
+
...options.on,
|
|
11
|
+
opened: (socket) => {
|
|
12
|
+
options.on?.opened?.(socket);
|
|
13
|
+
restart = () => {
|
|
14
|
+
if (socket.readyState === WebSocket.OPEN) {
|
|
15
|
+
socket.close(4205, "Client Restart");
|
|
16
|
+
} else {
|
|
17
|
+
restartRequested = true;
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
if (restartRequested) {
|
|
21
|
+
restartRequested = false;
|
|
22
|
+
restart();
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
return {
|
|
28
|
+
...client,
|
|
29
|
+
restart: () => restart()
|
|
30
|
+
};
|
|
31
|
+
}
|
package/dist/types.d.ts
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@gambalabs/apollo",
|
|
3
|
+
"version": "6.0.0-alpha.10",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"repository": "https://github.com/GambaLabs/apollo",
|
|
6
|
+
"homepage": "https://apollo.nuxtjs.org",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/types.d.ts",
|
|
11
|
+
"import": "./dist/module.mjs"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"main": "./dist/module.mjs",
|
|
15
|
+
"types": "./dist/module.d.ts",
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@apollo/client": "^3.8.7",
|
|
21
|
+
"@nuxt/kit": "^3.8.2",
|
|
22
|
+
"@rollup/plugin-graphql": "^2.0.4",
|
|
23
|
+
"@vue/apollo-composable": "4.0.0-beta.4",
|
|
24
|
+
"apollo-upload-client": "^18.0.1",
|
|
25
|
+
"crypto-hash": "^2.0.1",
|
|
26
|
+
"defu": "^6.1.3",
|
|
27
|
+
"destr": "^1.2.2",
|
|
28
|
+
"graphql": "^16.8.1",
|
|
29
|
+
"graphql-tag": "^2.12.6",
|
|
30
|
+
"graphql-ws": "^5.14.2",
|
|
31
|
+
"jiti": "^1.21.0",
|
|
32
|
+
"ohash": "^1.1.3",
|
|
33
|
+
"pusher-js": "^8.3.0"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@nuxt/module-builder": "^0.2.1",
|
|
37
|
+
"@nuxt/schema": "^3.8.2",
|
|
38
|
+
"@nuxt/ui": "^0.4.1",
|
|
39
|
+
"@nuxtjs/eslint-config-typescript": "^12.1.0",
|
|
40
|
+
"@types/node": "^18.18.13",
|
|
41
|
+
"eslint": "^8.54.0",
|
|
42
|
+
"nuxt": "^3.8.2"
|
|
43
|
+
},
|
|
44
|
+
"publishConfig": {
|
|
45
|
+
"access": "public"
|
|
46
|
+
},
|
|
47
|
+
"resolutions": {
|
|
48
|
+
"@nuxtjs/apollo": "link:."
|
|
49
|
+
},
|
|
50
|
+
"packageManager": "pnpm@7.29.1",
|
|
51
|
+
"scripts": {
|
|
52
|
+
"build": "nuxt-module-build",
|
|
53
|
+
"dev": "nuxi dev playground --dotenv .env --host",
|
|
54
|
+
"dev:build": "nuxi build playground",
|
|
55
|
+
"dev:prepare": "nuxt-module-build --stub && nuxi prepare playground",
|
|
56
|
+
"release": "standard-version --prerelease alpha && git push --follow-tags && pnpm publish --tag next"
|
|
57
|
+
}
|
|
58
|
+
}
|