nuxt-graphql-middleware 5.0.0-alpha.13 → 5.0.0-alpha.15

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/README.md CHANGED
@@ -1,9 +1,8 @@
1
1
  ![nuxt-graphql-middleware banner](docs/banner.png?raw=true 'Nuxt GraphQL Middleware - Expose queries and mutations as fully typed API routes.')
2
2
 
3
- # Nuxt GraphQL Middleware
3
+ # nuxt-graphql-middleware
4
4
 
5
- Keep your GraphQL server private: Expose GraphQL queries and mutations as fully
6
- typed API routes.
5
+ A GraphQL client for Nuxt 3.
7
6
 
8
7
  **[Documentation](https://nuxt-graphql-middleware.dulnan.net)** –
9
8
  **[npm](https://www.npmjs.com/package/nuxt-graphql-middleware)** –
@@ -13,19 +12,20 @@ typed API routes.
13
12
 
14
13
  ## Features
15
14
 
16
- - Exposes each query and mutation as an API route
17
- - GraphQL requests are only done on the server side
18
- - No GraphQL documents in client bundle
19
- - Includes composables to perform queries or mutations
20
- - Modify request headers, responses and handle errors
21
- - HMR for all GraphQL files
22
- - Integration with [Nuxt DevTools](https://devtools.nuxtjs.org)
23
- - Full TypeScript integration for schema, queries, mutations and fragments using
24
- [graphql-code-generator](https://github.com/dotansimha/graphql-code-generator)
15
+ - Exposes each query and mutation as an **API route**
16
+ - GraphQL requests are done **only on the server side**
17
+ - Includes **composables** to perform queries or mutations
18
+ - **No GraphQL documents** in client bundle
19
+ - Super fast **TypeScript code generation** using
20
+ **[graphql-typescript-deluxe](https://github.com/dulnan/graphql-typescript-deluxe)**
21
+ - **HMR** for all GraphQL files
22
+ - Optional **Client side caching** for query operations
23
+ - Modify **request headers**, responses and handle errors
24
+ - Integration with **[Nuxt DevTools](https://devtools.nuxtjs.org)**
25
25
 
26
- # Setup
26
+ ## Setup
27
27
 
28
- ## Install
28
+ ### Install
29
29
 
30
30
  ```bash
31
31
  npx nuxi@latest module add nuxt-graphql-middleware
@@ -42,6 +42,9 @@ export default defineNuxtConfig({
42
42
  })
43
43
  ```
44
44
 
45
+ See
46
+ [all configuration options](https://nuxt-graphql-middleware.dulnan.net/configuration/module)
47
+
45
48
  ## Usage
46
49
 
47
50
  Write your first query, e.g. in pages/films.query.graphql:
@@ -59,7 +62,7 @@ query films {
59
62
  Your query is now available via the useGraphqlQuery() composable:
60
63
 
61
64
  ```typescript
62
- const { data } = await useGraphqlQuery('films')
65
+ const { data, errors } = await useGraphqlQuery('films')
63
66
  console.log(data.allFilms.films)
64
67
  ```
65
68
 
@@ -70,12 +73,91 @@ const { data } = await useAsyncGraphqlQuery('films')
70
73
  console.log(data.value.allFilms.films)
71
74
  ```
72
75
 
73
- Alternatively you can also call
74
- `http://localhost:3000/api/graphql_middleware/query/films` to get the same
75
- result.
76
+ Alternatively you can also directly call the API route to get the same result:
77
+
78
+ ```typescript
79
+ const response = await $fetch('/api/graphql_middleware/query/films')
80
+ ```
81
+
82
+ Or using `useFetch`:
83
+
84
+ ```typescript
85
+ const { data } = await useFetch('/api/graphql_middleware/query/films')
86
+ ```
76
87
 
77
88
  ## Nuxt 2
78
89
 
79
90
  The 3.x releases are only compatible with Nuxt 3. The
80
91
  [2.x branch](https://github.com/dulnan/nuxt-graphql-middleware/tree/2.x) and
81
- releases on npm are compatible with Nuxt 2.
92
+ releases on npm are compatible with Nuxt 2. However this version is not
93
+ maintained anymore.
94
+
95
+ ## Development
96
+
97
+ The module uses the default Nuxt module authoring setup where the module itself
98
+ is located in `./src`, with a playground located in `./playground/`.
99
+
100
+ ### Setup
101
+
102
+ #### Install dependencies
103
+
104
+ Install the dependencies of the module and playground:
105
+
106
+ ```sh
107
+ npm install
108
+ ```
109
+
110
+ #### Prepare Types
111
+
112
+ This will generate all the types needed to start developing:
113
+
114
+ ```sh
115
+ npm run dev:prepare
116
+ ```
117
+
118
+ #### Start Apollo Server
119
+
120
+ The playground uses an Apollo server that needs to be built separately.
121
+
122
+ ```sh
123
+ cd apollo
124
+ npm install
125
+ npm run compile
126
+ npm run start
127
+ ```
128
+
129
+ ### Start the Playground
130
+
131
+ ```sh
132
+ npm run dev
133
+ ```
134
+
135
+ You can now open http://localhost:3000 to start developing.
136
+
137
+ ### Testing
138
+
139
+ #### Lint / Code Style
140
+
141
+ ```sh
142
+ npm run lint
143
+ npm run prettier
144
+ ```
145
+
146
+ #### Unit Tests (Vitest)
147
+
148
+ Unit tests are done using Vitest.
149
+
150
+ ```sh
151
+ npm run test:ci
152
+ ```
153
+
154
+ #### E2E (Cypress)
155
+
156
+ We use Cypress to run some E2E tests. The tests are executed against the
157
+ playground **build**:
158
+
159
+ ```sh
160
+ npm run dev:build
161
+ npm run dev:start
162
+ npm run cypress
163
+ ```
@@ -7,5 +7,5 @@
7
7
  <link rel="prefetch" as="script" crossorigin href="/__nuxt-graphql-middleware/_nuxt/u9er1db2.js">
8
8
  <link rel="prefetch" as="style" crossorigin href="/__nuxt-graphql-middleware/_nuxt/error-500._g0akJim.css">
9
9
  <link rel="prefetch" as="script" crossorigin href="/__nuxt-graphql-middleware/_nuxt/AZpplOcD.js">
10
- <script type="module" src="/__nuxt-graphql-middleware/_nuxt/M311G39J.js" crossorigin></script></head><body><div id="__nuxt"></div><div id="teleports"></div><script type="application/json" data-nuxt-data="nuxt-app" data-ssr="false" id="__NUXT_DATA__">[{"prerenderedAt":1,"serverRendered":2},1743835674022,false]</script>
11
- <script>window.__NUXT__={};window.__NUXT__.config={public:{},app:{baseURL:"/__nuxt-graphql-middleware",buildId:"332611ea-70e0-4678-afde-6ef504a9ef06",buildAssetsDir:"/_nuxt/",cdnURL:""}}</script></body></html>
10
+ <script type="module" src="/__nuxt-graphql-middleware/_nuxt/M311G39J.js" crossorigin></script></head><body><div id="__nuxt"></div><div id="teleports"></div><script type="application/json" data-nuxt-data="nuxt-app" data-ssr="false" id="__NUXT_DATA__">[{"prerenderedAt":1,"serverRendered":2},1744006183997,false]</script>
11
+ <script>window.__NUXT__={};window.__NUXT__.config={public:{},app:{baseURL:"/__nuxt-graphql-middleware",buildId:"e8397e32-8634-465d-8c5a-ae7d6f943789",buildAssetsDir:"/_nuxt/",cdnURL:""}}</script></body></html>
@@ -7,5 +7,5 @@
7
7
  <link rel="prefetch" as="script" crossorigin href="/__nuxt-graphql-middleware/_nuxt/u9er1db2.js">
8
8
  <link rel="prefetch" as="style" crossorigin href="/__nuxt-graphql-middleware/_nuxt/error-500._g0akJim.css">
9
9
  <link rel="prefetch" as="script" crossorigin href="/__nuxt-graphql-middleware/_nuxt/AZpplOcD.js">
10
- <script type="module" src="/__nuxt-graphql-middleware/_nuxt/M311G39J.js" crossorigin></script></head><body><div id="__nuxt"></div><div id="teleports"></div><script type="application/json" data-nuxt-data="nuxt-app" data-ssr="false" id="__NUXT_DATA__">[{"prerenderedAt":1,"serverRendered":2},1743835674022,false]</script>
11
- <script>window.__NUXT__={};window.__NUXT__.config={public:{},app:{baseURL:"/__nuxt-graphql-middleware",buildId:"332611ea-70e0-4678-afde-6ef504a9ef06",buildAssetsDir:"/_nuxt/",cdnURL:""}}</script></body></html>
10
+ <script type="module" src="/__nuxt-graphql-middleware/_nuxt/M311G39J.js" crossorigin></script></head><body><div id="__nuxt"></div><div id="teleports"></div><script type="application/json" data-nuxt-data="nuxt-app" data-ssr="false" id="__NUXT_DATA__">[{"prerenderedAt":1,"serverRendered":2},1744006183997,false]</script>
11
+ <script>window.__NUXT__={};window.__NUXT__.config={public:{},app:{baseURL:"/__nuxt-graphql-middleware",buildId:"e8397e32-8634-465d-8c5a-ae7d6f943789",buildAssetsDir:"/_nuxt/",cdnURL:""}}</script></body></html>
@@ -1 +1 @@
1
- {"id":"332611ea-70e0-4678-afde-6ef504a9ef06","timestamp":1743835671841}
1
+ {"id":"e8397e32-8634-465d-8c5a-ae7d6f943789","timestamp":1744006181773}
@@ -0,0 +1 @@
1
+ {"id":"e8397e32-8634-465d-8c5a-ae7d6f943789","timestamp":1744006181773,"matcher":{"static":{},"wildcard":{},"dynamic":{}},"prerendered":[]}
@@ -7,5 +7,5 @@
7
7
  <link rel="prefetch" as="script" crossorigin href="/__nuxt-graphql-middleware/_nuxt/u9er1db2.js">
8
8
  <link rel="prefetch" as="style" crossorigin href="/__nuxt-graphql-middleware/_nuxt/error-500._g0akJim.css">
9
9
  <link rel="prefetch" as="script" crossorigin href="/__nuxt-graphql-middleware/_nuxt/AZpplOcD.js">
10
- <script type="module" src="/__nuxt-graphql-middleware/_nuxt/M311G39J.js" crossorigin></script></head><body><div id="__nuxt"></div><div id="teleports"></div><script type="application/json" data-nuxt-data="nuxt-app" data-ssr="false" id="__NUXT_DATA__">[{"prerenderedAt":1,"serverRendered":2},1743835674022,false]</script>
11
- <script>window.__NUXT__={};window.__NUXT__.config={public:{},app:{baseURL:"/__nuxt-graphql-middleware",buildId:"332611ea-70e0-4678-afde-6ef504a9ef06",buildAssetsDir:"/_nuxt/",cdnURL:""}}</script></body></html>
10
+ <script type="module" src="/__nuxt-graphql-middleware/_nuxt/M311G39J.js" crossorigin></script></head><body><div id="__nuxt"></div><div id="teleports"></div><script type="application/json" data-nuxt-data="nuxt-app" data-ssr="false" id="__NUXT_DATA__">[{"prerenderedAt":1,"serverRendered":2},1744006183997,false]</script>
11
+ <script>window.__NUXT__={};window.__NUXT__.config={public:{},app:{baseURL:"/__nuxt-graphql-middleware",buildId:"e8397e32-8634-465d-8c5a-ae7d6f943789",buildAssetsDir:"/_nuxt/",cdnURL:""}}</script></body></html>
package/dist/module.d.mts CHANGED
@@ -1,10 +1,13 @@
1
1
  import * as _nuxt_schema from '@nuxt/schema';
2
- import { HookResult } from 'nuxt/schema';
3
- import { OperationResponseError } from '../dist/runtime/types.js';
4
- export { GraphqlMiddlewareServerOptions } from '../dist/runtime/types.js';
5
2
  import { Types } from '@graphql-codegen/plugin-helpers';
6
3
  import { SchemaASTConfig } from '@graphql-codegen/schema-ast';
7
- import { GeneratorOptions } from 'graphql-typescript-deluxe';
4
+ import { GeneratorOptions, GeneratorOutput } from 'graphql-typescript-deluxe';
5
+ import { GraphQLSchema, GraphQLNamedType } from 'graphql';
6
+ import { Nuxt, ResolvedNuxtTemplate, WatchEvent, HookResult } from 'nuxt/schema';
7
+ import { Resolver } from '@nuxt/kit';
8
+ import { RouterMethod } from 'h3';
9
+ import { OperationResponseError } from '../dist/runtime/types.js';
10
+ export { GraphqlMiddlewareServerOptions } from '../dist/runtime/types.js';
8
11
 
9
12
  interface ModuleOptions {
10
13
  /**
@@ -125,6 +128,8 @@ interface ModuleOptions {
125
128
  logOnlyErrors?: boolean;
126
129
  /**
127
130
  * Options for graphql-typescript-deluxe code generator.
131
+ *
132
+ * @see [GeneratorOptions](https://github.com/dulnan/graphql-typescript-deluxe/blob/main/src/types/options.ts#L193)
128
133
  */
129
134
  codegenConfig?: GeneratorOptions;
130
135
  /**
@@ -133,6 +138,8 @@ interface ModuleOptions {
133
138
  codegenSchemaConfig?: {
134
139
  /**
135
140
  * Configure how the schema.graphql file should be generated.
141
+ *
142
+ * @see [SchemaASTConfig](https://github.com/dotansimha/graphql-code-generator/blob/master/packages/plugins/other/schema-ast/src/index.ts#L23)
136
143
  */
137
144
  schemaAstConfig?: SchemaASTConfig;
138
145
  /**
@@ -149,6 +156,8 @@ interface ModuleOptions {
149
156
  * }
150
157
  * }
151
158
  * ```
159
+ *
160
+ * @see [Types.UrlSchemaOptions](https://github.com/dotansimha/graphql-code-generator/blob/master/packages/utils/plugins-helpers/src/types.ts#L82)
152
161
  */
153
162
  urlSchemaOptions?: Types.UrlSchemaOptions;
154
163
  };
@@ -171,8 +180,351 @@ interface ModuleOptions {
171
180
  };
172
181
  }
173
182
 
183
+ type RpcItem = {
184
+ id: string;
185
+ timestamp?: number;
186
+ source: string;
187
+ name: string;
188
+ identifier: 'fragment' | 'query' | 'mutation';
189
+ filePath: string;
190
+ };
191
+
192
+ declare const defaultOptions: ModuleOptions;
193
+
194
+ declare class ConsolePrompt {
195
+ private abortController;
196
+ confirm(message: string): Promise<'yes' | 'no' | 'cancel'>;
197
+ abort(): void;
198
+ }
199
+
200
+ type TemplateOptions = {
201
+ path: string;
202
+ virtual?: boolean;
203
+ };
204
+ type GeneratorTemplateCallback = (output: GeneratorOutput, helper: ModuleHelper) => string;
205
+ type StaticTemplateCallback = (helper: ModuleHelper) => string;
206
+ type GeneratorTemplate = {
207
+ type: 'generator';
208
+ options: TemplateOptions;
209
+ build: GeneratorTemplateCallback | null;
210
+ buildTypes: GeneratorTemplateCallback | null;
211
+ virtual?: boolean;
212
+ };
213
+ type StaticTemplate = {
214
+ type: 'static';
215
+ options: TemplateOptions;
216
+ build: StaticTemplateCallback | null;
217
+ buildTypes: StaticTemplateCallback | null;
218
+ virtual?: boolean;
219
+ };
220
+
221
+ type WithRequired<T, K extends keyof T> = T & {
222
+ [P in K]-?: T[P];
223
+ };
224
+ type RequiredModuleOptions = WithRequired<ModuleOptions, keyof typeof defaultOptions>;
225
+ type ModuleHelperResolvers = {
226
+ /**
227
+ * Resolver for paths relative to the module root.
228
+ */
229
+ module: Resolver;
230
+ /**
231
+ * Resolve relative to the app's server directory.
232
+ */
233
+ server: Resolver;
234
+ /**
235
+ * Resolve relative to the Nuxt src folder.
236
+ */
237
+ src: Resolver;
238
+ /**
239
+ * Resolve relative to the Nuxt app directory.
240
+ */
241
+ app: Resolver;
242
+ /**
243
+ * Resolve relative to the Nuxt root.
244
+ *
245
+ * Should be where nuxt.config.ts is located.
246
+ */
247
+ root: Resolver;
248
+ };
249
+ type ModuleHelperPaths = {
250
+ runtimeTypes: string;
251
+ root: string;
252
+ nuxtConfig: string;
253
+ serverDir: string;
254
+ schema: string;
255
+ serverOptions: string | null;
256
+ clientOptions: string | null;
257
+ moduleBuildDir: string;
258
+ moduleTypesDir: string;
259
+ };
260
+ declare class ModuleHelper {
261
+ private nuxt;
262
+ readonly resolvers: ModuleHelperResolvers;
263
+ readonly paths: ModuleHelperPaths;
264
+ readonly isDev: boolean;
265
+ readonly options: RequiredModuleOptions;
266
+ readonly prompt: ConsolePrompt;
267
+ private nitroExternals;
268
+ private tsPaths;
269
+ constructor(nuxt: Nuxt, moduleUrl: string, options: ModuleOptions);
270
+ /**
271
+ * Find the path to the graphqlMiddleware.serverOptions.ts file.
272
+ */
273
+ private findServerOptions;
274
+ private findClientOptions;
275
+ /**
276
+ * Transform the path relative to the module's build directory.
277
+ *
278
+ * @param path - The absolute path.
279
+ *
280
+ * @returns The path relative to the module's build directory.
281
+ */
282
+ toModuleBuildRelative(path: string): string;
283
+ /**
284
+ * Transform the path relative to the Nuxt build directory.
285
+ *
286
+ * @param path - The absolute path.
287
+ *
288
+ * @returns The path relative to the module's build directory.
289
+ */
290
+ toBuildRelative(path: string): string;
291
+ /**
292
+ * Get all file paths that match the import patterns.
293
+ */
294
+ getImportPatternFiles(): Promise<string[]>;
295
+ matchesImportPattern(filePath: string): boolean;
296
+ addAlias(name: string, path: string): void;
297
+ inlineNitroExternals(arg: ResolvedNuxtTemplate | string): void;
298
+ transpile(path: string): void;
299
+ applyBuildConfig(): void;
300
+ processTemplate(path: string, content: string): string;
301
+ addTemplate(template: StaticTemplate): void;
302
+ addPlugin(name: string): void;
303
+ addServerHandler(name: string, path: string, method: RouterMethod): void;
304
+ addComposable(name: string): void;
305
+ addServerUtil(name: string): void;
306
+ }
307
+
308
+ type CollectorWatchEventResult = {
309
+ hasChanged: boolean;
310
+ affectedOperations: string[];
311
+ error?: {
312
+ message: string;
313
+ };
314
+ };
315
+ declare class Collector {
316
+ private schema;
317
+ private helper;
318
+ /**
319
+ * All collected files.
320
+ */
321
+ private files;
322
+ /**
323
+ * All documents provided by hooks.
324
+ */
325
+ private hookDocuments;
326
+ /**
327
+ * All file paths provided by hooks.
328
+ */
329
+ private hookFiles;
330
+ /**
331
+ * The code generator.
332
+ */
333
+ private generator;
334
+ /**
335
+ * A map of operation name and timestamp when the operation was last validated.
336
+ */
337
+ private operationTimestamps;
338
+ /**
339
+ * The generated operations and fragments.
340
+ */
341
+ readonly rpcItems: Map<string, RpcItem>;
342
+ /**
343
+ * The registered templates.
344
+ */
345
+ private templates;
346
+ /**
347
+ * The generated template contents.
348
+ */
349
+ private templateResult;
350
+ constructor(schema: GraphQLSchema, helper: ModuleHelper);
351
+ reset(): Promise<void>;
352
+ updateSchema(schema: GraphQLSchema): Promise<void>;
353
+ private filePathToBuildRelative;
354
+ private filePathToSourceRelative;
355
+ private operationToLogEntry;
356
+ private getTemplate;
357
+ /**
358
+ * Executes code gen and performs validation for operations.
359
+ */
360
+ private buildState;
361
+ private buildErrorMessage;
362
+ private logError;
363
+ /**
364
+ * Initialise the collector.
365
+ *
366
+ * In dev mode, the method will call itself recursively until all documents
367
+ * are valid.
368
+ *
369
+ * If not in dev mode the method will throw an error when documents are not
370
+ * valid.
371
+ */
372
+ init(): Promise<void>;
373
+ addHookDocument(identifier: string, source: string): void;
374
+ addHookFile(filePath: string): void;
375
+ /**
376
+ * Initialise the collector.
377
+ */
378
+ private initDocuments;
379
+ /**
380
+ * Add a file.
381
+ */
382
+ private addFile;
383
+ private matchesPatternOrExists;
384
+ private handleAdd;
385
+ private handleChange;
386
+ private handleUnlink;
387
+ private handleUnlinkDir;
388
+ /**
389
+ * Handle the watcher event for the given file path.
390
+ */
391
+ handleWatchEvent(event: WatchEvent, filePath: string): Promise<CollectorWatchEventResult>;
392
+ /**
393
+ * Adds a virtual template (not written to disk) for both Nuxt and Nitro.
394
+ *
395
+ * For some reason a template written to disk works for both Nuxt and Nitro,
396
+ * but a virtual template requires adding two templates.
397
+ */
398
+ private addVirtualTemplate;
399
+ /**
400
+ * Adds a template that dependes on Collector state.
401
+ */
402
+ addTemplate(template: GeneratorTemplate): void;
403
+ }
404
+
405
+ /**
406
+ * Handles downloading, loading and saving the GraphQL schema.
407
+ */
408
+ declare class SchemaProvider {
409
+ private helper;
410
+ /**
411
+ * The raw schema content.
412
+ */
413
+ private schemaContent;
414
+ /**
415
+ * The parsed schema object.
416
+ */
417
+ private schema;
418
+ constructor(helper: ModuleHelper);
419
+ init(): Promise<void>;
420
+ private loadFromDiskFallback;
421
+ /**
422
+ * Loads the schema from disk.
423
+ *
424
+ * @returns The schema contents from disk.
425
+ */
426
+ private loadSchemaFromDisk;
427
+ /**
428
+ * Downloads the schema and saves it to disk.
429
+ *
430
+ * @returns The schema contents.
431
+ */
432
+ private downloadSchema;
433
+ /**
434
+ * Determine if the schema exists on disk.
435
+ *
436
+ * @returns True if the schema file exists on disk.
437
+ */
438
+ hasSchemaOnDisk(): Promise<boolean>;
439
+ /**
440
+ * Load the schema either from disk or by downloading it.
441
+ *
442
+ * @param forceDownload - Forces downloading the schema.
443
+ */
444
+ loadSchema(opts?: {
445
+ forceDownload?: boolean;
446
+ forceDisk?: boolean;
447
+ }): Promise<void>;
448
+ /**
449
+ * Get the schema.
450
+ *
451
+ * @returns The parsed GraphQL schema object.
452
+ */
453
+ getSchema(): GraphQLSchema;
454
+ }
455
+
456
+ /**
457
+ * The public module context class.
458
+ */
459
+ declare class ModuleContext {
460
+ private schemaProvider;
461
+ private collector;
462
+ constructor(schemaProvider: SchemaProvider, collector: Collector);
463
+ /**
464
+ * Return the GraphQL schema.
465
+ *
466
+ * Note that the schema may be updated during development, so it can become
467
+ * stale. Prefer using methods like `schemaHasType()` to query the schema.
468
+ *
469
+ * @returns The GraphQL schema.
470
+ */
471
+ getSchema(): GraphQLSchema;
472
+ /**
473
+ * Check if the given GraphQL type (interface, concrete type, enum, input type)
474
+ * exists in the schema.
475
+ *
476
+ * @param name - The name of the type.
477
+ *
478
+ * @returns True if the type exists in the schema.
479
+ */
480
+ schemaHasType(name: string): boolean;
481
+ /**
482
+ * Get a type from the schema.
483
+ *
484
+ * @param name - The name of the type.
485
+ *
486
+ * @returns The type.
487
+ */
488
+ schemaGetType(name: string): GraphQLNamedType | undefined;
489
+ /**
490
+ * Add an additional static document.
491
+ *
492
+ * @param identifier - The unique identifier for your document.
493
+ * @param source - The document source.
494
+ */
495
+ addDocument(identifier: string, source: string): ModuleContext;
496
+ /**
497
+ * Add an additional GraphQL file to import.
498
+ *
499
+ * @param filePath - The absolute path to the file.
500
+ */
501
+ addImportFile(filePath: string): ModuleContext;
502
+ }
503
+
504
+ declare function useGraphqlModuleContext(): ModuleContext;
505
+ declare function useGraphqlModuleContext(options: {
506
+ nullOnMissing: true;
507
+ }): ModuleContext | null;
174
508
  declare const _default: _nuxt_schema.NuxtModule<ModuleOptions, ModuleOptions, false>;
175
509
 
510
+ declare module '#app' {
511
+ interface RuntimeNuxtHooks {
512
+ /**
513
+ * Emitted when any GraphQL response contains errors.
514
+ */
515
+ 'nuxt-graphql-middleware:errors': (errors: OperationResponseError) => HookResult;
516
+ }
517
+ }
518
+ declare module 'vite/types/customEvent.d.ts' {
519
+ interface CustomEventMap {
520
+ /**
521
+ * Emitted when GraphQL operations have been updated.
522
+ */
523
+ 'nuxt-graphql-middleware:reload': {
524
+ operations: string[];
525
+ };
526
+ }
527
+ }
176
528
  declare module '@nuxt/schema' {
177
529
  interface AppConfig {
178
530
  graphqlMiddleware: {
@@ -186,25 +538,38 @@ declare module '@nuxt/schema' {
186
538
  clientCacheMaxSize: number;
187
539
  };
188
540
  }
189
- }
190
- declare module '#app' {
191
- interface RuntimeNuxtHooks {
541
+ interface Nuxt {
192
542
  /**
193
- * Emitted when any GraphQL response contains errors.
543
+ * The nuxt-graphql-middleware module context.
194
544
  */
195
- 'nuxt-graphql-middleware:errors': (errors: OperationResponseError) => HookResult;
545
+ _nuxt_graphql_middleware?: ModuleContext;
196
546
  }
197
- }
198
- declare module 'vite/types/customEvent.d.ts' {
199
- interface CustomEventMap {
547
+ interface NuxtHooks {
200
548
  /**
201
- * Emitted when GraphQL operations have been updated.
549
+ * Called once right before the documents are initialised.
550
+ *
551
+ * Use this hook to add any additional documents based on for example the parsed schema.
552
+ *
553
+ * @example
554
+ *
555
+ * ```typescript`
556
+ * export default defineNuxtConfig({
557
+ * hooks: {
558
+ * 'nuxt-graphql-middleware:init': (ctx) => {
559
+ * if (ctx.schemaHasType('Comment')) {
560
+ * ctx.addDocument(
561
+ * 'queryFromHook',
562
+ * `query loadComments { author subject date body }`
563
+ * )
564
+ * }
565
+ * },
566
+ * },
567
+ * })
568
+ * ```
202
569
  */
203
- 'nuxt-graphql-middleware:reload': {
204
- operations: string[];
205
- };
570
+ 'nuxt-graphql-middleware:init': (ctx: ModuleContext) => void | Promise<void>;
206
571
  }
207
572
  }
208
573
 
209
- export { _default as default };
574
+ export { _default as default, useGraphqlModuleContext };
210
575
  export type { ModuleOptions };