nuxt-graphql-middleware 4.2.0 → 4.3.1

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/client/200.html +9 -9
  2. package/dist/client/404.html +9 -9
  3. package/dist/client/_nuxt/{BBbivCOF.js → B4FhP7a6.js} +1 -1
  4. package/dist/client/_nuxt/CH4m2wxh.js +25 -0
  5. package/dist/client/_nuxt/{DbuEOF3O.js → CPd6XBwJ.js} +1 -1
  6. package/dist/client/_nuxt/{Ch07F_Ul.js → GKcsigNx.js} +1 -1
  7. package/dist/client/_nuxt/{BKf42UCq.js → VR7nYXIq.js} +1 -1
  8. package/dist/client/_nuxt/builds/latest.json +1 -1
  9. package/dist/client/_nuxt/builds/meta/08eb555c-2460-4359-b00b-ff7301e8150d.json +1 -0
  10. package/dist/client/index.html +9 -9
  11. package/dist/module.d.mts +20 -15
  12. package/dist/module.d.ts +20 -15
  13. package/dist/module.json +1 -1
  14. package/dist/module.mjs +115 -54
  15. package/dist/runtime/clientOptions/index.d.ts +2 -0
  16. package/dist/runtime/clientOptions/index.js +3 -0
  17. package/dist/runtime/composables/nuxtApp.js +1 -1
  18. package/dist/runtime/composables/useAsyncGraphqlQuery.d.ts +4 -2
  19. package/dist/runtime/composables/useAsyncGraphqlQuery.js +26 -10
  20. package/dist/runtime/composables/useGraphqlMutation.d.ts +2 -2
  21. package/dist/runtime/composables/useGraphqlMutation.js +19 -2
  22. package/dist/runtime/composables/useGraphqlQuery.d.ts +2 -2
  23. package/dist/runtime/composables/useGraphqlQuery.js +29 -4
  24. package/dist/runtime/composables/useGraphqlUploadMutation.d.ts +2 -2
  25. package/dist/runtime/composables/useGraphqlUploadMutation.js +19 -3
  26. package/dist/runtime/{composables/shared.d.ts → helpers/composables.d.ts} +8 -1
  27. package/dist/runtime/helpers/composables.js +17 -0
  28. package/dist/runtime/server/tsconfig.json +3 -0
  29. package/dist/runtime/server/utils/index.d.ts +3 -0
  30. package/dist/runtime/server/utils/index.js +12 -0
  31. package/dist/runtime/server/utils/useGraphqlMutation.d.ts +7 -0
  32. package/dist/runtime/server/utils/useGraphqlMutation.js +20 -0
  33. package/dist/runtime/server/utils/useGraphqlQuery.d.ts +7 -0
  34. package/dist/runtime/server/utils/useGraphqlQuery.js +21 -0
  35. package/dist/runtime/serverHandler/helpers/index.d.ts +9 -5
  36. package/dist/runtime/serverHandler/helpers/index.js +40 -9
  37. package/dist/runtime/serverHandler/index.js +15 -7
  38. package/dist/runtime/serverHandler/upload.js +16 -7
  39. package/dist/runtime/serverOptions/defineGraphqlServerOptions.d.ts +2 -1
  40. package/dist/runtime/settings/index.d.ts +2 -1
  41. package/dist/runtime/settings/index.js +2 -1
  42. package/dist/runtime/types.d.ts +52 -1
  43. package/package.json +6 -2
  44. package/dist/client/_nuxt/CaoFd9E8.js +0 -25
  45. package/dist/client/_nuxt/builds/meta/fff2eb15-13a3-4063-86e7-990068006f7e.json +0 -1
  46. package/dist/runtime/composables/server.d.ts +0 -11
  47. package/dist/runtime/composables/server.js +0 -29
  48. package/dist/runtime/composables/shared.js +0 -5
  49. /package/dist/client/_nuxt/{entry.DlumAtbg.css → entry.D9ltLgme.css} +0 -0
  50. /package/dist/client/_nuxt/{error-404.C_4C5G96.css → error-404.SWzu_puR.css} +0 -0
  51. /package/dist/client/_nuxt/{error-500.CBAEdpZV.css → error-500.Bkv_zTjr.css} +0 -0
  52. /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
- import { dirname, resolve, relative } from 'pathe';
2
+ import { dirname, resolve, relative, parse as parse$1, join } 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.2.0";
24
+ const version = "4.3.1";
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
  }
@@ -386,7 +386,7 @@ function logDocuments(logger, documents, logEverything) {
386
386
  }
387
387
  }
388
388
 
389
- const logger = useLogger("nuxt-graphql-middleware");
389
+ const logger = useLogger(name);
390
390
  const defaultOptions = {
391
391
  codegenConfig: {
392
392
  exportFragmentSpreadSubTypes: true,
@@ -445,7 +445,6 @@ async function autoImportDocuments(patterns = [], srcResolver) {
445
445
  if (!patterns.length) {
446
446
  return Promise.resolve([]);
447
447
  }
448
- console.log({ patterns });
449
448
  const files = (await resolveFiles(srcResolver(), patterns, {
450
449
  followSymbolicLinks: false
451
450
  })).filter((path) => {
@@ -543,11 +542,11 @@ async function buildDocuments(providedDocuments = [], autoImportPatterns, resolv
543
542
  return documents;
544
543
  }
545
544
  function parseDocument(document, rootDir) {
546
- let name = document.filename ? document.filename.replace(rootDir, "") : "";
547
- if (name.charAt(0) === "/") {
548
- name = name.slice(1);
545
+ let name2 = document.filename ? document.filename.replace(rootDir, "") : "";
546
+ if (name2.charAt(0) === "/") {
547
+ name2 = name2.slice(1);
549
548
  }
550
- const source = new Source(document.content, name);
549
+ const source = new Source(document.content, name2);
551
550
  return parse(source);
552
551
  }
553
552
  function validateDocuments(schema, documents, rootDir) {
@@ -734,7 +733,21 @@ async function outputDocuments(outputPath, documents) {
734
733
  }
735
734
  });
736
735
  }
736
+ async function getOutputDocumentsPath(optionsOutputDocuments, nuxtBuildDir, resolvePath) {
737
+ if (!optionsOutputDocuments) {
738
+ return null;
739
+ }
740
+ if (typeof optionsOutputDocuments === "boolean") {
741
+ return resolve(nuxtBuildDir, `${name}/documents`);
742
+ } else {
743
+ return await resolvePath(optionsOutputDocuments);
744
+ }
745
+ }
737
746
 
747
+ function pathWithoutExtension(fullPath) {
748
+ const parsed = parse$1(fullPath);
749
+ return join(parsed.dir, parsed.name);
750
+ }
738
751
  const RPC_NAMESPACE = "nuxt-graphql-middleware";
739
752
  const module = defineNuxtModule({
740
753
  meta: {
@@ -774,6 +787,7 @@ const module = defineNuxtModule({
774
787
  const moduleResolver = createResolver(import.meta.url);
775
788
  const serverResolver = createResolver(nuxt.options.serverDir);
776
789
  const srcResolver = createResolver(nuxt.options.srcDir);
790
+ const appResolver = createResolver(nuxt.options.dir.app);
777
791
  const rootDir = nuxt.options.rootDir;
778
792
  const rootResolver = createResolver(rootDir);
779
793
  const schemaPath = await getSchemaPath(
@@ -828,14 +842,15 @@ const module = defineNuxtModule({
828
842
  ctx.templates = templates;
829
843
  ctx.documents = documents;
830
844
  rpc?.broadcast.documentsUpdated(documents);
831
- if (options.outputDocuments) {
832
- const destFolder = resolve(
833
- nuxt.options.buildDir,
834
- "nuxt-graphql-middleware/documents"
835
- );
836
- outputDocuments(destFolder, documents);
845
+ const outputDocumentsPath = await getOutputDocumentsPath(
846
+ options.outputDocuments,
847
+ nuxt.options.buildDir,
848
+ rootResolver.resolvePath
849
+ );
850
+ if (outputDocumentsPath) {
851
+ outputDocuments(outputDocumentsPath, documents);
837
852
  if (isFirst) {
838
- logger.info("Documents generated at " + destFolder);
853
+ logger.info("Documents generated at " + outputDocumentsPath);
839
854
  }
840
855
  }
841
856
  if (hasErrors) {
@@ -885,37 +900,30 @@ const module = defineNuxtModule({
885
900
  graphqlEndpoint: options.graphqlEndpoint || ""
886
901
  };
887
902
  if (options.includeComposables) {
888
- addImports({
889
- from: moduleResolver.resolve("./runtime/composables/useGraphqlQuery"),
890
- name: "useGraphqlQuery"
891
- });
892
- addImports({
893
- from: moduleResolver.resolve(
894
- "./runtime/composables/useGraphqlMutation"
895
- ),
896
- name: "useGraphqlMutation"
897
- });
898
- addImports({
899
- from: moduleResolver.resolve("./runtime/composables/useGraphqlState"),
900
- name: "useGraphqlState"
901
- });
902
- addImports({
903
- from: moduleResolver.resolve(
904
- "./runtime/composables/useAsyncGraphqlQuery"
905
- ),
906
- name: "useAsyncGraphqlQuery"
907
- });
908
- nuxt.options.alias["#graphql-composable"] = moduleResolver.resolve(
909
- "runtime/composables/server"
910
- );
903
+ const nuxtComposables = [
904
+ "useGraphqlQuery",
905
+ "useGraphqlMutation",
906
+ "useGraphqlState",
907
+ "useAsyncGraphqlQuery"
908
+ ];
911
909
  if (options.enableFileUploads) {
910
+ nuxtComposables.push("useGraphqlUploadMutation");
911
+ }
912
+ nuxtComposables.forEach((name2) => {
912
913
  addImports({
913
- from: moduleResolver.resolve(
914
- "./runtime/composables/useGraphqlUploadMutation"
915
- ),
916
- name: "useGraphqlUploadMutation"
914
+ from: moduleResolver.resolve("./runtime/composables/" + name2),
915
+ name: name2
917
916
  });
918
- }
917
+ });
918
+ const serverUtils = ["useGraphqlQuery", "useGraphqlMutation"].map(
919
+ (name2) => {
920
+ return {
921
+ from: moduleResolver.resolve("./runtime/server/utils/" + name2),
922
+ name: name2
923
+ };
924
+ }
925
+ );
926
+ addServerImports(serverUtils);
919
927
  }
920
928
  Object.values(GraphqlMiddlewareTemplate).forEach((filename) => {
921
929
  const result = addTemplate({
@@ -929,9 +937,15 @@ const module = defineNuxtModule({
929
937
  }
930
938
  });
931
939
  if (result.dst.includes(GraphqlMiddlewareTemplate.Documents)) {
932
- nuxt.options.alias["#graphql-documents"] = result.dst;
940
+ nuxt.options.alias["#graphql-documents"] = pathWithoutExtension(
941
+ result.dst
942
+ );
933
943
  } else if (result.dst.includes(GraphqlMiddlewareTemplate.OperationTypes)) {
934
- nuxt.options.alias["#graphql-operations"] = result.dst;
944
+ nuxt.options.alias["#graphql-operations"] = pathWithoutExtension(
945
+ result.dst
946
+ );
947
+ } else if (result.dst.includes(GraphqlMiddlewareTemplate.ComposableContext)) {
948
+ nuxt.options.alias["#nuxt-graphql-middleware/generated-types"] = pathWithoutExtension(result.dst);
935
949
  }
936
950
  });
937
951
  addTemplate({
@@ -942,7 +956,7 @@ const module = defineNuxtModule({
942
956
  import type {
943
957
  GraphqlMiddlewareQuery,
944
958
  GraphqlMiddlewareMutation,
945
- } from '#build/nuxt-graphql-middleware'
959
+ } from '#nuxt-graphql-middleware/generated-types'
946
960
 
947
961
  declare module '#graphql-documents' {
948
962
  type Documents = {
@@ -1003,11 +1017,11 @@ export { serverOptions }
1003
1017
  return `
1004
1018
  import type { GraphqlMiddlewareServerOptions } from '${moduleTypesPath}'
1005
1019
  ${serverOptionsLineTypes}
1006
- import type { GraphqlServerResponse } from '#graphql-middleware/types'
1007
- import type { GraphqlMiddlewareResponseUnion } from '#build/nuxt-graphql-middleware'
1020
+ import type { GraphqlServerResponse } from '${runtimeTypesPath}'
1021
+ import type { GraphqlMiddlewareResponseUnion } from '#nuxt-graphql-middleware/generated-types'
1008
1022
 
1009
1023
  type GraphqlResponseAdditions =
1010
- typeof serverOptions extends GraphqlMiddlewareServerOptions<infer R> ? R : {}
1024
+ typeof serverOptions extends GraphqlMiddlewareServerOptions<infer R, any, any> ? R : {}
1011
1025
 
1012
1026
  export type GraphqlResponse<T> = GraphqlServerResponse<T> & GraphqlResponseAdditions
1013
1027
 
@@ -1017,11 +1031,58 @@ export { serverOptions }
1017
1031
  `;
1018
1032
  }
1019
1033
  });
1034
+ const getClientOptionsImport = () => {
1035
+ const clientOptionsPath = appResolver.resolve(
1036
+ "graphqlMiddleware.clientOptions"
1037
+ );
1038
+ if (fileExists(clientOptionsPath)) {
1039
+ const pathRelative = relative(nuxt.options.buildDir, clientOptionsPath);
1040
+ return `import clientOptions from '${pathRelative}'`;
1041
+ }
1042
+ };
1043
+ const clientOptionsImport = getClientOptionsImport();
1044
+ const clientOptionsTemplate = addTemplate({
1045
+ filename: "graphqlMiddleware.clientOptions.mjs",
1046
+ write: true,
1047
+ getContents: () => {
1048
+ if (clientOptionsImport) {
1049
+ return `${clientOptionsImport}
1050
+ export { clientOptions }`;
1051
+ }
1052
+ return `export const clientOptions = {}`;
1053
+ }
1054
+ });
1055
+ const runtimeTypesPath = relative(
1056
+ nuxt.options.buildDir,
1057
+ moduleResolver.resolve("./runtime/types.ts")
1058
+ );
1059
+ addTemplate({
1060
+ filename: "graphqlMiddleware.clientOptions.d.ts",
1061
+ write: true,
1062
+ getContents: () => {
1063
+ if (clientOptionsImport) {
1064
+ return `import type { GraphqlClientOptions } from '${runtimeTypesPath}'
1065
+ ${clientOptionsImport}
1066
+
1067
+ export type GraphqlClientContext = typeof clientOptions extends GraphqlClientOptions<infer R> ? R : {}
1068
+
1069
+ export { clientOptions }`;
1070
+ }
1071
+ return `import type { GraphqlClientOptions } from '${runtimeTypesPath}'
1072
+ export const clientOptions: GraphqlClientOptions
1073
+
1074
+ export type GraphqlClientContext = {}
1075
+ `;
1076
+ }
1077
+ });
1078
+ nuxt.options.alias["#graphql-middleware-client-options"] = pathWithoutExtension(clientOptionsTemplate.dst);
1020
1079
  nuxt.options.nitro.externals = nuxt.options.nitro.externals || {};
1021
1080
  nuxt.options.nitro.externals.inline = nuxt.options.nitro.externals.inline || [];
1022
1081
  nuxt.options.nitro.externals.inline.push(template.dst);
1023
- nuxt.options.alias["#graphql-middleware-server-options-build"] = template.dst;
1024
- nuxt.options.alias["#graphql-middleware/types"] = moduleResolver.resolve("./runtime/types");
1082
+ nuxt.options.alias["#graphql-middleware-server-options-build"] = pathWithoutExtension(template.dst);
1083
+ nuxt.options.alias["#graphql-middleware/types"] = pathWithoutExtension(
1084
+ moduleResolver.resolve("./runtime/types.ts")
1085
+ );
1025
1086
  addServerHandler({
1026
1087
  handler: moduleResolver.resolve("./runtime/serverHandler/index"),
1027
1088
  route: options.serverApiPrefix + "/:operation/:name"
@@ -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,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) => {
@@ -1,11 +1,13 @@
1
1
  import type { FetchOptions } from 'ofetch';
2
- import type { GraphqlMiddlewareQuery, GraphqlMiddlewareMutation } from '#build/nuxt-graphql-middleware';
2
+ import type { GraphqlMiddlewareQuery, GraphqlMiddlewareMutation } from '#nuxt-graphql-middleware/generated-types';
3
3
  import type { RequestCacheOptions } from '#graphql-middleware/types';
4
+ import type { GraphqlClientContext } from '#graphql-middleware-client-options';
4
5
  export type GraphqlMiddlewareQueryName = keyof GraphqlMiddlewareQuery;
5
6
  export type GraphqlMiddlewareMutationName = keyof GraphqlMiddlewareMutation;
6
7
  export type GraphqlComposableOptions = {
7
8
  fetchOptions?: FetchOptions;
8
9
  graphqlCaching?: RequestCacheOptions;
10
+ clientContext?: Partial<GraphqlClientContext>;
9
11
  };
10
12
  export type GetQueryArgs<T extends GraphqlMiddlewareQueryName, M extends GraphqlMiddlewareQuery> = M[T][0] extends null ? [T, (null | undefined)?, GraphqlComposableOptions?] : M[T][1] extends false ? [T, M[T][0], GraphqlComposableOptions?] : [T, M[T][0]?, GraphqlComposableOptions?];
11
13
  export type GetMutationArgs<T extends GraphqlMiddlewareMutationName, M extends GraphqlMiddlewareMutation> = M[T][0] extends null ? [T, (null | undefined)?, GraphqlComposableOptions?] : M[T][1] extends false ? [T, M[T][0], GraphqlComposableOptions?] : [T, M[T][0]?, GraphqlComposableOptions?];
@@ -16,21 +18,26 @@ export type QueryObjectArgs<T extends GraphqlMiddlewareQueryName, M extends Grap
16
18
  name: T;
17
19
  fetchOptions?: FetchOptions;
18
20
  graphqlCaching?: RequestCacheOptions;
21
+ clientContext?: Partial<GraphqlClientContext>;
19
22
  variables?: null;
20
23
  } : {
21
24
  name: T;
22
25
  variables: M[T][0];
23
26
  fetchOptions?: FetchOptions;
24
27
  graphqlCaching?: RequestCacheOptions;
28
+ clientContext?: Partial<GraphqlClientContext>;
25
29
  };
26
30
  export type MutationObjectArgs<T extends GraphqlMiddlewareMutationName, M extends GraphqlMiddlewareMutation> = M[T][0] extends null ? {
27
31
  name: T;
28
32
  variables?: null;
29
33
  fetchOptions?: FetchOptions;
34
+ clientContext?: Partial<GraphqlClientContext>;
30
35
  } : {
31
36
  name: T;
32
37
  variables: M[T][0];
33
38
  fetchOptions?: FetchOptions;
39
+ clientContext?: Partial<GraphqlClientContext>;
34
40
  };
35
41
  export type PickFrom<T, K extends Array<string>> = T extends Array<any> ? T : T extends Record<string, any> ? keyof T extends K[number] ? T : K[number] extends never ? T : Pick<T, K[number]> : T;
36
42
  export type KeysOf<T> = Array<T extends T ? (keyof T extends string ? keyof T : never) : never>;
43
+ export declare function encodeContext(context: Record<string, string | null | undefined>): Record<string, string>;
@@ -0,0 +1,17 @@
1
+ import { useRuntimeConfig } from "#imports";
2
+ import { CLIENT_CONTEXT_PREFIX } from "../settings/index.js";
3
+ export function getEndpoint(operation, operationName) {
4
+ const config = useRuntimeConfig();
5
+ return `${config?.public?.["nuxt-graphql-middleware"]?.serverApiPrefix}/${operation}/${operationName}`;
6
+ }
7
+ export function encodeContext(context) {
8
+ return Object.entries(context).reduce(
9
+ (acc, [key, value]) => {
10
+ if (typeof value === "string") {
11
+ acc[CLIENT_CONTEXT_PREFIX + key] = value;
12
+ }
13
+ return acc;
14
+ },
15
+ {}
16
+ );
17
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "extends": "../../../.nuxt/tsconfig.server.json"
3
+ }
@@ -0,0 +1,3 @@
1
+ import type { FetchOptions } from 'ofetch';
2
+ import type { GraphqlResponse } from '#graphql-middleware-server-options-build';
3
+ export declare function performRequest<T>(operation: string, operationName: string, method: 'get' | 'post', options: FetchOptions): Promise<GraphqlResponse<T>>;
@@ -0,0 +1,12 @@
1
+ import { getEndpoint } from "./../../helpers/composables.js";
2
+ export function performRequest(operation, operationName, method, options) {
3
+ return $fetch(getEndpoint(operation, operationName), {
4
+ ...options,
5
+ method
6
+ }).then((v) => {
7
+ return {
8
+ data: v.data,
9
+ errors: v.errors || []
10
+ };
11
+ });
12
+ }
@@ -0,0 +1,7 @@
1
+ import type { GraphqlResponse } from '#graphql-middleware-server-options-build';
2
+ import { type GraphqlMiddlewareMutationName, type GetMutationArgs, type MutationObjectArgs, type GetMutationResult } from './../../helpers/composables.js';
3
+ import type { GraphqlMiddlewareMutation } from '#nuxt-graphql-middleware/generated-types';
4
+ /**
5
+ * Performs a GraphQL mutation.
6
+ */
7
+ export declare function useGraphqlMutation<T extends GraphqlMiddlewareMutationName, R extends GetMutationResult<T, GraphqlMiddlewareMutation>>(...args: GetMutationArgs<T, GraphqlMiddlewareMutation> | [MutationObjectArgs<T, GraphqlMiddlewareMutation>]): Promise<GraphqlResponse<R>>;
@@ -0,0 +1,20 @@
1
+ import {
2
+ encodeContext
3
+ } from "./../../helpers/composables.js";
4
+ import { performRequest } from "./index.js";
5
+ export function useGraphqlMutation(...args) {
6
+ const [name, body, fetchOptions = {}, clientContext = {}] = typeof args[0] === "string" ? [args[0], args[1], args[2]?.fetchOptions, args[2]?.clientContext] : [
7
+ args[0].name,
8
+ args[0].variables,
9
+ args[0].fetchOptions,
10
+ args[0].clientContext
11
+ ];
12
+ return performRequest("mutation", name, "post", {
13
+ ...fetchOptions,
14
+ body,
15
+ params: {
16
+ ...encodeContext(clientContext),
17
+ ...fetchOptions.params || {}
18
+ }
19
+ });
20
+ }
@@ -0,0 +1,7 @@
1
+ import type { GraphqlResponse } from '#graphql-middleware-server-options-build';
2
+ import { type GraphqlMiddlewareQueryName, type GetQueryArgs, type QueryObjectArgs, type GetQueryResult } from './../../helpers/composables.js';
3
+ import type { GraphqlMiddlewareQuery } from '#nuxt-graphql-middleware/generated-types';
4
+ /**
5
+ * Performs a GraphQL query.
6
+ */
7
+ export declare function useGraphqlQuery<T extends GraphqlMiddlewareQueryName, R extends GetQueryResult<T, GraphqlMiddlewareQuery>>(...args: GetQueryArgs<T, GraphqlMiddlewareQuery> | [QueryObjectArgs<T, GraphqlMiddlewareQuery>]): Promise<GraphqlResponse<R>>;
@@ -0,0 +1,21 @@
1
+ import {
2
+ encodeContext
3
+ } from "./../../helpers/composables.js";
4
+ import { buildRequestParams } from "./../../helpers/index.js";
5
+ import { performRequest } from "./index.js";
6
+ export function useGraphqlQuery(...args) {
7
+ const [name, variables, fetchOptions = {}, clientContext = {}] = 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
+ return performRequest("query", name, "get", {
14
+ ...fetchOptions,
15
+ params: {
16
+ ...fetchOptions.params || {},
17
+ ...buildRequestParams(variables),
18
+ ...encodeContext(clientContext)
19
+ }
20
+ });
21
+ }