nuxt-graphql-middleware 4.1.1 → 4.3.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 (63) hide show
  1. package/dist/client/200.html +9 -9
  2. package/dist/client/404.html +9 -9
  3. package/dist/client/_nuxt/{Bk31qVW4.js → B4FhP7a6.js} +1 -1
  4. package/dist/client/_nuxt/CH4m2wxh.js +25 -0
  5. package/dist/client/_nuxt/CPd6XBwJ.js +1 -0
  6. package/dist/client/_nuxt/GKcsigNx.js +1 -0
  7. package/dist/client/_nuxt/VR7nYXIq.js +2 -0
  8. package/dist/client/_nuxt/builds/latest.json +1 -1
  9. package/dist/client/_nuxt/builds/meta/172a2fbf-9aed-4d11-81c4-ac46ab9b664c.json +1 -0
  10. package/dist/client/_nuxt/entry.D9ltLgme.css +1 -0
  11. package/dist/client/_nuxt/error-404.SWzu_puR.css +1 -0
  12. package/dist/client/_nuxt/error-500.Bkv_zTjr.css +1 -0
  13. package/dist/client/index.html +9 -9
  14. package/dist/module.d.mts +21 -17
  15. package/dist/module.d.ts +21 -17
  16. package/dist/module.json +3 -3
  17. package/dist/module.mjs +177 -73
  18. package/dist/runtime/clientOptions/index.d.ts +2 -0
  19. package/dist/runtime/clientOptions/index.js +3 -0
  20. package/dist/runtime/composables/nuxtApp.js +1 -1
  21. package/dist/runtime/composables/useAsyncGraphqlQuery.d.ts +4 -2
  22. package/dist/runtime/composables/useAsyncGraphqlQuery.js +26 -10
  23. package/dist/runtime/composables/useGraphqlMutation.d.ts +2 -2
  24. package/dist/runtime/composables/useGraphqlMutation.js +19 -2
  25. package/dist/runtime/composables/useGraphqlQuery.d.ts +2 -2
  26. package/dist/runtime/composables/useGraphqlQuery.js +29 -4
  27. package/dist/runtime/composables/useGraphqlState.d.ts +2 -1
  28. package/dist/runtime/composables/useGraphqlUploadMutation.d.ts +2 -2
  29. package/dist/runtime/composables/useGraphqlUploadMutation.js +19 -3
  30. package/dist/runtime/{composables/shared.d.ts → helpers/composables.d.ts} +8 -1
  31. package/dist/runtime/helpers/composables.js +17 -0
  32. package/dist/runtime/plugins/provideState.d.ts +6 -1
  33. package/dist/runtime/server/tsconfig.json +3 -0
  34. package/dist/runtime/server/utils/index.d.ts +3 -0
  35. package/dist/runtime/server/utils/index.js +12 -0
  36. package/dist/runtime/server/utils/useGraphqlMutation.d.ts +7 -0
  37. package/dist/runtime/server/utils/useGraphqlMutation.js +20 -0
  38. package/dist/runtime/server/utils/useGraphqlQuery.d.ts +7 -0
  39. package/dist/runtime/server/utils/useGraphqlQuery.js +21 -0
  40. package/dist/runtime/serverHandler/helpers/index.d.ts +10 -6
  41. package/dist/runtime/serverHandler/helpers/index.js +40 -9
  42. package/dist/runtime/serverHandler/index.js +15 -7
  43. package/dist/runtime/serverHandler/tsconfig.json +3 -0
  44. package/dist/runtime/serverHandler/upload.js +16 -8
  45. package/dist/runtime/serverOptions/defineGraphqlServerOptions.d.ts +2 -1
  46. package/dist/runtime/settings/index.d.ts +2 -1
  47. package/dist/runtime/settings/index.js +2 -1
  48. package/dist/runtime/types.d.ts +52 -1
  49. package/dist/types.d.mts +0 -4
  50. package/dist/types.d.ts +0 -4
  51. package/package.json +15 -8
  52. package/dist/client/_nuxt/CHM3Rz8p.js +0 -1
  53. package/dist/client/_nuxt/CUKk0hSk.js +0 -24
  54. package/dist/client/_nuxt/Ca6k-hEN.js +0 -2
  55. package/dist/client/_nuxt/KmIjpfWJ.js +0 -1
  56. package/dist/client/_nuxt/builds/meta/42e92f16-7f29-4b6f-8e46-68076031ade3.json +0 -1
  57. package/dist/client/_nuxt/entry.BMcZ91J5.css +0 -1
  58. package/dist/client/_nuxt/error-404.CjTTbIxB.css +0 -1
  59. package/dist/client/_nuxt/error-500.B4KzowuE.css +0 -1
  60. package/dist/runtime/composables/server.d.ts +0 -11
  61. package/dist/runtime/composables/server.js +0 -29
  62. package/dist/runtime/composables/shared.js +0 -5
  63. /package/dist/client/_nuxt/{index.DCCKx2Zk.css → index.D19Q16VT.css} +0 -0
package/dist/module.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  import { fileURLToPath } from 'url';
2
2
  import { dirname, resolve, relative } from 'pathe';
3
3
  import { defu } from 'defu';
4
- import { useLogger, resolveAlias, resolveFiles, defineNuxtModule, createResolver, addImports, addTemplate, addServerHandler, addPlugin, updateTemplates } from '@nuxt/kit';
4
+ import { useLogger, resolveAlias, resolveFiles, defineNuxtModule, createResolver, addImports, addServerImports, addTemplate, addServerHandler, addPlugin, updateTemplates } from '@nuxt/kit';
5
5
  import inquirer from 'inquirer';
6
6
  import { onDevToolsInitialized, extendServerRpc } from '@nuxt/devtools-kit';
7
7
  import { existsSync } from 'fs';
@@ -21,7 +21,7 @@ import { pascalCase } from 'change-case-all';
21
21
  import colors from 'picocolors';
22
22
 
23
23
  const name = "nuxt-graphql-middleware";
24
- const version = "4.1.1";
24
+ const version = "4.3.0";
25
25
 
26
26
  const DEVTOOLS_UI_ROUTE = "/__nuxt-graphql-middleware";
27
27
  const DEVTOOLS_UI_LOCAL_PORT = 3300;
@@ -239,10 +239,10 @@ const plugin$1 = (_schema, documents, config) => {
239
239
  import type { GraphqlResponse } from '#graphql-middleware-server-options-build'
240
240
  import type {
241
241
  ${imports.join(",\n ")}
242
- } from './graphql-operations'
242
+ } from './../graphql-operations'
243
243
 
244
244
 
245
- declare module '#build/nuxt-graphql-middleware' {
245
+ declare module '#nuxt-graphql-middleware/generated-types' {
246
246
  export type GraphqlMiddlewareResponseUnion = ${resultTypes.join(" | ")}
247
247
  ${code}
248
248
  }
@@ -400,7 +400,7 @@ const defaultOptions = {
400
400
  }
401
401
  },
402
402
  downloadSchema: true,
403
- schemaPath: "./schema.graphql",
403
+ schemaPath: "~~/schema.graphql",
404
404
  serverApiPrefix: "/api/graphql_middleware",
405
405
  graphqlEndpoint: "",
406
406
  debug: false,
@@ -423,8 +423,8 @@ function validateOptions(options) {
423
423
  throw new Error("Missing graphqlEndpoint.");
424
424
  }
425
425
  }
426
- async function getSchemaPath(options, resolver, writeToDisk = false) {
427
- const dest = resolver(options.schemaPath);
426
+ async function getSchemaPath(schemaPath, options, resolver, writeToDisk = false) {
427
+ const dest = resolver(schemaPath);
428
428
  if (!options.downloadSchema) {
429
429
  const fileExists2 = await promises.access(dest).then(() => true).catch(() => false);
430
430
  if (!fileExists2) {
@@ -505,6 +505,9 @@ async function buildDocuments(providedDocuments = [], autoImportPatterns, resolv
505
505
  return documents2;
506
506
  }
507
507
  return documents2.map((v) => {
508
+ if (!v.content.trim()) {
509
+ return null;
510
+ }
508
511
  try {
509
512
  return {
510
513
  content: inlineFragments(v.content, resolveAlias),
@@ -518,6 +521,8 @@ async function buildDocuments(providedDocuments = [], autoImportPatterns, resolv
518
521
  }
519
522
  return null;
520
523
  }).filter(falsy);
524
+ }).then((docs) => {
525
+ return docs.filter((v) => v.content.trim());
521
526
  });
522
527
  if (!autoInlineFragments) {
523
528
  return documents;
@@ -536,23 +541,23 @@ async function buildDocuments(providedDocuments = [], autoImportPatterns, resolv
536
541
  });
537
542
  return documents;
538
543
  }
539
- function parseDocument(document, srcDir) {
540
- let name = document.filename ? document.filename.replace(srcDir, "") : "";
544
+ function parseDocument(document, rootDir) {
545
+ let name = document.filename ? document.filename.replace(rootDir, "") : "";
541
546
  if (name.charAt(0) === "/") {
542
547
  name = name.slice(1);
543
548
  }
544
549
  const source = new Source(document.content, name);
545
550
  return parse(source);
546
551
  }
547
- function validateDocuments(schema, documents, srcDir) {
552
+ function validateDocuments(schema, documents, rootDir) {
548
553
  const validated = [];
549
554
  for (let i = 0; i < documents.length; i++) {
550
555
  const document = { ...documents[i] };
551
556
  if (document.filename) {
552
- document.relativePath = document.filename.replace(srcDir + "/", "");
557
+ document.relativePath = document.filename.replace(rootDir + "/", "");
553
558
  }
554
559
  try {
555
- const node = parseDocument(document, srcDir);
560
+ const node = parseDocument(document, rootDir);
556
561
  document.content = print(node);
557
562
  document.errors = validateGraphQlDocuments(schema, [
558
563
  node
@@ -626,7 +631,7 @@ function cleanGraphqlDocument(graphqlContent, operationName) {
626
631
  ]
627
632
  };
628
633
  }
629
- async function generate(options, schemaPath, resolver, srcDir, logEverything = false) {
634
+ async function generate(options, schemaPath, resolver, rootDir, logEverything = false) {
630
635
  const schemaContent = await promises.readFile(schemaPath).then((v) => v.toString());
631
636
  const schema = await loadSchema(schemaContent, { loaders: [] });
632
637
  const documents = await buildDocuments(
@@ -635,12 +640,15 @@ async function generate(options, schemaPath, resolver, srcDir, logEverything = f
635
640
  resolver,
636
641
  !!options.autoInlineFragments
637
642
  );
638
- const validated = validateDocuments(schema, documents, srcDir);
643
+ const validated = validateDocuments(schema, documents, rootDir);
639
644
  const extracted = validated.filter(
640
645
  (v) => !v.operation
641
646
  );
642
647
  for (let i = 0; i < validated.length; i++) {
643
648
  const v = validated[i];
649
+ if (!v) {
650
+ continue;
651
+ }
644
652
  if (v.isValid) {
645
653
  try {
646
654
  const node = parse(v.content);
@@ -704,7 +712,7 @@ async function generate(options, schemaPath, resolver, srcDir, logEverything = f
704
712
  })
705
713
  };
706
714
  }
707
- const fileExists = (path, extensions = ["js", "ts"]) => {
715
+ const fileExists = (path, extensions = ["js", "ts", "mjs"]) => {
708
716
  if (!path) {
709
717
  return null;
710
718
  } else if (existsSync$1(path)) {
@@ -733,22 +741,45 @@ const module = defineNuxtModule({
733
741
  configKey: "graphqlMiddleware",
734
742
  version,
735
743
  compatibility: {
736
- nuxt: ">=3.1.0"
744
+ nuxt: ">=3.13.0"
737
745
  }
738
746
  },
739
747
  defaults: defaultOptions,
740
748
  async setup(passedOptions, nuxt) {
741
749
  const options = defu({}, passedOptions, defaultOptions);
750
+ const isModuleBuild = process.env.MODULE_BUILD === "true" && nuxt.options._prepare;
751
+ if (isModuleBuild) {
752
+ options.graphqlEndpoint = "http://localhost";
753
+ options.downloadSchema = false;
754
+ options.schemaPath = "~~/schema.graphql";
755
+ options.autoInlineFragments = true;
756
+ options.autoImportPatterns = [
757
+ "~~/playground/**/*.{gql,graphql}",
758
+ "!node_modules"
759
+ ];
760
+ }
742
761
  if (!passedOptions.autoImportPatterns) {
743
- options.autoImportPatterns = ["**/*.{gql,graphql}", "!node_modules"];
762
+ options.autoImportPatterns = ["~~/**/*.{gql,graphql}", "!node_modules"];
763
+ }
764
+ options.autoImportPatterns = (options.autoImportPatterns || []).map(
765
+ (pattern) => {
766
+ return resolveAlias(pattern);
767
+ }
768
+ );
769
+ if (!nuxt.options._prepare) {
770
+ validateOptions(options);
744
771
  }
745
- validateOptions(options);
772
+ const schemaPathReplaced = resolveAlias(options.schemaPath);
746
773
  const moduleResolver = createResolver(import.meta.url);
747
- const srcDir = nuxt.options.srcDir;
748
- const srcResolver = createResolver(srcDir).resolve;
774
+ const serverResolver = createResolver(nuxt.options.serverDir);
775
+ const srcResolver = createResolver(nuxt.options.srcDir);
776
+ const appResolver = createResolver(nuxt.options.dir.app);
777
+ const rootDir = nuxt.options.rootDir;
778
+ const rootResolver = createResolver(rootDir);
749
779
  const schemaPath = await getSchemaPath(
780
+ schemaPathReplaced,
750
781
  options,
751
- srcResolver,
782
+ rootResolver.resolve,
752
783
  options.downloadSchema
753
784
  );
754
785
  const runtimeDir = fileURLToPath(new URL("./runtime", import.meta.url));
@@ -790,8 +821,8 @@ const module = defineNuxtModule({
790
821
  const { templates, hasErrors, documents } = await generate(
791
822
  options,
792
823
  schemaPath,
793
- srcResolver,
794
- srcDir,
824
+ rootResolver.resolve,
825
+ rootDir,
795
826
  isFirst
796
827
  );
797
828
  ctx.templates = templates;
@@ -831,7 +862,12 @@ const module = defineNuxtModule({
831
862
  });
832
863
  prompt.then(async ({ accept }) => {
833
864
  if (accept) {
834
- await getSchemaPath(options, srcResolver, true);
865
+ await getSchemaPath(
866
+ schemaPathReplaced,
867
+ options,
868
+ rootResolver.resolve,
869
+ true
870
+ );
835
871
  await generateHandler();
836
872
  }
837
873
  });
@@ -849,37 +885,30 @@ const module = defineNuxtModule({
849
885
  graphqlEndpoint: options.graphqlEndpoint || ""
850
886
  };
851
887
  if (options.includeComposables) {
852
- addImports({
853
- from: moduleResolver.resolve("./runtime/composables/useGraphqlQuery"),
854
- name: "useGraphqlQuery"
855
- });
856
- addImports({
857
- from: moduleResolver.resolve(
858
- "./runtime/composables/useGraphqlMutation"
859
- ),
860
- name: "useGraphqlMutation"
861
- });
862
- addImports({
863
- from: moduleResolver.resolve("./runtime/composables/useGraphqlState"),
864
- name: "useGraphqlState"
865
- });
866
- addImports({
867
- from: moduleResolver.resolve(
868
- "./runtime/composables/useAsyncGraphqlQuery"
869
- ),
870
- name: "useAsyncGraphqlQuery"
871
- });
872
- nuxt.options.alias["#graphql-composable"] = moduleResolver.resolve(
873
- "runtime/composables/server"
874
- );
888
+ const nuxtComposables = [
889
+ "useGraphqlQuery",
890
+ "useGraphqlMutation",
891
+ "useGraphqlState",
892
+ "useAsyncGraphqlQuery"
893
+ ];
875
894
  if (options.enableFileUploads) {
895
+ nuxtComposables.push("useGraphqlUploadMutation");
896
+ }
897
+ nuxtComposables.forEach((name2) => {
876
898
  addImports({
877
- from: moduleResolver.resolve(
878
- "./runtime/composables/useGraphqlUploadMutation"
879
- ),
880
- name: "useGraphqlUploadMutation"
899
+ from: moduleResolver.resolve("./runtime/composables/" + name2),
900
+ name: name2
881
901
  });
882
- }
902
+ });
903
+ const serverUtils = ["useGraphqlQuery", "useGraphqlMutation"].map(
904
+ (name2) => {
905
+ return {
906
+ from: moduleResolver.resolve("./runtime/server/utils/" + name2),
907
+ name: name2
908
+ };
909
+ }
910
+ );
911
+ addServerImports(serverUtils);
883
912
  }
884
913
  Object.values(GraphqlMiddlewareTemplate).forEach((filename) => {
885
914
  const result = addTemplate({
@@ -896,6 +925,8 @@ const module = defineNuxtModule({
896
925
  nuxt.options.alias["#graphql-documents"] = result.dst;
897
926
  } else if (result.dst.includes(GraphqlMiddlewareTemplate.OperationTypes)) {
898
927
  nuxt.options.alias["#graphql-operations"] = result.dst;
928
+ } else if (result.dst.includes(GraphqlMiddlewareTemplate.ComposableContext)) {
929
+ nuxt.options.alias["#nuxt-graphql-middleware/generated-types"] = result.dst;
899
930
  }
900
931
  });
901
932
  addTemplate({
@@ -906,7 +937,7 @@ const module = defineNuxtModule({
906
937
  import type {
907
938
  GraphqlMiddlewareQuery,
908
939
  GraphqlMiddlewareMutation,
909
- } from '#build/nuxt-graphql-middleware'
940
+ } from '#nuxt-graphql-middleware/generated-types'
910
941
 
911
942
  declare module '#graphql-documents' {
912
943
  type Documents = {
@@ -919,36 +950,109 @@ declare module '#graphql-documents' {
919
950
  `;
920
951
  }
921
952
  });
922
- const extensions = ["js", "mjs", "ts"];
923
- const resolvedPath = "~/app/graphqlMiddleware.serverOptions".replace(/^(~~|@@)/, nuxt.options.rootDir).replace(/^(~|@)/, nuxt.options.srcDir);
924
- const template = (() => {
925
- const resolvedFilename = `graphqlMiddleware.serverOptions.ts`;
926
- const maybeUserFile = fileExists(resolvedPath, extensions);
927
- const moduleTypesPath = relative(
928
- nuxt.options.buildDir,
929
- moduleResolver.resolve("./types")
930
- );
931
- const serverOptionsLine = maybeUserFile ? `import serverOptions from './../app/graphqlMiddleware.serverOptions'` : `const serverOptions: GraphqlMiddlewareServerOptions = {}`;
932
- return addTemplate({
933
- filename: resolvedFilename,
934
- write: true,
935
- getContents: () => `
936
- import type { GraphqlMiddlewareServerOptions } from '${moduleTypesPath}'
953
+ const findServerOptions = () => {
954
+ const newPath = serverResolver.resolve("graphqlMiddleware.serverOptions");
955
+ const serverPath = fileExists(newPath);
956
+ if (serverPath) {
957
+ return serverPath;
958
+ }
959
+ const candidates = [
960
+ rootResolver.resolve("graphqlMiddleware.serverOptions"),
961
+ rootResolver.resolve("app/graphqlMiddleware.serverOptions"),
962
+ srcResolver.resolve("graphqlMiddleware.serverOptions")
963
+ ];
964
+ for (let i = 0; i < candidates.length; i++) {
965
+ const path = candidates[i];
966
+ const filePath = fileExists(path);
967
+ if (filePath) {
968
+ logger.warn(
969
+ `The graphqlMiddleware.serverOptions file should be placed in Nuxt's <serverDir> ("${nuxt.options.serverDir}/graphqlMiddleware.serverOptions.ts"). The new path will be enforced in the next major release.`
970
+ );
971
+ return filePath;
972
+ }
973
+ }
974
+ logger.info("No graphqlMiddleware.serverOptions file found.");
975
+ };
976
+ const resolvedPath = findServerOptions();
977
+ const moduleTypesPath = relative(
978
+ nuxt.options.buildDir,
979
+ moduleResolver.resolve("./types")
980
+ );
981
+ const resolvedPathRelative = resolvedPath ? relative(nuxt.options.buildDir, resolvedPath) : null;
982
+ const template = addTemplate({
983
+ filename: "graphqlMiddleware.serverOptions.mjs",
984
+ write: true,
985
+ getContents: () => {
986
+ const serverOptionsLine = resolvedPathRelative ? `import serverOptions from '${resolvedPathRelative}'` : `const serverOptions = {}`;
987
+ return `
937
988
  ${serverOptionsLine}
989
+ export { serverOptions }
990
+ `;
991
+ }
992
+ });
993
+ addTemplate({
994
+ filename: "graphqlMiddleware.serverOptions.d.ts",
995
+ write: true,
996
+ getContents: () => {
997
+ const serverOptionsLineTypes = resolvedPathRelative ? `import serverOptions from '${resolvedPathRelative}'` : `const serverOptions: GraphqlMiddlewareServerOptions = {}`;
998
+ return `
999
+ import type { GraphqlMiddlewareServerOptions } from '${moduleTypesPath}'
1000
+ ${serverOptionsLineTypes}
938
1001
  import type { GraphqlServerResponse } from '#graphql-middleware/types'
939
- import type { GraphqlMiddlewareResponseUnion } from '#build/nuxt-graphql-middleware'
1002
+ import type { GraphqlMiddlewareResponseUnion } from '#nuxt-graphql-middleware/generated-types'
940
1003
 
941
1004
  type GraphqlResponseAdditions =
942
- typeof serverOptions extends GraphqlMiddlewareServerOptions<infer R> ? R : {}
1005
+ typeof serverOptions extends GraphqlMiddlewareServerOptions<infer R, any, any> ? R : {}
943
1006
 
944
1007
  export type GraphqlResponse<T> = GraphqlServerResponse<T> & GraphqlResponseAdditions
945
1008
 
946
1009
  export type GraphqlResponseTyped = GraphqlResponse<GraphqlMiddlewareResponseUnion>
947
1010
 
948
1011
  export { serverOptions }
949
- `
950
- });
951
- })();
1012
+ `;
1013
+ }
1014
+ });
1015
+ const getClientOptionsImport = () => {
1016
+ const clientOptionsPath = appResolver.resolve(
1017
+ "graphqlMiddleware.clientOptions"
1018
+ );
1019
+ if (fileExists(clientOptionsPath)) {
1020
+ const pathRelative = relative(nuxt.options.buildDir, clientOptionsPath);
1021
+ return `import clientOptions from '${pathRelative}'`;
1022
+ }
1023
+ };
1024
+ const clientOptionsImport = getClientOptionsImport();
1025
+ const clientOptionsTemplate = addTemplate({
1026
+ filename: "graphqlMiddleware.clientOptions.mjs",
1027
+ write: true,
1028
+ getContents: () => {
1029
+ if (clientOptionsImport) {
1030
+ return `${clientOptionsImport}
1031
+ export { clientOptions }`;
1032
+ }
1033
+ return `export const clientOptions = {}`;
1034
+ }
1035
+ });
1036
+ addTemplate({
1037
+ filename: "graphqlMiddleware.clientOptions.d.ts",
1038
+ write: true,
1039
+ getContents: () => {
1040
+ if (clientOptionsImport) {
1041
+ return `import { GraphqlClientOptions } from '#graphql-middleware/types'
1042
+ ${clientOptionsImport}
1043
+
1044
+ export type GraphqlClientContext = typeof clientOptions extends GraphqlClientOptions<infer R> ? R : {}
1045
+
1046
+ export { clientOptions }`;
1047
+ }
1048
+ return `import { GraphqlClientOptions } from '#graphql-middleware/types'
1049
+ export const clientOptions: GraphqlClientOptions
1050
+
1051
+ export type GraphqlClientContext = {}
1052
+ `;
1053
+ }
1054
+ });
1055
+ nuxt.options.alias["#graphql-middleware-client-options"] = clientOptionsTemplate.dst;
952
1056
  nuxt.options.nitro.externals = nuxt.options.nitro.externals || {};
953
1057
  nuxt.options.nitro.externals.inline = nuxt.options.nitro.externals.inline || [];
954
1058
  nuxt.options.nitro.externals.inline.push(template.dst);
@@ -0,0 +1,2 @@
1
+ import type { GraphqlClientOptions, ContextType } from './../types.js';
2
+ export declare function defineGraphqlClientOptions<T extends ContextType>(options: GraphqlClientOptions<T>): GraphqlClientOptions<T>;
@@ -0,0 +1,3 @@
1
+ export function defineGraphqlClientOptions(options) {
2
+ return options;
3
+ }
@@ -1,5 +1,5 @@
1
1
  import { useGraphqlState } from "./useGraphqlState.js";
2
- import { getEndpoint } from "./shared.js";
2
+ import { getEndpoint } from "./../helpers/composables.js";
3
3
  import { hash } from "ohash";
4
4
  import { GraphqlMiddlewareCache } from "../helpers/ClientCache.js";
5
5
  import { useNuxtApp, useAppConfig } from "#imports";
@@ -1,13 +1,15 @@
1
- import { type GraphqlMiddlewareQueryName, type KeysOf, type PickFrom } from './shared.js';
1
+ import { type GraphqlMiddlewareQueryName, type KeysOf, type PickFrom } from './../helpers/composables.js';
2
2
  import type { FetchOptions } from 'ofetch';
3
3
  import { type Ref } from 'vue';
4
- import type { GraphqlMiddlewareQuery } from '#build/nuxt-graphql-middleware';
4
+ import { type GraphqlClientContext } from '#graphql-middleware-client-options';
5
+ import type { GraphqlMiddlewareQuery } from '#nuxt-graphql-middleware/generated-types';
5
6
  import type { GraphqlResponse } from '#graphql-middleware-server-options-build';
6
7
  import type { RequestCacheOptions } from '#graphql-middleware/types';
7
8
  import type { AsyncData, AsyncDataOptions, NuxtError } from '#app';
8
9
  type AsyncGraphqlQueryOptions<ResponseType, DefaultT, Keys extends KeysOf<DefaultT>, F> = AsyncDataOptions<ResponseType, DefaultT, Keys> & {
9
10
  graphqlCaching?: RequestCacheOptions;
10
11
  fetchOptions?: F;
12
+ clientContext?: Partial<GraphqlClientContext>;
11
13
  };
12
14
  /**
13
15
  * Wrapper for useAsyncData to perform a single GraphQL query.
@@ -1,6 +1,12 @@
1
+ import {
2
+ encodeContext
3
+ } from "./../helpers/composables.js";
1
4
  import { isRef, unref } from "vue";
2
5
  import { buildRequestParams } from "./../helpers/index.js";
3
6
  import { performRequest } from "./nuxtApp.js";
7
+ import {
8
+ clientOptions
9
+ } from "#graphql-middleware-client-options";
4
10
  import { useAsyncData, useAppConfig, useNuxtApp } from "#imports";
5
11
  import { hash } from "ohash";
6
12
  export function useAsyncGraphqlQuery(name, ...args) {
@@ -28,16 +34,26 @@ export function useAsyncGraphqlQuery(name, ...args) {
28
34
  }
29
35
  return useAsyncData(
30
36
  key,
31
- () => performRequest(
32
- "query",
33
- name,
34
- "get",
35
- {
36
- params: buildRequestParams(unref(variables)),
37
- ...fetchOptions
38
- },
39
- asyncDataOptions.graphqlCaching
40
- ),
37
+ () => {
38
+ const globalClientContext = clientOptions.buildClientContext ? clientOptions.buildClientContext() : {};
39
+ return performRequest(
40
+ "query",
41
+ name,
42
+ "get",
43
+ {
44
+ ...fetchOptions,
45
+ params: {
46
+ ...fetchOptions?.params || {},
47
+ ...buildRequestParams(unref(variables)),
48
+ ...encodeContext({
49
+ ...globalClientContext,
50
+ ...asyncDataOptions.clientContext || {}
51
+ })
52
+ }
53
+ },
54
+ asyncDataOptions.graphqlCaching
55
+ );
56
+ },
41
57
  asyncDataOptions
42
58
  );
43
59
  }
@@ -1,5 +1,5 @@
1
- import { type GraphqlMiddlewareMutationName, type GetMutationArgs, type MutationObjectArgs, type GetMutationResult } from './shared.js';
2
- import type { GraphqlMiddlewareMutation } from '#build/nuxt-graphql-middleware';
1
+ import { type GraphqlMiddlewareMutationName, type GetMutationArgs, type MutationObjectArgs, type GetMutationResult } from './../helpers/composables.js';
2
+ import type { GraphqlMiddlewareMutation } from '#nuxt-graphql-middleware/generated-types';
3
3
  import type { GraphqlResponse } from '#graphql-middleware-server-options-build';
4
4
  /**
5
5
  * Performs a GraphQL mutation.
@@ -1,8 +1,25 @@
1
+ import {
2
+ encodeContext
3
+ } from "./../helpers/composables.js";
1
4
  import { performRequest } from "./nuxtApp.js";
5
+ import { clientOptions } from "#graphql-middleware-client-options";
2
6
  export function useGraphqlMutation(...args) {
3
- const [name, body, fetchOptions = {}] = typeof args[0] === "string" ? [args[0], args[1], args[2]?.fetchOptions] : [args[0].name, args[0].variables, args[0].fetchOptions];
7
+ const [name, body, fetchOptions = {}, overrideClientContext = {}] = typeof args[0] === "string" ? [args[0], args[1], args[2]?.fetchOptions, args[2]?.clientContext] : [
8
+ args[0].name,
9
+ args[0].variables,
10
+ args[0].fetchOptions,
11
+ args[0].clientContext
12
+ ];
13
+ const globalClientContext = clientOptions.buildClientContext ? clientOptions.buildClientContext() : {};
4
14
  return performRequest("mutation", name, "post", {
15
+ ...fetchOptions,
5
16
  body,
6
- ...fetchOptions
17
+ params: {
18
+ ...fetchOptions.params || {},
19
+ ...encodeContext({
20
+ ...globalClientContext,
21
+ ...overrideClientContext
22
+ })
23
+ }
7
24
  });
8
25
  }
@@ -1,5 +1,5 @@
1
- import { type GraphqlMiddlewareQueryName, type GetQueryArgs, type QueryObjectArgs, type GetQueryResult } from './shared.js';
2
- import type { GraphqlMiddlewareQuery } from '#build/nuxt-graphql-middleware';
1
+ import { type GraphqlMiddlewareQueryName, type GetQueryArgs, type QueryObjectArgs, type GetQueryResult } from './../helpers/composables.js';
2
+ import type { GraphqlMiddlewareQuery } from '#nuxt-graphql-middleware/generated-types';
3
3
  import type { GraphqlResponse } from '#graphql-middleware-server-options-build';
4
4
  /**
5
5
  * Performs a GraphQL query.
@@ -1,19 +1,44 @@
1
+ import {
2
+ encodeContext
3
+ } from "./../helpers/composables.js";
1
4
  import { buildRequestParams } from "./../helpers/index.js";
2
5
  import { performRequest } from "./nuxtApp.js";
6
+ import { clientOptions } from "#graphql-middleware-client-options";
3
7
  export function useGraphqlQuery(...args) {
4
- const [name, variables, fetchOptions = {}, graphqlCaching = {}] = typeof args[0] === "string" ? [args[0], args[1], args[2]?.fetchOptions, args[2]?.graphqlCaching] : [
8
+ const [
9
+ name,
10
+ variables,
11
+ fetchOptions = {},
12
+ graphqlCaching = {},
13
+ overrideClientContext = {}
14
+ ] = typeof args[0] === "string" ? [
15
+ args[0],
16
+ args[1],
17
+ args[2]?.fetchOptions,
18
+ args[2]?.graphqlCaching,
19
+ args[2]?.clientContext
20
+ ] : [
5
21
  args[0].name,
6
22
  args[0].variables,
7
23
  args[0].fetchOptions,
8
- args[0].graphqlCaching
24
+ args[0].graphqlCaching,
25
+ args[0].clientContext
9
26
  ];
27
+ const globalClientContext = clientOptions.buildClientContext ? clientOptions.buildClientContext() : {};
10
28
  return performRequest(
11
29
  "query",
12
30
  name,
13
31
  "get",
14
32
  {
15
- params: buildRequestParams(variables),
16
- ...fetchOptions
33
+ ...fetchOptions,
34
+ params: {
35
+ ...fetchOptions.params || {},
36
+ ...buildRequestParams(variables),
37
+ ...encodeContext({
38
+ ...globalClientContext,
39
+ ...overrideClientContext
40
+ })
41
+ }
17
42
  },
18
43
  graphqlCaching
19
44
  );
@@ -1,2 +1,3 @@
1
+ import type { NuxtApp } from '#app';
1
2
  import type { GraphqlMiddlewareState } from '#graphql-middleware/types';
2
- export declare const useGraphqlState: (providedApp?: any) => GraphqlMiddlewareState | null;
3
+ export declare const useGraphqlState: (providedApp?: NuxtApp) => GraphqlMiddlewareState | null;
@@ -1,5 +1,5 @@
1
- import { type GraphqlMiddlewareMutationName, type GetMutationArgs, type MutationObjectArgs, type GetMutationResult } from './shared.js';
2
- import type { GraphqlMiddlewareMutation } from '#build/nuxt-graphql-middleware';
1
+ import { type GraphqlMiddlewareMutationName, type GetMutationArgs, type MutationObjectArgs, type GetMutationResult } from './../helpers/composables.js';
2
+ import type { GraphqlMiddlewareMutation } from '#nuxt-graphql-middleware/generated-types';
3
3
  import type { GraphqlResponse } from '#graphql-middleware-server-options-build';
4
4
  /**
5
5
  * Performs a GraphQL upload mutation.
@@ -1,6 +1,8 @@
1
1
  import {
2
- getEndpoint
3
- } from "./shared.js";
2
+ getEndpoint,
3
+ encodeContext
4
+ } from "./../helpers/composables.js";
5
+ import { clientOptions } from "#graphql-middleware-client-options";
4
6
  import { useGraphqlState } from "#imports";
5
7
  function createFormData(variables) {
6
8
  const formData = new FormData();
@@ -34,7 +36,12 @@ function createFormData(variables) {
34
36
  return formData;
35
37
  }
36
38
  export function useGraphqlUploadMutation(...args) {
37
- const [name, variables, fetchOptions = {}] = typeof args[0] === "string" ? [args[0], args[1]] : [args[0].name, args[0].variables, args[0].fetchOptions];
39
+ const [name, variables, fetchOptions = {}, overrideClientContext = {}] = typeof args[0] === "string" ? [args[0], args[1], args[2]?.fetchOptions, args[2]?.clientContext] : [
40
+ args[0].name,
41
+ args[0].variables,
42
+ args[0].fetchOptions,
43
+ args[0].clientContext
44
+ ];
38
45
  if (!variables) {
39
46
  throw new Error(
40
47
  'Using "useGraphqlUploadMutation" without variables is not supported.'
@@ -42,9 +49,18 @@ export function useGraphqlUploadMutation(...args) {
42
49
  }
43
50
  const state = useGraphqlState();
44
51
  const formData = createFormData(variables);
52
+ const globalClientContext = clientOptions.buildClientContext ? clientOptions.buildClientContext() : {};
53
+ const clientContext = encodeContext({
54
+ ...globalClientContext,
55
+ ...overrideClientContext
56
+ });
45
57
  return $fetch(getEndpoint("upload", name), {
46
58
  ...state && state.fetchOptions ? state.fetchOptions : {},
47
59
  ...fetchOptions || {},
60
+ params: {
61
+ ...clientContext,
62
+ ...fetchOptions.params || {}
63
+ },
48
64
  method: "POST",
49
65
  body: formData
50
66
  }).then((v) => {