@lewebsimple/nuxt-graphql 0.5.12 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/dist/module.d.mts +5 -38
  2. package/dist/module.json +2 -2
  3. package/dist/module.mjs +111 -166
  4. package/dist/runtime/app/composables/useAsyncGraphQLQuery.d.ts +7 -7
  5. package/dist/runtime/app/composables/useAsyncGraphQLQuery.js +12 -8
  6. package/dist/runtime/app/composables/useGraphQLCache.client.d.ts +9 -1
  7. package/dist/runtime/app/composables/useGraphQLCache.client.js +1 -1
  8. package/dist/runtime/app/composables/useGraphQLMutation.d.ts +3 -12
  9. package/dist/runtime/app/composables/useGraphQLMutation.js +11 -10
  10. package/dist/runtime/app/composables/useGraphQLQuery.d.ts +2 -11
  11. package/dist/runtime/app/composables/useGraphQLQuery.js +11 -21
  12. package/dist/runtime/app/composables/useGraphQLSubscription.client.d.ts +1 -1
  13. package/dist/runtime/app/composables/useGraphQLSubscription.client.js +1 -1
  14. package/dist/runtime/{shared → app}/lib/cache.d.ts +9 -3
  15. package/dist/runtime/{shared → app}/lib/cache.js +3 -3
  16. package/dist/runtime/app/plugins/execute-graphql.d.ts +25 -0
  17. package/dist/runtime/app/plugins/execute-graphql.js +25 -0
  18. package/dist/runtime/app/plugins/graphql-sse.client.d.ts +12 -2
  19. package/dist/runtime/server/api/graphql.d.ts +0 -6
  20. package/dist/runtime/server/api/graphql.js +4 -5
  21. package/dist/runtime/server/lib/execute-graphql-schema.d.ts +3 -0
  22. package/dist/runtime/server/lib/execute-graphql-schema.js +22 -0
  23. package/dist/runtime/server/lib/remote-executor.d.ts +17 -14
  24. package/dist/runtime/server/lib/remote-executor.js +17 -18
  25. package/dist/runtime/server/lib/yoga.d.ts +1 -2
  26. package/dist/runtime/server/lib/yoga.js +2 -4
  27. package/dist/runtime/server/tsconfig.json +2 -2
  28. package/dist/runtime/server/utils/defineRemoteExecutorHooks.d.ts +1 -6
  29. package/dist/runtime/server/utils/useGraphQLOperation.d.ts +16 -0
  30. package/dist/runtime/server/utils/useGraphQLOperation.js +12 -0
  31. package/dist/runtime/shared/lib/error.d.ts +7 -21
  32. package/dist/runtime/shared/lib/error.js +1 -18
  33. package/dist/runtime/shared/lib/headers.d.ts +1 -6
  34. package/dist/runtime/shared/lib/headers.js +1 -1
  35. package/dist/runtime/shared/lib/types.d.ts +16 -0
  36. package/dist/runtime/shared/utils/execute-graphql-http.d.ts +7 -0
  37. package/dist/runtime/shared/utils/execute-graphql-http.js +31 -0
  38. package/package.json +4 -7
  39. package/dist/runtime/app/lib/execute-http.d.ts +0 -14
  40. package/dist/runtime/app/lib/execute-http.js +0 -8
  41. package/dist/runtime/app/plugins/graphql-request.d.ts +0 -12
  42. package/dist/runtime/app/plugins/graphql-request.js +0 -20
  43. package/dist/runtime/server/lib/default-schema.d.ts +0 -4
  44. package/dist/runtime/server/lib/default-schema.js +0 -16
  45. package/dist/runtime/server/lib/execute-schema.d.ts +0 -11
  46. package/dist/runtime/server/lib/execute-schema.js +0 -23
  47. package/dist/runtime/server/utils/useServerGraphQLMutation.d.ts +0 -17
  48. package/dist/runtime/server/utils/useServerGraphQLMutation.js +0 -14
  49. package/dist/runtime/server/utils/useServerGraphQLQuery.d.ts +0 -17
  50. package/dist/runtime/server/utils/useServerGraphQLQuery.js +0 -14
  51. package/dist/runtime/shared/lib/utils.d.ts +0 -1
  52. /package/dist/runtime/shared/lib/{utils.js → types.js} +0 -0
package/dist/module.d.mts CHANGED
@@ -1,5 +1,6 @@
1
1
  import * as _nuxt_schema from '@nuxt/schema';
2
2
  import { HeadersInput } from '../dist/runtime/shared/lib/headers.js';
3
+ import { CacheConfig } from '../dist/runtime/app/lib/cache.js';
3
4
 
4
5
  type LocalSchemaDef = {
5
6
  type: "local";
@@ -9,56 +10,22 @@ type RemoteSchemaDef = {
9
10
  type: "remote";
10
11
  endpoint: string;
11
12
  headers?: HeadersInput;
13
+ hooks?: string[];
12
14
  };
13
15
  type SchemaDef = LocalSchemaDef | RemoteSchemaDef;
14
16
 
15
17
  interface NuxtGraphQLModuleOptions {
16
- /**
17
- * Client-side GraphQL configuration (HTTP + cache).
18
- */
19
18
  client?: {
20
- /**
21
- * Global cache configuration for queries.
22
- */
23
- cache?: Partial<GraphQLCacheConfig>;
24
- /**
25
- * GraphQL documents glob pattern.
26
- * Default: "**\/*.gql"
27
- */
28
19
  documents?: string;
29
- /**
30
- * Headers forwarded from the SSR request to graphql-request.
31
- * Default: ["authorization", "cookie"]
32
- */
20
+ cache?: Partial<CacheConfig>;
33
21
  ssrForwardHeaders?: string[];
34
22
  };
35
- /**
36
- * Where to write graphql.config.json.
37
- * Resolved from rootDir.
38
- * Default: ./graphql.config.json
39
- */
40
- saveConfig?: string;
41
- /**
42
- * Where to write the stitched GraphQL SDL.
43
- * Resolved from rootDir.
44
- * Default: server/graphql/schema.graphql
45
- */
46
- saveSDL?: string;
47
- /**
48
- * Server-side GraphQL configuration (Yoga server + execution).
49
- */
50
23
  server?: {
51
- /**
52
- * Paths to GraphQL server context factories relative to rootDir.
53
- * export default defineGraphQLContext((event: H3Event) => Promise<Record<string, unknown>>)
54
- */
55
24
  context?: string[];
56
- /**
57
- * GraphQL schema definition.
58
- * Key = schemaName.
59
- */
60
25
  schema?: Record<string, SchemaDef>;
61
26
  };
27
+ saveConfig?: string;
28
+ saveSDL?: string;
62
29
  }
63
30
  declare const _default: _nuxt_schema.NuxtModule<NuxtGraphQLModuleOptions, NuxtGraphQLModuleOptions, false>;
64
31
 
package/dist/module.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
- "name": "nuxt-graphql",
2
+ "name": "@lewebsimple/nuxt-graphql",
3
3
  "configKey": "graphql",
4
- "version": "0.5.12",
4
+ "version": "0.6.0",
5
5
  "builder": {
6
6
  "@nuxt/module-builder": "1.0.2",
7
7
  "unbuild": "3.6.1"
package/dist/module.mjs CHANGED
@@ -2,16 +2,17 @@ import { mkdirSync, writeFileSync } from 'node:fs';
2
2
  import { relative, resolve, dirname } from 'node:path';
3
3
  import { defu } from 'defu';
4
4
  import { stitchSchemas } from '@graphql-tools/stitch';
5
- import { defineNuxtModule, useLogger, createResolver, addTemplate, addServerTemplate, addTypeTemplate, addServerHandler, addPlugin, addImportsDir, addServerImportsDir } from '@nuxt/kit';
5
+ import { addTemplate, addServerTemplate, defineNuxtModule, useLogger, createResolver, addServerHandler, addPlugin, addImportsDir, addServerImportsDir } from '@nuxt/kit';
6
6
  import { hash } from 'ohash';
7
+ import { createRequire } from 'node:module';
7
8
  import { printSchema, lexicographicSortSchema, buildSchema, getIntrospectionQuery, buildClientSchema, GraphQLSchema, Kind } from 'graphql';
8
- import { resolveCacheConfig } from '../dist/runtime/shared/lib/cache.js';
9
9
  import { loadDocuments } from '@graphql-tools/load';
10
10
  import { GraphQLFileLoader } from '@graphql-tools/graphql-file-loader';
11
11
  import { codegen } from '@graphql-codegen/core';
12
12
  import * as typescriptPlugin from '@graphql-codegen/typescript';
13
13
  import * as typescriptOperationsPlugin from '@graphql-codegen/typescript-operations';
14
14
  import * as typedDocumentNodePlugin from '@graphql-codegen/typed-document-node';
15
+ import { resolveCacheConfig } from '../dist/runtime/app/lib/cache.js';
15
16
 
16
17
  const buildCache = /* @__PURE__ */ new Map();
17
18
  function getCachedLoader(baseKey, loader) {
@@ -43,21 +44,45 @@ function clearBuildCache(baseKey) {
43
44
  const cyan = "\x1B[36m";
44
45
  const reset = "\x1B[0m";
45
46
 
47
+ const require$1 = createRequire(import.meta.url);
48
+ function splitModule(ts) {
49
+ const tsCompiler = require$1("typescript");
50
+ const fileName = "module.ts";
51
+ const outputs = {};
52
+ const compilerOptions = {
53
+ target: tsCompiler.ScriptTarget.ES2020,
54
+ module: tsCompiler.ModuleKind.ESNext,
55
+ moduleResolution: tsCompiler.ModuleResolutionKind.NodeNext,
56
+ declaration: true,
57
+ importsNotUsedAsValues: tsCompiler.ImportsNotUsedAsValues.Remove,
58
+ preserveValueImports: false
59
+ };
60
+ const host = tsCompiler.createCompilerHost(compilerOptions, true);
61
+ host.getSourceFile = (name) => name === fileName ? tsCompiler.createSourceFile(fileName, ts, tsCompiler.ScriptTarget.ESNext, true) : void 0;
62
+ host.readFile = () => ts;
63
+ host.fileExists = (f) => f === fileName;
64
+ host.writeFile = (name, content) => {
65
+ outputs[name] = content;
66
+ };
67
+ const program = tsCompiler.createProgram([fileName], compilerOptions, host);
68
+ program.emit();
69
+ return {
70
+ mjs: Object.entries(outputs).find(([n]) => n.endsWith(".js"))?.[1]?.trim() ?? "",
71
+ dts: Object.entries(outputs).find(([n]) => n.endsWith(".d.ts"))?.[1]?.trim() ?? ""
72
+ };
73
+ }
74
+
46
75
  function getContextTemplate({ importPaths }) {
47
76
  const contextImports = importPaths.map((importPath, index) => `import context${index} from ${JSON.stringify(importPath)};`);
48
77
  const contextTypes = ["{}", ...importPaths.map((_, index) => `Awaited<ReturnType<typeof context${index}>>`)];
49
- const contextTsArray = ["(event: H3Event) => ({})", ...importPaths.map((_, index) => `context${index}`)];
50
- const contextMjsArray = ["(event) => ({})", ...importPaths.map((_, index) => `context${index}`)];
51
- const types = `
78
+ const contextArray = ["(event: H3Event) => ({})", ...importPaths.map((_, index) => `context${index}`)];
79
+ const ts = `
52
80
  import type { H3Event } from "h3";
53
81
  ${contextImports.join("\n")}
54
82
 
55
83
  export type GraphQLContext = ${contextTypes.join(" & ")};
56
- `.trim();
57
- const ts = `
58
- ${types}
59
84
 
60
- const contextFactories = [${contextTsArray.join(", ")}];
85
+ const contextFactories = [${contextArray.join(", ")}];
61
86
 
62
87
  export async function createContext(event: H3Event): Promise<GraphQLContext> {
63
88
  return Object.assign(
@@ -65,24 +90,7 @@ export async function createContext(event: H3Event): Promise<GraphQLContext> {
65
90
  ...await Promise.all(contextFactories.map((factory) => factory(event)))
66
91
  )
67
92
  }`.trim();
68
- const mjs = `
69
- ${contextImports.join("\n")}
70
-
71
- const contextFactories = [${contextMjsArray.join(", ")}];
72
-
73
- export async function createContext(event) {
74
- return Object.assign(
75
- {},
76
- ...await Promise.all(contextFactories.map((factory) => factory(event)))
77
- )
78
- }
79
- `.trim();
80
- const dts = `
81
- ${types}
82
-
83
- export async function createContext(event: H3Event): Promise<GraphQLContext>;
84
- `.trim();
85
- return { ts, mjs, dts };
93
+ return { ts, ...splitModule(ts) };
86
94
  }
87
95
 
88
96
  function getRelativePath(from, to) {
@@ -110,12 +118,10 @@ ${localImports.join("\n")}
110
118
  ${remoteImports.join("\n")}
111
119
 
112
120
  export const schema = stitchSchemas({
113
- subschemas: [
114
- ${[mergedSchema, ...remoteSchemas].join(",\n ")}
115
- ],
121
+ subschemas: [${[mergedSchema, ...remoteSchemas].join(", ")}],
116
122
  });
117
123
  `.trim();
118
- return ts;
124
+ return { ts, ...splitModule(ts) };
119
125
  }
120
126
  async function loadLocalSchema({ importPath }) {
121
127
  const { createJiti } = await import('jiti');
@@ -126,23 +132,21 @@ async function loadLocalSchema({ importPath }) {
126
132
  }
127
133
  return module.schema;
128
134
  }
129
- function getSchemaSDL(schema) {
130
- return printSchema(lexicographicSortSchema(schema));
131
- }
132
- function getDefaultSchema() {
133
- return buildSchema(`type Query { _empty: String }`);
134
- }
135
-
136
- async function getRemoteSchemaTemplate({ endpoint, loadSchema }) {
135
+ async function getRemoteSchemaTemplate({ endpoint, headers, hooks, loadSchema }) {
136
+ const hooksImports = hooks.map((hook, index) => `import hook${index} from ${JSON.stringify(hook.importPath)};`);
137
+ const hooksArray = hooks.map((_, index) => `hook${index}`);
137
138
  const schema = await loadSchema();
138
139
  const sdl = getSchemaSDL(schema);
139
- return `
140
+ const ts = `
141
+ import type { GraphQLSchema } from "graphql";
140
142
  import { buildSchema } from "graphql";
141
- import { buildHTTPExecutor } from "@graphql-tools/executor-http";
143
+ import { getRemoteExecutor } from "#graphql/runtime/remote-executor";
144
+ ${hooksImports.join("\n")}
142
145
 
143
- const executor = buildHTTPExecutor({
146
+ const executor = getRemoteExecutor({
144
147
  endpoint: "${endpoint}",
145
- fetch: globalThis.fetch,
148
+ headers: ${JSON.stringify(headers)},
149
+ hooks: [${hooksArray.join(", ")}],
146
150
  });
147
151
 
148
152
  const sdl = \`${sdl.replace(/`/g, "\\`")}\`;
@@ -151,8 +155,9 @@ const sdl = \`${sdl.replace(/`/g, "\\`")}\`;
151
155
  export const schema = {
152
156
  schema: buildSchema(sdl),
153
157
  executor,
154
- }
158
+ } as unknown as GraphQLSchema;
155
159
  `.trim();
160
+ return { ts, ...splitModule(ts) };
156
161
  }
157
162
  async function introspectRemoteSchema({ endpoint }) {
158
163
  const response = await fetch(endpoint, {
@@ -179,65 +184,26 @@ function stripSubscriptions(schema) {
179
184
  directives: schema.getDirectives()
180
185
  });
181
186
  }
182
-
183
- function renderAppTypesTemplate() {
184
- return `
185
- import type { GraphQLClient } from "graphql-request";
186
- import type { Client as SSEClient } from "graphql-sse";
187
-
188
- declare module "#app/nuxt" {
189
- interface NuxtApp {
190
- $getGraphQLClient: () => GraphQLClient;
191
- $getGraphQLSSEClient: () => SSEClient;
192
- }
193
- }
194
-
195
- declare module "#app" {
196
- interface NuxtApp {
197
- $getGraphQLClient: () => GraphQLClient;
198
- $getGraphQLSSEClient: () => SSEClient;
199
- }
200
- }
201
-
202
- export {};
203
- `.trim();
204
- }
205
- function renderServerTypesTemplate() {
206
- return `
207
- declare module "h3" {
208
- interface H3EventContext {
209
- _graphqlInFlightRequestsMap?: Map<string, Promise<unknown>>;
210
- }
211
- }
212
-
213
- export {};
214
- `.trim();
187
+ function getSchemaSDL(schema) {
188
+ return printSchema(lexicographicSortSchema(schema));
215
189
  }
216
- function renderSharedTypesTemplate() {
217
- return `
218
- import type { DocumentNode } from "graphql";
219
-
220
- declare global {
221
- type GraphQLCacheConfig = {
222
- policy: "no-cache" | "cache-first" | "network-first" | "swr";
223
- ttl?: number;
224
- keyPrefix: string;
225
- keyVersion: string | number;
226
- };;
190
+ function getDefaultSchema() {
191
+ return buildSchema(`type Query { _empty: String }`);
227
192
  }
228
193
 
229
- declare module "nuxt/schema" {
230
- interface PublicRuntimeConfig {
231
- graphql: {
232
- cacheConfig: GraphQLCacheConfig;
233
- ssrForwardHeaders: string[];
234
- };
194
+ function addUniversalTemplate({ filename, getContents, emitTs }) {
195
+ let modulePath;
196
+ if (emitTs) {
197
+ modulePath = addTemplate({ filename: `${filename}.ts`, getContents: async () => (await getContents()).ts, write: true }).dst;
198
+ } else {
199
+ modulePath = addTemplate({ filename: `${filename}.mjs`, getContents: async () => (await getContents()).mjs, write: true }).dst;
200
+ addTemplate({ filename: `${filename}.d.ts`, getContents: async () => (await getContents()).dts });
235
201
  }
202
+ addServerTemplate({ filename: `${filename}.mjs`, getContents: async () => (await getContents()).mjs });
203
+ return modulePath;
236
204
  }
237
205
 
238
- export { };
239
- `.trim();
240
- }
206
+ const version = "0.6.0";
241
207
 
242
208
  async function getDocuments(documentsGlob) {
243
209
  try {
@@ -258,7 +224,7 @@ async function getDocuments(documentsGlob) {
258
224
  }
259
225
 
260
226
  async function getOperationsTemplate({ loadSchema, loadDocuments, documentGlob }) {
261
- const content = await codegen({
227
+ const ts = await codegen({
262
228
  filename: "operations.ts",
263
229
  schema: await loadSchema(),
264
230
  documents: await loadDocuments(documentGlob),
@@ -309,7 +275,7 @@ async function getOperationsTemplate({ loadSchema, loadDocuments, documentGlob }
309
275
  },
310
276
  config: {}
311
277
  });
312
- return content;
278
+ return { ts, ...splitModule(ts) };
313
279
  }
314
280
 
315
281
  async function getRegistryTemplate({ loadDocuments, documentGlob }) {
@@ -351,7 +317,7 @@ export type ResultOf<TName extends keyof OperationRegistry> = OperationRegistry[
351
317
  export const registry: { [K in keyof OperationRegistry]: { kind: OperationRegistry[K]["kind"]; document: DocumentNode; }; } = {
352
318
  ${operations.map(({ name, kind }) => `${name}: { kind: "${kind}", document: ${name}Document },`).join("\n ")}
353
319
  };`.trim();
354
- return ts;
320
+ return { ts, ...splitModule(ts) };
355
321
  }
356
322
  function collectOperations(documents) {
357
323
  const operations = /* @__PURE__ */ new Map();
@@ -373,17 +339,15 @@ function collectOperations(documents) {
373
339
  return Array.from(operations.values());
374
340
  }
375
341
 
376
- const version = "0.5.12";
377
-
378
342
  const module$1 = defineNuxtModule({
379
343
  meta: {
380
- name: "nuxt-graphql",
344
+ name: "@lewebsimple/nuxt-graphql",
381
345
  configKey: "graphql"
382
346
  },
383
347
  defaults: {},
384
348
  async setup(options, nuxt) {
385
349
  const logger = useLogger("@lewebsimple/nuxt-graphql");
386
- logger.info(`Initializing @lewebsimple/nuxt-graphql v${version}`);
350
+ logger.info(`@lewebsimple/nuxt-graphql v${version} loaded`);
387
351
  const { resolve: resolveModule } = createResolver(import.meta.url);
388
352
  const { rootDir } = nuxt.options;
389
353
  const { resolve: resolveRoot, resolvePath: rawResolveRootPath } = createResolver(rootDir);
@@ -392,22 +356,15 @@ const module$1 = defineNuxtModule({
392
356
  }
393
357
  const nuxtAliases = {};
394
358
  const nitroAliases = {};
359
+ const emitTs = Boolean(nuxt.options.dev) || Boolean(process.env.NUXT_MODULE_PREPARE);
360
+ nuxtAliases["#graphql/runtime/remote-executor"] = resolveModule("./runtime/server/lib/remote-executor");
361
+ nitroAliases["#graphql/runtime/remote-executor"] = resolveModule("./runtime/server/lib/remote-executor");
395
362
  const contextInput = {
396
363
  importPaths: await Promise.all((options.server?.context || []).map((path) => resolveRootPath(path)))
397
364
  };
398
- let contextDst;
399
- if (nuxt.options.dev || process.env.NUXT_MODULE_PREPARE) {
400
- logger.info("Development mode detected: using TypeScript GraphQL context template.");
401
- contextDst = addTemplate({ filename: "graphql/context.ts", getContents: () => getContextTemplate(contextInput).ts, write: true }).dst;
402
- addServerTemplate({ filename: "graphql/context.mjs", getContents: () => getContextTemplate(contextInput).mjs });
403
- } else {
404
- logger.info("Production mode detected: using MJS GraphQL context template.");
405
- contextDst = addTemplate({ filename: "graphql/context.mjs", getContents: () => getContextTemplate(contextInput).mjs, write: true }).dst;
406
- addServerTemplate({ filename: "graphql/context.mjs", getContents: () => getContextTemplate(contextInput).mjs });
407
- addTypeTemplate({ filename: "graphql/context.d.ts", getContents: () => getContextTemplate(contextInput).dts });
408
- }
409
- nuxtAliases["#graphql/context"] = contextDst;
410
- nitroAliases["#graphql/context"] = contextDst;
365
+ const contextPath = addUniversalTemplate({ filename: "graphql/context", getContents: () => getContextTemplate(contextInput), emitTs });
366
+ nuxtAliases["#graphql/context"] = contextPath;
367
+ nitroAliases["#graphql/context"] = contextPath;
411
368
  const schemaInput = { local: {}, remote: {} };
412
369
  const schemaLoaders = {};
413
370
  for (const [schemaName, schemaDef] of Object.entries(options.server?.schema || {})) {
@@ -416,27 +373,24 @@ const module$1 = defineNuxtModule({
416
373
  }
417
374
  if (schemaDef.type === "local") {
418
375
  const importPath = await resolveRootPath(schemaDef.path);
376
+ const loadSchema2 = getCachedLoader(`schema:local:${schemaName}`, async () => await loadLocalSchema({ importPath }));
419
377
  schemaInput.local[schemaName] = { importPath };
420
- schemaLoaders[schemaName] = getCachedLoader(`schema:local:${schemaName}`, async () => await loadLocalSchema({ importPath }));
378
+ schemaLoaders[schemaName] = loadSchema2;
421
379
  } else if (schemaDef.type === "remote") {
422
380
  const { endpoint } = schemaDef;
423
- const remoteSchemaInput = {
424
- endpoint,
425
- loadSchema: getCachedLoader(`schema:remote:${schemaName}`, async () => await introspectRemoteSchema({ endpoint }))
426
- };
427
- const filename = `graphql/schemas/${schemaName}.ts`;
428
- addTemplate({ filename, getContents: async () => await getRemoteSchemaTemplate(remoteSchemaInput), write: true });
429
- addServerTemplate({ filename, getContents: async () => await getRemoteSchemaTemplate(remoteSchemaInput) });
381
+ const loadSchema2 = getCachedLoader(`schema:remote:${schemaName}`, async () => await introspectRemoteSchema({ endpoint }));
382
+ const hooks = await Promise.all((schemaDef.hooks || []).map(async (hookPath) => ({ importPath: await resolveRootPath(hookPath) })));
383
+ const remoteSchemaInput = { endpoint, headers: schemaDef.headers || {}, hooks, loadSchema: loadSchema2 };
384
+ addUniversalTemplate({ filename: `graphql/schemas/${schemaName}`, getContents: () => getRemoteSchemaTemplate(remoteSchemaInput), emitTs });
430
385
  schemaInput.remote[schemaName] = { importPath: `./schemas/${schemaName}` };
431
- schemaLoaders[schemaName] = remoteSchemaInput.loadSchema;
386
+ schemaLoaders[schemaName] = loadSchema2;
432
387
  } else {
433
388
  throw new Error(`Unknown schema type for schema "${schemaName}"`);
434
389
  }
435
390
  }
436
- const schemaDst = addTemplate({ filename: `graphql/schema.ts`, getContents: () => getSchemaTemplate(schemaInput), write: true }).dst;
437
- addServerTemplate({ filename: `graphql/schema.ts`, getContents: () => getSchemaTemplate(schemaInput) });
438
- nuxtAliases["#graphql/schema"] = schemaDst;
439
- nitroAliases["#graphql/schema"] = schemaDst;
391
+ const schemaPath = addUniversalTemplate({ filename: "graphql/schema", getContents: () => getSchemaTemplate(schemaInput), emitTs });
392
+ nuxtAliases["#graphql/schema"] = schemaPath;
393
+ nitroAliases["#graphql/schema"] = schemaPath;
440
394
  const sdlPath = resolveRoot(options.saveSDL || "server/graphql/schema.graphql");
441
395
  const loadSchema = getCachedLoader("schema:stitched", async () => {
442
396
  const subschemas = await Promise.all(Object.values(schemaLoaders).map((loadSchema2) => loadSchema2()));
@@ -444,13 +398,13 @@ const module$1 = defineNuxtModule({
444
398
  logger.warn(`No GraphQL schemas defined: using default empty schema.`);
445
399
  subschemas.push(getDefaultSchema());
446
400
  }
447
- const schema = stitchSchemas({
448
- subschemas
449
- });
450
- const sdl = getSchemaSDL(schema);
451
- mkdirSync(dirname(sdlPath), { recursive: true });
452
- writeFileSync(sdlPath, sdl, { encoding: "utf-8" });
453
- logger.info(`Stitched GraphQL SDL saved to: ${cyan}${getRelativePath(rootDir, sdlPath)}${reset}`);
401
+ const schema = stitchSchemas({ subschemas });
402
+ if (nuxt.options.dev) {
403
+ const sdl = getSchemaSDL(schema);
404
+ mkdirSync(dirname(sdlPath), { recursive: true });
405
+ writeFileSync(sdlPath, sdl, { encoding: "utf-8" });
406
+ logger.info(`Stitched GraphQL SDL saved to: ${cyan}${getRelativePath(rootDir, sdlPath)}${reset}`);
407
+ }
454
408
  return schema;
455
409
  });
456
410
  const loadDocuments = getCachedLoader("documents", async (documentsGlob) => {
@@ -460,38 +414,28 @@ const module$1 = defineNuxtModule({
460
414
  }
461
415
  return documents;
462
416
  });
463
- const operationsInput = {
464
- loadSchema,
465
- loadDocuments,
466
- documentGlob: options.client?.documents || "**/*.gql"
467
- };
468
- const operationsDst = addTemplate({ filename: "graphql/operations.ts", getContents: async () => await getOperationsTemplate(operationsInput), write: true }).dst;
469
- addServerTemplate({ filename: "graphql/operations.ts", getContents: async () => await getOperationsTemplate(operationsInput) });
470
- nuxtAliases["#graphql/operations"] = operationsDst;
471
- nitroAliases["#graphql/operations"] = operationsDst;
472
- const registryInput = {
473
- loadDocuments,
474
- documentGlob: options.client?.documents || "**/*.gql"
475
- };
476
- const registryDst = addTemplate({ filename: "graphql/registry.ts", getContents: async () => await getRegistryTemplate(registryInput), write: true }).dst;
477
- addServerTemplate({ filename: "graphql/registry.ts", getContents: async () => await getRegistryTemplate(registryInput) });
478
- nuxtAliases["#graphql/registry"] = registryDst;
479
- nitroAliases["#graphql/registry"] = registryDst;
480
- const configPath = resolveRoot(options.saveConfig || "graphql.config.json");
481
- const config = {
482
- schema: getRelativePath(rootDir, sdlPath),
483
- documents: options.client?.documents || "**/*.gql"
484
- };
485
- mkdirSync(dirname(configPath), { recursive: true });
486
- writeFileSync(configPath, JSON.stringify(config, null, 2), { encoding: "utf-8" });
487
- logger.info(`GraphQL config saved to: ${cyan}${getRelativePath(rootDir, configPath)}${reset}`);
417
+ const operationsInput = { loadSchema, loadDocuments, documentGlob: options.client?.documents || "**/*.gql" };
418
+ const operationsPath = addUniversalTemplate({ filename: "graphql/operations", getContents: () => getOperationsTemplate(operationsInput), emitTs });
419
+ nuxtAliases["#graphql/operations"] = operationsPath;
420
+ nitroAliases["#graphql/operations"] = operationsPath;
421
+ const registryInput = { loadDocuments, documentGlob: options.client?.documents || "**/*.gql" };
422
+ const registryPath = addUniversalTemplate({ filename: "graphql/registry", getContents: () => getRegistryTemplate(registryInput), emitTs });
423
+ nuxtAliases["#graphql/registry"] = registryPath;
424
+ nitroAliases["#graphql/registry"] = registryPath;
425
+ if (nuxt.options.dev) {
426
+ const configPath = resolveRoot(options.saveConfig || "graphql.config.json");
427
+ const config = {
428
+ schema: getRelativePath(rootDir, sdlPath),
429
+ documents: options.client?.documents || "**/*.gql"
430
+ };
431
+ mkdirSync(dirname(configPath), { recursive: true });
432
+ writeFileSync(configPath, JSON.stringify(config, null, 2), { encoding: "utf-8" });
433
+ logger.info(`GraphQL config saved to: ${cyan}${getRelativePath(rootDir, configPath)}${reset}`);
434
+ }
488
435
  nuxt.options.alias = defu(nuxt.options.alias, nuxtAliases);
489
436
  nuxt.hook("nitro:config", (nitroConfig) => {
490
437
  nitroConfig.alias = defu(nitroConfig.alias, nitroAliases);
491
438
  });
492
- addTypeTemplate({ filename: "types/nuxt-graphql.app.d.ts", getContents: () => renderAppTypesTemplate() }, { nuxt: true });
493
- addTypeTemplate({ filename: "types/nuxt-graphql.server.d.ts", getContents: () => renderServerTypesTemplate() }, { nitro: true, node: true });
494
- addTypeTemplate({ filename: "types/nuxt-graphql.shared.d.ts", getContents: () => renderSharedTypesTemplate() }, { nuxt: true, nitro: true, node: true });
495
439
  nuxt.options.runtimeConfig.public.graphql = defu(nuxt.options.runtimeConfig.public.graphql, {
496
440
  cacheConfig: resolveCacheConfig(options.client?.cache),
497
441
  ssrForwardHeaders: options.client?.ssrForwardHeaders || ["authorization", "cookie"]
@@ -509,9 +453,10 @@ const module$1 = defineNuxtModule({
509
453
  nuxt.hook("listen", (_, { url }) => {
510
454
  logger.success(`GraphQL Yoga ready: ${cyan}${url.replace(/\/$/, "")}/api/graphql${reset}`);
511
455
  });
512
- addPlugin(resolveModule("./runtime/app/plugins/graphql-request"));
456
+ addPlugin(resolveModule("./runtime/app/plugins/execute-graphql"));
513
457
  addPlugin(resolveModule("./runtime/app/plugins/graphql-sse.client"));
514
458
  addImportsDir(resolveModule("./runtime/app/composables"));
459
+ addImportsDir(resolveModule("./runtime/shared/utils"));
515
460
  addServerImportsDir(resolveModule("./runtime/server/utils"));
516
461
  }
517
462
  });
@@ -1,11 +1,11 @@
1
- import { useAsyncData, type AsyncDataOptions } from "#app";
1
+ import { type AsyncData, type AsyncDataOptions } from "#app";
2
2
  import type { QueryName, ResultOf, VariablesOf } from "#graphql/registry";
3
3
  import { type MaybeRefOrGetter } from "#imports";
4
- import { type ExecuteGraphQLHTTPOptions } from "../lib/execute-http.js";
5
- import type { IsEmptyObject } from "../../shared/lib/utils.js";
6
- type UseAsyncGraphQLQueryOptions<TName extends QueryName> = ExecuteGraphQLHTTPOptions & {
7
- cache?: Partial<GraphQLCacheConfig>;
8
- } & AsyncDataOptions<ResultOf<TName>>;
4
+ import type { IsEmptyObject } from "../../shared/lib/types.js";
5
+ import { type CacheConfig } from "../lib/cache.js";
6
+ type UseAsyncGraphQLQueryOptions<TName extends QueryName> = AsyncDataOptions<ResultOf<TName>> & {
7
+ cache?: Partial<CacheConfig>;
8
+ };
9
9
  /**
10
10
  * Async GraphQL query composable with caching support.
11
11
  *
@@ -13,5 +13,5 @@ type UseAsyncGraphQLQueryOptions<TName extends QueryName> = ExecuteGraphQLHTTPOp
13
13
  * @param args Operation variables (if any) and optional HTTP headers.
14
14
  * @returns Nuxt AsyncData wrapper for the query result.
15
15
  */
16
- export declare function useAsyncGraphQLQuery<TName extends QueryName>(operationName: TName, ...args: IsEmptyObject<VariablesOf<TName>> extends true ? [variables?: MaybeRefOrGetter<VariablesOf<TName>>, options?: UseAsyncGraphQLQueryOptions<TName>] : [variables: MaybeRefOrGetter<VariablesOf<TName>>, options?: UseAsyncGraphQLQueryOptions<TName>]): ReturnType<typeof useAsyncData<ResultOf<TName>>>;
16
+ export declare function useAsyncGraphQLQuery<TName extends QueryName>(operationName: TName, ...args: IsEmptyObject<VariablesOf<TName>> extends true ? [variables?: MaybeRefOrGetter<VariablesOf<TName>>, options?: UseAsyncGraphQLQueryOptions<TName>] : [variables: MaybeRefOrGetter<VariablesOf<TName>>, options?: UseAsyncGraphQLQueryOptions<TName>]): AsyncData<ResultOf<TName> | null, Error | undefined>;
17
17
  export {};
@@ -1,14 +1,16 @@
1
- import { useAsyncData, useNuxtData, useRuntimeConfig } from "#app";
1
+ import { useAsyncData, useNuxtApp, useNuxtData, useRuntimeConfig } from "#app";
2
2
  import { computed, toValue } from "#imports";
3
- import { executeGraphQLHTTP } from "../lib/execute-http.js";
3
+ import { getOperationDocument } from "../../shared/lib/registry.js";
4
+ import { getCacheKeyParts, resolveCacheConfig } from "../lib/cache.js";
4
5
  import { getInFlightRequests } from "../lib/in-flight.js";
5
6
  import { getPersistedEntry, setPersistedEntry } from "../lib/persisted.js";
6
- import { getCacheKeyParts, resolveCacheConfig } from "../../shared/lib/cache.js";
7
7
  export function useAsyncGraphQLQuery(operationName, ...args) {
8
+ const { $executeGraphQL } = useNuxtApp();
8
9
  const [variables, options] = args;
10
+ const document = getOperationDocument(operationName);
9
11
  const isClient = import.meta.client;
10
12
  const { public: { graphql } } = useRuntimeConfig();
11
- const { headers, cache, ...asyncDataOptions } = options ?? {};
13
+ const { cache, ...asyncDataOptions } = options ?? {};
12
14
  const cacheConfig = resolveCacheConfig(graphql.cacheConfig, cache);
13
15
  const cacheKey = computed(() => getCacheKeyParts(cacheConfig, operationName, toValue(variables)).key);
14
16
  const inFlight = getInFlightRequests();
@@ -17,16 +19,18 @@ export function useAsyncGraphQLQuery(operationName, ...args) {
17
19
  if (inFlight.has(key)) {
18
20
  return inFlight.get(key);
19
21
  }
20
- const promise = executeGraphQLHTTP(operationName, toValue(variables), { headers }).then((data) => {
22
+ const promise = $executeGraphQL({ query: document, variables: toValue(variables), operationName }).then((result) => {
23
+ if (result.error) {
24
+ throw result.error;
25
+ }
26
+ const data = result.data;
21
27
  if (isClient && cacheConfig.ttl !== void 0) {
22
28
  setPersistedEntry(key, data, cacheConfig.ttl);
23
29
  }
24
30
  return data;
25
31
  });
26
32
  inFlight.set(key, promise);
27
- promise.finally(() => {
28
- inFlight.delete(key);
29
- });
33
+ promise.finally(() => inFlight.delete(key));
30
34
  return promise;
31
35
  }
32
36
  async function asyncDataHandler() {
@@ -1,13 +1,21 @@
1
1
  import type { QueryName } from "#graphql/registry";
2
+ import { type CacheConfig } from "../lib/cache.js";
2
3
  /**
3
4
  * GraphQL cache helper composable.
4
5
  *
5
6
  * @returns Cache config and invalidation helper.
6
7
  */
7
8
  export declare function useGraphQLCache(): {
8
- readonly cacheConfig: any;
9
+ readonly cacheConfig: CacheConfig;
9
10
  readonly invalidate: (options?: {
10
11
  operation: QueryName;
11
12
  variables?: unknown;
12
13
  }) => Promise<void>;
13
14
  };
15
+ declare module "nuxt/schema" {
16
+ interface PublicRuntimeConfig {
17
+ graphql: {
18
+ cacheConfig: CacheConfig;
19
+ };
20
+ }
21
+ }
@@ -1,5 +1,5 @@
1
1
  import { clearNuxtData, useRuntimeConfig } from "#imports";
2
- import { getCacheKeyParts } from "../../shared/lib/cache.js";
2
+ import { getCacheKeyParts } from "../lib/cache.js";
3
3
  import { deletePersistedByPrefix, deletePersistedEntry } from "../lib/persisted.js";
4
4
  export function useGraphQLCache() {
5
5
  const { public: { graphql: { cacheConfig } } } = useRuntimeConfig();
@@ -1,15 +1,6 @@
1
1
  import type { MutationName, ResultOf, VariablesOf } from "#graphql/registry";
2
- import { type ExecuteGraphQLHTTPOptions } from "../lib/execute-http.js";
3
- import { type SafeResult } from "../../shared/lib/error.js";
4
- import type { IsEmptyObject } from "../../shared/lib/utils.js";
5
- /**
6
- * GraphQL mutation composable with pending state.
7
- *
8
- * @param operationName Operation name from the registry.
9
- * @param options HTTP options including headers.
10
- * @returns Mutation helpers and pending ref.
11
- */
12
- export declare function useGraphQLMutation<TName extends MutationName>(operationName: TName, options?: ExecuteGraphQLHTTPOptions): {
13
- mutate: (...args: IsEmptyObject<VariablesOf<TName>> extends true ? [variables?: VariablesOf<TName>] : [variables: VariablesOf<TName>]) => Promise<SafeResult<ResultOf<TName>>>;
2
+ import type { ExecuteGraphQLResult, IsEmptyObject } from "../../shared/lib/types.js";
3
+ export declare function useGraphQLMutation<TName extends MutationName>(operationName: TName): {
14
4
  pending: import("vue").Ref<boolean, boolean>;
5
+ mutate: (...args: IsEmptyObject<VariablesOf<TName>> extends true ? [variables?: VariablesOf<TName>] : [variables: VariablesOf<TName>]) => Promise<ExecuteGraphQLResult<ResultOf<TName>>>;
15
6
  };