@shopify/hydrogen-codegen 0.2.1 → 0.2.2

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.
@@ -1,26 +1,37 @@
1
1
  'use strict';
2
2
 
3
- var plugin_js = require('./plugin.cjs');
4
-
3
+ const QUERIES_PLACEHOLDER = "%queries%";
4
+ const MUTATIONS_PLACEHOLDER = "%mutations%";
5
5
  const sfapiDefaultInterfaceExtensionCode = `
6
6
  declare module '@shopify/hydrogen' {
7
- interface StorefrontQueries extends ${plugin_js.GENERATED_QUERY_INTERFACE_NAME} {}
8
- interface StorefrontMutations extends ${plugin_js.GENERATED_MUTATION_INTERFACE_NAME} {}
7
+ interface StorefrontQueries extends ${QUERIES_PLACEHOLDER} {}
8
+ interface StorefrontMutations extends ${MUTATIONS_PLACEHOLDER} {}
9
9
  }`;
10
10
  const caapiDefaultInterfaceExtensionCode = `
11
11
  declare module '@shopify/hydrogen' {
12
- interface CustomerAccountQueries extends ${plugin_js.GENERATED_QUERY_INTERFACE_NAME} {}
13
- interface CustomerAccountMutations extends ${plugin_js.GENERATED_MUTATION_INTERFACE_NAME} {}
12
+ interface CustomerAccountQueries extends ${QUERIES_PLACEHOLDER} {}
13
+ interface CustomerAccountMutations extends ${MUTATIONS_PLACEHOLDER} {}
14
14
  }`;
15
+ function replacePlaceholders(code, queryType, mutationType) {
16
+ return code.replace(QUERIES_PLACEHOLDER, queryType).replace(MUTATIONS_PLACEHOLDER, mutationType);
17
+ }
15
18
  const sfapiDefaultValues = {
16
19
  importTypesFrom: "@shopify/hydrogen/storefront-api-types",
17
20
  namespacedImportName: "StorefrontAPI",
18
- interfaceExtensionCode: sfapiDefaultInterfaceExtensionCode
21
+ interfaceExtensionCode: ({ queryType, mutationType }) => replacePlaceholders(
22
+ sfapiDefaultInterfaceExtensionCode,
23
+ queryType,
24
+ mutationType
25
+ )
19
26
  };
20
27
  const caapiDefaultValues = {
21
28
  importTypesFrom: "@shopify/hydrogen/customer-account-api-types",
22
29
  namespacedImportName: "CustomerAccountAPI",
23
- interfaceExtensionCode: caapiDefaultInterfaceExtensionCode
30
+ interfaceExtensionCode: ({ queryType, mutationType }) => replacePlaceholders(
31
+ caapiDefaultInterfaceExtensionCode,
32
+ queryType,
33
+ mutationType
34
+ )
24
35
  };
25
36
  function getDefaultOptions(outputFile = "") {
26
37
  return /^(customer|caapi\.)/i.test(outputFile) ? caapiDefaultValues : sfapiDefaultValues;
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/defaults.ts"],"names":[],"mappings":"AAAA;AAAA,EACE;AAAA,EACA;AAAA,OACK;AAEP,MAAM,qCAAqC;AAAA;AAAA,wCAEH,8BAA8B;AAAA,0CAC5B,iCAAiC;AAAA;AAG3E,MAAM,qCAAqC;AAAA;AAAA,6CAEE,8BAA8B;AAAA,+CAC5B,iCAAiC;AAAA;AAShF,MAAM,qBAAoC;AAAA,EACxC,iBAAiB;AAAA,EACjB,sBAAsB;AAAA,EACtB,wBAAwB;AAC1B;AAEA,MAAM,qBAAoC;AAAA,EACxC,iBAAiB;AAAA,EACjB,sBAAsB;AAAA,EACtB,wBAAwB;AAC1B;AAEO,SAAS,kBAAkB,aAAa,IAAI;AACjD,SAAO,uBAAuB,KAAK,UAAU,IACzC,qBACA;AACN","sourcesContent":["import {\n GENERATED_MUTATION_INTERFACE_NAME,\n GENERATED_QUERY_INTERFACE_NAME,\n} from './plugin.js';\n\nconst sfapiDefaultInterfaceExtensionCode = `\ndeclare module '@shopify/hydrogen' {\n interface StorefrontQueries extends ${GENERATED_QUERY_INTERFACE_NAME} {}\n interface StorefrontMutations extends ${GENERATED_MUTATION_INTERFACE_NAME} {}\n}`;\n\nconst caapiDefaultInterfaceExtensionCode = `\ndeclare module '@shopify/hydrogen' {\n interface CustomerAccountQueries extends ${GENERATED_QUERY_INTERFACE_NAME} {}\n interface CustomerAccountMutations extends ${GENERATED_MUTATION_INTERFACE_NAME} {}\n}`;\n\ntype DefaultValues = {\n importTypesFrom: string;\n namespacedImportName: string;\n interfaceExtensionCode: string;\n};\n\nconst sfapiDefaultValues: DefaultValues = {\n importTypesFrom: '@shopify/hydrogen/storefront-api-types',\n namespacedImportName: 'StorefrontAPI',\n interfaceExtensionCode: sfapiDefaultInterfaceExtensionCode,\n};\n\nconst caapiDefaultValues: DefaultValues = {\n importTypesFrom: '@shopify/hydrogen/customer-account-api-types',\n namespacedImportName: 'CustomerAccountAPI',\n interfaceExtensionCode: caapiDefaultInterfaceExtensionCode,\n};\n\nexport function getDefaultOptions(outputFile = '') {\n return /^(customer|caapi\\.)/i.test(outputFile)\n ? caapiDefaultValues\n : sfapiDefaultValues;\n}\n"]}
1
+ {"version":3,"sources":["../../src/defaults.ts"],"names":[],"mappings":"AAEA,MAAM,sBAAsB;AAC5B,MAAM,wBAAwB;AAE9B,MAAM,qCAAqC;AAAA;AAAA,wCAEH,mBAAmB;AAAA,0CACjB,qBAAqB;AAAA;AAG/D,MAAM,qCAAqC;AAAA;AAAA,6CAEE,mBAAmB;AAAA,+CACjB,qBAAqB;AAAA;AAGpE,SAAS,oBACP,MACA,WACA,cACA;AACA,SAAO,KACJ,QAAQ,qBAAqB,SAAS,EACtC,QAAQ,uBAAuB,YAAY;AAChD;AAQA,MAAM,qBAAoC;AAAA,EACxC,iBAAiB;AAAA,EACjB,sBAAsB;AAAA,EACtB,wBAAwB,CAAC,EAAC,WAAW,aAAY,MAC/C;AAAA,IACE;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACJ;AAEA,MAAM,qBAAoC;AAAA,EACxC,iBAAiB;AAAA,EACjB,sBAAsB;AAAA,EACtB,wBAAwB,CAAC,EAAC,WAAW,aAAY,MAC/C;AAAA,IACE;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACJ;AAEO,SAAS,kBAAkB,aAAa,IAAI;AACjD,SAAO,uBAAuB,KAAK,UAAU,IACzC,qBACA;AACN","sourcesContent":["import type {PresetConfig} from './preset';\n\nconst QUERIES_PLACEHOLDER = '%queries%';\nconst MUTATIONS_PLACEHOLDER = '%mutations%';\n\nconst sfapiDefaultInterfaceExtensionCode = `\ndeclare module '@shopify/hydrogen' {\n interface StorefrontQueries extends ${QUERIES_PLACEHOLDER} {}\n interface StorefrontMutations extends ${MUTATIONS_PLACEHOLDER} {}\n}`;\n\nconst caapiDefaultInterfaceExtensionCode = `\ndeclare module '@shopify/hydrogen' {\n interface CustomerAccountQueries extends ${QUERIES_PLACEHOLDER} {}\n interface CustomerAccountMutations extends ${MUTATIONS_PLACEHOLDER} {}\n}`;\n\nfunction replacePlaceholders(\n code: string,\n queryType: string,\n mutationType: string,\n) {\n return code\n .replace(QUERIES_PLACEHOLDER, queryType)\n .replace(MUTATIONS_PLACEHOLDER, mutationType);\n}\n\ntype DefaultValues = {\n importTypesFrom: string;\n namespacedImportName: string;\n interfaceExtensionCode: NonNullable<PresetConfig['interfaceExtension']>;\n};\n\nconst sfapiDefaultValues: DefaultValues = {\n importTypesFrom: '@shopify/hydrogen/storefront-api-types',\n namespacedImportName: 'StorefrontAPI',\n interfaceExtensionCode: ({queryType, mutationType}) =>\n replacePlaceholders(\n sfapiDefaultInterfaceExtensionCode,\n queryType,\n mutationType,\n ),\n};\n\nconst caapiDefaultValues: DefaultValues = {\n importTypesFrom: '@shopify/hydrogen/customer-account-api-types',\n namespacedImportName: 'CustomerAccountAPI',\n interfaceExtensionCode: ({queryType, mutationType}) =>\n replacePlaceholders(\n caapiDefaultInterfaceExtensionCode,\n queryType,\n mutationType,\n ),\n};\n\nexport function getDefaultOptions(outputFile = '') {\n return /^(customer|caapi\\.)/i.test(outputFile)\n ? caapiDefaultValues\n : sfapiDefaultValues;\n}\n"]}
@@ -1,10 +1,8 @@
1
1
  'use strict';
2
2
 
3
3
  var preset_js = require('./preset.cjs');
4
- var plugin_js = require('./plugin.cjs');
5
4
  var schema_js = require('./schema.cjs');
6
- var sources_js = require('./sources.cjs');
7
- var pluck_js = require('./pluck.cjs');
5
+ var graphqlCodegen = require('@shopify/graphql-codegen');
8
6
 
9
7
 
10
8
 
@@ -12,10 +10,6 @@ Object.defineProperty(exports, 'preset', {
12
10
  enumerable: true,
13
11
  get: function () { return preset_js.preset; }
14
12
  });
15
- Object.defineProperty(exports, 'plugin', {
16
- enumerable: true,
17
- get: function () { return plugin_js.plugin; }
18
- });
19
13
  Object.defineProperty(exports, 'getSchema', {
20
14
  enumerable: true,
21
15
  get: function () { return schema_js.getSchema; }
@@ -24,13 +18,17 @@ Object.defineProperty(exports, 'schema', {
24
18
  enumerable: true,
25
19
  get: function () { return schema_js.schema; }
26
20
  });
27
- Object.defineProperty(exports, 'processSources', {
21
+ Object.defineProperty(exports, 'pluckConfig', {
28
22
  enumerable: true,
29
- get: function () { return sources_js.processSources; }
23
+ get: function () { return graphqlCodegen.pluckConfig; }
30
24
  });
31
- Object.defineProperty(exports, 'pluckConfig', {
25
+ Object.defineProperty(exports, 'plugin', {
26
+ enumerable: true,
27
+ get: function () { return graphqlCodegen.plugin; }
28
+ });
29
+ Object.defineProperty(exports, 'processSources', {
32
30
  enumerable: true,
33
- get: function () { return pluck_js.pluckConfig; }
31
+ get: function () { return graphqlCodegen.processSources; }
34
32
  });
35
33
  //# sourceMappingURL=out.js.map
36
34
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,SAAQ,cAAa;AACrB,SAAQ,cAAa;AACrB,SAAQ,QAAQ,iBAAgB;AAChC,SAAQ,sBAAqB;AAC7B,SAAQ,mBAAkB","sourcesContent":["export {preset} from './preset.js';\nexport {plugin} from './plugin.js';\nexport {schema, getSchema} from './schema.js';\nexport {processSources} from './sources.js';\nexport {pluckConfig} from './pluck.js';\nexport type * from './client.js';\n"]}
1
+ {"version":3,"sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,SAAQ,cAAgC;AACxC,SAAQ,QAAQ,iBAAgB;AAChC;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OAOK","sourcesContent":["export {preset, type PresetConfig} from './preset.js';\nexport {schema, getSchema} from './schema.js';\nexport {\n plugin,\n pluckConfig,\n processSources,\n type GenericVariables,\n type EmptyVariables,\n type ClientReturn,\n type IsOptionalVariables,\n type ClientVariables,\n type ClientVariablesInRestParams,\n} from '@shopify/graphql-codegen';\n"]}
@@ -1,119 +1,32 @@
1
1
  'use strict';
2
2
 
3
- var addPlugin = require('@graphql-codegen/add');
4
- var typescriptPlugin = require('@graphql-codegen/typescript');
5
- var typescriptOperationPlugin = require('@graphql-codegen/typescript-operations');
6
- var sources_js = require('./sources.cjs');
3
+ var graphqlCodegen = require('@shopify/graphql-codegen');
7
4
  var defaults_js = require('./defaults.cjs');
8
- var plugin_js = require('./plugin.cjs');
9
-
10
- function _interopNamespace(e) {
11
- if (e && e.__esModule) return e;
12
- var n = Object.create(null);
13
- if (e) {
14
- Object.keys(e).forEach(function (k) {
15
- if (k !== 'default') {
16
- var d = Object.getOwnPropertyDescriptor(e, k);
17
- Object.defineProperty(n, k, d.get ? d : {
18
- enumerable: true,
19
- get: function () { return e[k]; }
20
- });
21
- }
22
- });
23
- }
24
- n.default = e;
25
- return Object.freeze(n);
26
- }
27
-
28
- var addPlugin__namespace = /*#__PURE__*/_interopNamespace(addPlugin);
29
- var typescriptPlugin__namespace = /*#__PURE__*/_interopNamespace(typescriptPlugin);
30
- var typescriptOperationPlugin__namespace = /*#__PURE__*/_interopNamespace(typescriptOperationPlugin);
31
5
 
32
6
  const preset = {
33
7
  [Symbol.for("name")]: "hydrogen",
34
8
  buildGeneratesSection: (options) => {
35
- if (!options.baseOutputDir.endsWith(".d.ts")) {
36
- throw new Error("[hydrogen-preset] target output should be a .d.ts file");
37
- }
38
- if (options.plugins?.length > 0 && Object.keys(options.plugins).some((p) => p.startsWith("typescript"))) {
39
- throw new Error(
40
- "[hydrogen-preset] providing additional typescript-based `plugins` leads to duplicated generated types"
9
+ try {
10
+ const defaultOptions = defaults_js.getDefaultOptions(options.baseOutputDir);
11
+ return graphqlCodegen.preset.buildGeneratesSection({
12
+ ...options,
13
+ presetConfig: {
14
+ importTypes: {
15
+ namespace: defaultOptions.namespacedImportName,
16
+ from: defaultOptions.importTypesFrom
17
+ },
18
+ interfaceExtension: defaultOptions.interfaceExtensionCode,
19
+ ...options.presetConfig
20
+ }
21
+ });
22
+ } catch (err) {
23
+ const error = err;
24
+ error.message = error.message.replace(
25
+ "[@shopify/graphql-codegen]",
26
+ "[hydrogen-preset]"
41
27
  );
28
+ throw error;
42
29
  }
43
- const sourcesWithOperations = sources_js.processSources(options.documents);
44
- const sources = sourcesWithOperations.map(({ source }) => source);
45
- const defaultOptions = defaults_js.getDefaultOptions(options.baseOutputDir);
46
- const importTypes = options.presetConfig.importTypes ?? true;
47
- const namespacedImportName = options.presetConfig.namespacedImportName ?? defaultOptions.namespacedImportName;
48
- const importTypesFrom = options.presetConfig.importTypesFrom ?? defaultOptions.importTypesFrom;
49
- const interfaceExtensionCode = options.presetConfig.interfaceExtension?.({
50
- queryType: plugin_js.GENERATED_QUERY_INTERFACE_NAME,
51
- mutationType: plugin_js.GENERATED_MUTATION_INTERFACE_NAME
52
- }) ?? defaultOptions.interfaceExtensionCode;
53
- const pluginMap = {
54
- ...options.pluginMap,
55
- [`add`]: addPlugin__namespace,
56
- [`typescript`]: typescriptPlugin__namespace,
57
- [`typescript-operations`]: typescriptOperationPlugin__namespace,
58
- [`gen-dts`]: { plugin: plugin_js.plugin }
59
- };
60
- const plugins = [
61
- // 1. Disable eslint for the generated file
62
- {
63
- [`add`]: {
64
- content: `/* eslint-disable eslint-comments/disable-enable-pair */
65
- /* eslint-disable eslint-comments/no-unlimited-disable */
66
- /* eslint-disable */`
67
- }
68
- },
69
- // 2. Import all the generated API types from Hydrogen or generate all the types from the schema.
70
- importTypes ? {
71
- [`add`]: {
72
- content: `import * as ${namespacedImportName} from '${importTypesFrom}';
73
- `
74
- }
75
- } : {
76
- [`typescript`]: {
77
- useTypeImports: true,
78
- useImplementingTypes: true,
79
- enumsAsTypes: true
80
- }
81
- },
82
- // 3. Generate the operations (i.e. queries, mutations, and fragments types)
83
- {
84
- [`typescript-operations`]: {
85
- useTypeImports: true,
86
- // Use `import type` instead of `import`
87
- preResolveTypes: false,
88
- // Use Pick<...> instead of primitives
89
- mergeFragmentTypes: true,
90
- // Merge equal fragment interfaces. Avoids adding `| {}` to Metaobject
91
- skipTypename: options.presetConfig.skipTypenameInOperations ?? true,
92
- // Skip __typename fields
93
- namespacedImportName: importTypes ? namespacedImportName : void 0
94
- }
95
- },
96
- // 4. Augment Hydrogen query/mutation interfaces with the generated operations
97
- { [`gen-dts`]: { sourcesWithOperations, interfaceExtensionCode } },
98
- // 5. Custom plugins from the user
99
- ...options.plugins
100
- ];
101
- return [
102
- {
103
- filename: options.baseOutputDir,
104
- plugins,
105
- pluginMap,
106
- schema: options.schema,
107
- config: {
108
- // For the TS plugin:
109
- defaultScalarType: "unknown",
110
- // Allow overwriting defaults:
111
- ...options.config
112
- },
113
- documents: sources,
114
- documentTransforms: options.documentTransforms
115
- }
116
- ];
117
30
  }
118
31
  };
119
32
 
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/preset.ts"],"names":[],"mappings":"AACA,YAAY,eAAe;AAC3B,YAAY,sBAAsB;AAClC,YAAY,+BAA+B;AAC3C,SAAQ,sBAAqB;AAC7B,SAAQ,yBAAwB;AAChC;AAAA,EACE,UAAU;AAAA,EACV;AAAA,EACA;AAAA,OACK;AAgCA,MAAM,SAAmD;AAAA,EAC9D,CAAC,OAAO,IAAI,MAAM,CAAC,GAAG;AAAA,EACtB,uBAAuB,CAAC,YAAY;AAClC,QAAI,CAAC,QAAQ,cAAc,SAAS,OAAO,GAAG;AAC5C,YAAM,IAAI,MAAM,wDAAwD;AAAA,IAC1E;AAEA,QACE,QAAQ,SAAS,SAAS,KAC1B,OAAO,KAAK,QAAQ,OAAO,EAAE,KAAK,CAAC,MAAM,EAAE,WAAW,YAAY,CAAC,GACnE;AACA,YAAM,IAAI;AAAA,QACR;AAAA,MACF;AAAA,IACF;AAEA,UAAM,wBAAwB,eAAe,QAAQ,SAAS;AAC9D,UAAM,UAAU,sBAAsB,IAAI,CAAC,EAAC,OAAM,MAAM,MAAM;AAE9D,UAAM,iBAAiB,kBAAkB,QAAQ,aAAa;AAE9D,UAAM,cAAc,QAAQ,aAAa,eAAe;AACxD,UAAM,uBACJ,QAAQ,aAAa,wBACrB,eAAe;AACjB,UAAM,kBACJ,QAAQ,aAAa,mBAAmB,eAAe;AAEzD,UAAM,yBACJ,QAAQ,aAAa,qBAAqB;AAAA,MACxC,WAAW;AAAA,MACX,cAAc;AAAA,IAChB,CAAC,KAAK,eAAe;AAEvB,UAAM,YAAY;AAAA,MAChB,GAAG,QAAQ;AAAA,MACX,CAAC,KAAK,GAAG;AAAA,MACT,CAAC,YAAY,GAAG;AAAA,MAChB,CAAC,uBAAuB,GAAG;AAAA,MAC3B,CAAC,SAAS,GAAG,EAAC,QAAQ,UAAS;AAAA,IACjC;AAEA,UAAM,UAAyC;AAAA;AAAA,MAE7C;AAAA,QACE,CAAC,KAAK,GAAG;AAAA,UACP,SAAS;AAAA;AAAA;AAAA,QACX;AAAA,MACF;AAAA;AAAA,MAEA,cACI;AAAA,QACE,CAAC,KAAK,GAAG;AAAA,UACP,SAAS,eAAe,oBAAoB,UAAU,eAAe;AAAA;AAAA,QACvE;AAAA,MACF,IACA;AAAA,QACE,CAAC,YAAY,GAAG;AAAA,UACd,gBAAgB;AAAA,UAChB,sBAAsB;AAAA,UACtB,cAAc;AAAA,QAChB;AAAA,MACF;AAAA;AAAA,MAEJ;AAAA,QACE,CAAC,uBAAuB,GAAG;AAAA,UACzB,gBAAgB;AAAA;AAAA,UAChB,iBAAiB;AAAA;AAAA,UACjB,oBAAoB;AAAA;AAAA,UACpB,cAAc,QAAQ,aAAa,4BAA4B;AAAA;AAAA,UAC/D,sBAAsB,cAAc,uBAAuB;AAAA,QAC7D;AAAA,MACF;AAAA;AAAA,MAEA,EAAC,CAAC,SAAS,GAAG,EAAC,uBAAuB,uBAAsB,EAAC;AAAA;AAAA,MAE7D,GAAG,QAAQ;AAAA,IACb;AAEA,WAAO;AAAA,MACL;AAAA,QACE,UAAU,QAAQ;AAAA,QAClB;AAAA,QACA;AAAA,QACA,QAAQ,QAAQ;AAAA,QAChB,QAAQ;AAAA;AAAA,UAEN,mBAAmB;AAAA;AAAA,UAEnB,GAAG,QAAQ;AAAA,QACb;AAAA,QACA,WAAW;AAAA,QACX,oBAAoB,QAAQ;AAAA,MAC9B;AAAA,IACF;AAAA,EACF;AACF","sourcesContent":["import type {Types} from '@graphql-codegen/plugin-helpers';\nimport * as addPlugin from '@graphql-codegen/add';\nimport * as typescriptPlugin from '@graphql-codegen/typescript';\nimport * as typescriptOperationPlugin from '@graphql-codegen/typescript-operations';\nimport {processSources} from './sources.js';\nimport {getDefaultOptions} from './defaults.js';\nimport {\n plugin as dtsPlugin,\n GENERATED_MUTATION_INTERFACE_NAME,\n GENERATED_QUERY_INTERFACE_NAME,\n} from './plugin.js';\n\nexport type HydrogenPresetConfig = {\n /**\n * Name for the variable that contains the imported types.\n * @default 'StorefrontAPI'\n */\n namespacedImportName?: string;\n /**\n * Module to import the types from.\n * @default '@shopify/hydrogen/storefront-api-types'\n */\n importTypesFrom?: string;\n /**\n * Whether types should be imported from the `importTypesFrom` module, or generated inline.\n * @default true\n */\n importTypes?: boolean;\n /**\n * Whether to skip adding `__typename` to generated operation types.\n * @default true\n */\n skipTypenameInOperations?: boolean;\n /**\n * Override the default interface extension.\n */\n interfaceExtension?: (options: {\n queryType: string;\n mutationType: string;\n }) => string;\n};\n\nexport const preset: Types.OutputPreset<HydrogenPresetConfig> = {\n [Symbol.for('name')]: 'hydrogen',\n buildGeneratesSection: (options) => {\n if (!options.baseOutputDir.endsWith('.d.ts')) {\n throw new Error('[hydrogen-preset] target output should be a .d.ts file');\n }\n\n if (\n options.plugins?.length > 0 &&\n Object.keys(options.plugins).some((p) => p.startsWith('typescript'))\n ) {\n throw new Error(\n '[hydrogen-preset] providing additional typescript-based `plugins` leads to duplicated generated types',\n );\n }\n\n const sourcesWithOperations = processSources(options.documents);\n const sources = sourcesWithOperations.map(({source}) => source);\n\n const defaultOptions = getDefaultOptions(options.baseOutputDir);\n\n const importTypes = options.presetConfig.importTypes ?? true;\n const namespacedImportName =\n options.presetConfig.namespacedImportName ??\n defaultOptions.namespacedImportName;\n const importTypesFrom =\n options.presetConfig.importTypesFrom ?? defaultOptions.importTypesFrom;\n\n const interfaceExtensionCode =\n options.presetConfig.interfaceExtension?.({\n queryType: GENERATED_QUERY_INTERFACE_NAME,\n mutationType: GENERATED_MUTATION_INTERFACE_NAME,\n }) ?? defaultOptions.interfaceExtensionCode;\n\n const pluginMap = {\n ...options.pluginMap,\n [`add`]: addPlugin,\n [`typescript`]: typescriptPlugin,\n [`typescript-operations`]: typescriptOperationPlugin,\n [`gen-dts`]: {plugin: dtsPlugin},\n };\n\n const plugins: Array<Types.ConfiguredPlugin> = [\n // 1. Disable eslint for the generated file\n {\n [`add`]: {\n content: `/* eslint-disable eslint-comments/disable-enable-pair */\\n/* eslint-disable eslint-comments/no-unlimited-disable */\\n/* eslint-disable */`,\n },\n },\n // 2. Import all the generated API types from Hydrogen or generate all the types from the schema.\n importTypes\n ? {\n [`add`]: {\n content: `import * as ${namespacedImportName} from '${importTypesFrom}';\\n`,\n },\n }\n : {\n [`typescript`]: {\n useTypeImports: true,\n useImplementingTypes: true,\n enumsAsTypes: true,\n },\n },\n // 3. Generate the operations (i.e. queries, mutations, and fragments types)\n {\n [`typescript-operations`]: {\n useTypeImports: true, // Use `import type` instead of `import`\n preResolveTypes: false, // Use Pick<...> instead of primitives\n mergeFragmentTypes: true, // Merge equal fragment interfaces. Avoids adding `| {}` to Metaobject\n skipTypename: options.presetConfig.skipTypenameInOperations ?? true, // Skip __typename fields\n namespacedImportName: importTypes ? namespacedImportName : undefined,\n },\n },\n // 4. Augment Hydrogen query/mutation interfaces with the generated operations\n {[`gen-dts`]: {sourcesWithOperations, interfaceExtensionCode}},\n // 5. Custom plugins from the user\n ...options.plugins,\n ];\n\n return [\n {\n filename: options.baseOutputDir,\n plugins,\n pluginMap,\n schema: options.schema,\n config: {\n // For the TS plugin:\n defaultScalarType: 'unknown',\n // Allow overwriting defaults:\n ...options.config,\n },\n documents: sources,\n documentTransforms: options.documentTransforms,\n },\n ];\n },\n};\n"]}
1
+ {"version":3,"sources":["../../src/preset.ts"],"names":[],"mappings":"AACA;AAAA,EACE,UAAU;AAAA,OAEL;AACP,SAAQ,yBAAwB;AAIzB,MAAM,SAA2C;AAAA,EACtD,CAAC,OAAO,IAAI,MAAM,CAAC,GAAG;AAAA,EACtB,uBAAuB,CAAC,YAAY;AAClC,QAAI;AACF,YAAM,iBAAiB,kBAAkB,QAAQ,aAAa;AAE9D,aAAO,eAAe,sBAAsB;AAAA,QAC1C,GAAG;AAAA,QACH,cAAc;AAAA,UACZ,aAAa;AAAA,YACX,WAAW,eAAe;AAAA,YAC1B,MAAM,eAAe;AAAA,UACvB;AAAA,UACA,oBAAoB,eAAe;AAAA,UACnC,GAAG,QAAQ;AAAA,QACb;AAAA,MACF,CAAC;AAAA,IACH,SAAS,KAAK;AACZ,YAAM,QAAQ;AAEd,YAAM,UAAU,MAAM,QAAQ;AAAA,QAC5B;AAAA,QACA;AAAA,MACF;AAEA,YAAM;AAAA,IACR;AAAA,EACF;AACF","sourcesContent":["import type {Types} from '@graphql-codegen/plugin-helpers';\nimport {\n preset as internalPreset,\n type PresetConfig as InternalPresetConfig,\n} from '@shopify/graphql-codegen';\nimport {getDefaultOptions} from './defaults.js';\n\nexport type PresetConfig = Partial<InternalPresetConfig>;\n\nexport const preset: Types.OutputPreset<PresetConfig> = {\n [Symbol.for('name')]: 'hydrogen',\n buildGeneratesSection: (options) => {\n try {\n const defaultOptions = getDefaultOptions(options.baseOutputDir);\n\n return internalPreset.buildGeneratesSection({\n ...options,\n presetConfig: {\n importTypes: {\n namespace: defaultOptions.namespacedImportName,\n from: defaultOptions.importTypesFrom,\n },\n interfaceExtension: defaultOptions.interfaceExtensionCode,\n ...options.presetConfig,\n } satisfies PresetConfig,\n });\n } catch (err) {\n const error = err as Error;\n\n error.message = error.message.replace(\n '[@shopify/graphql-codegen]',\n '[hydrogen-preset]',\n );\n\n throw error;\n }\n },\n};\n"]}
@@ -1,24 +1,35 @@
1
- import { GENERATED_QUERY_INTERFACE_NAME, GENERATED_MUTATION_INTERFACE_NAME } from './plugin.js';
2
-
1
+ const QUERIES_PLACEHOLDER = "%queries%";
2
+ const MUTATIONS_PLACEHOLDER = "%mutations%";
3
3
  const sfapiDefaultInterfaceExtensionCode = `
4
4
  declare module '@shopify/hydrogen' {
5
- interface StorefrontQueries extends ${GENERATED_QUERY_INTERFACE_NAME} {}
6
- interface StorefrontMutations extends ${GENERATED_MUTATION_INTERFACE_NAME} {}
5
+ interface StorefrontQueries extends ${QUERIES_PLACEHOLDER} {}
6
+ interface StorefrontMutations extends ${MUTATIONS_PLACEHOLDER} {}
7
7
  }`;
8
8
  const caapiDefaultInterfaceExtensionCode = `
9
9
  declare module '@shopify/hydrogen' {
10
- interface CustomerAccountQueries extends ${GENERATED_QUERY_INTERFACE_NAME} {}
11
- interface CustomerAccountMutations extends ${GENERATED_MUTATION_INTERFACE_NAME} {}
10
+ interface CustomerAccountQueries extends ${QUERIES_PLACEHOLDER} {}
11
+ interface CustomerAccountMutations extends ${MUTATIONS_PLACEHOLDER} {}
12
12
  }`;
13
+ function replacePlaceholders(code, queryType, mutationType) {
14
+ return code.replace(QUERIES_PLACEHOLDER, queryType).replace(MUTATIONS_PLACEHOLDER, mutationType);
15
+ }
13
16
  const sfapiDefaultValues = {
14
17
  importTypesFrom: "@shopify/hydrogen/storefront-api-types",
15
18
  namespacedImportName: "StorefrontAPI",
16
- interfaceExtensionCode: sfapiDefaultInterfaceExtensionCode
19
+ interfaceExtensionCode: ({ queryType, mutationType }) => replacePlaceholders(
20
+ sfapiDefaultInterfaceExtensionCode,
21
+ queryType,
22
+ mutationType
23
+ )
17
24
  };
18
25
  const caapiDefaultValues = {
19
26
  importTypesFrom: "@shopify/hydrogen/customer-account-api-types",
20
27
  namespacedImportName: "CustomerAccountAPI",
21
- interfaceExtensionCode: caapiDefaultInterfaceExtensionCode
28
+ interfaceExtensionCode: ({ queryType, mutationType }) => replacePlaceholders(
29
+ caapiDefaultInterfaceExtensionCode,
30
+ queryType,
31
+ mutationType
32
+ )
22
33
  };
23
34
  function getDefaultOptions(outputFile = "") {
24
35
  return /^(customer|caapi\.)/i.test(outputFile) ? caapiDefaultValues : sfapiDefaultValues;
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/defaults.ts"],"names":[],"mappings":"AAAA;AAAA,EACE;AAAA,EACA;AAAA,OACK;AAEP,MAAM,qCAAqC;AAAA;AAAA,wCAEH,8BAA8B;AAAA,0CAC5B,iCAAiC;AAAA;AAG3E,MAAM,qCAAqC;AAAA;AAAA,6CAEE,8BAA8B;AAAA,+CAC5B,iCAAiC;AAAA;AAShF,MAAM,qBAAoC;AAAA,EACxC,iBAAiB;AAAA,EACjB,sBAAsB;AAAA,EACtB,wBAAwB;AAC1B;AAEA,MAAM,qBAAoC;AAAA,EACxC,iBAAiB;AAAA,EACjB,sBAAsB;AAAA,EACtB,wBAAwB;AAC1B;AAEO,SAAS,kBAAkB,aAAa,IAAI;AACjD,SAAO,uBAAuB,KAAK,UAAU,IACzC,qBACA;AACN","sourcesContent":["import {\n GENERATED_MUTATION_INTERFACE_NAME,\n GENERATED_QUERY_INTERFACE_NAME,\n} from './plugin.js';\n\nconst sfapiDefaultInterfaceExtensionCode = `\ndeclare module '@shopify/hydrogen' {\n interface StorefrontQueries extends ${GENERATED_QUERY_INTERFACE_NAME} {}\n interface StorefrontMutations extends ${GENERATED_MUTATION_INTERFACE_NAME} {}\n}`;\n\nconst caapiDefaultInterfaceExtensionCode = `\ndeclare module '@shopify/hydrogen' {\n interface CustomerAccountQueries extends ${GENERATED_QUERY_INTERFACE_NAME} {}\n interface CustomerAccountMutations extends ${GENERATED_MUTATION_INTERFACE_NAME} {}\n}`;\n\ntype DefaultValues = {\n importTypesFrom: string;\n namespacedImportName: string;\n interfaceExtensionCode: string;\n};\n\nconst sfapiDefaultValues: DefaultValues = {\n importTypesFrom: '@shopify/hydrogen/storefront-api-types',\n namespacedImportName: 'StorefrontAPI',\n interfaceExtensionCode: sfapiDefaultInterfaceExtensionCode,\n};\n\nconst caapiDefaultValues: DefaultValues = {\n importTypesFrom: '@shopify/hydrogen/customer-account-api-types',\n namespacedImportName: 'CustomerAccountAPI',\n interfaceExtensionCode: caapiDefaultInterfaceExtensionCode,\n};\n\nexport function getDefaultOptions(outputFile = '') {\n return /^(customer|caapi\\.)/i.test(outputFile)\n ? caapiDefaultValues\n : sfapiDefaultValues;\n}\n"]}
1
+ {"version":3,"sources":["../../src/defaults.ts"],"names":[],"mappings":"AAEA,MAAM,sBAAsB;AAC5B,MAAM,wBAAwB;AAE9B,MAAM,qCAAqC;AAAA;AAAA,wCAEH,mBAAmB;AAAA,0CACjB,qBAAqB;AAAA;AAG/D,MAAM,qCAAqC;AAAA;AAAA,6CAEE,mBAAmB;AAAA,+CACjB,qBAAqB;AAAA;AAGpE,SAAS,oBACP,MACA,WACA,cACA;AACA,SAAO,KACJ,QAAQ,qBAAqB,SAAS,EACtC,QAAQ,uBAAuB,YAAY;AAChD;AAQA,MAAM,qBAAoC;AAAA,EACxC,iBAAiB;AAAA,EACjB,sBAAsB;AAAA,EACtB,wBAAwB,CAAC,EAAC,WAAW,aAAY,MAC/C;AAAA,IACE;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACJ;AAEA,MAAM,qBAAoC;AAAA,EACxC,iBAAiB;AAAA,EACjB,sBAAsB;AAAA,EACtB,wBAAwB,CAAC,EAAC,WAAW,aAAY,MAC/C;AAAA,IACE;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACJ;AAEO,SAAS,kBAAkB,aAAa,IAAI;AACjD,SAAO,uBAAuB,KAAK,UAAU,IACzC,qBACA;AACN","sourcesContent":["import type {PresetConfig} from './preset';\n\nconst QUERIES_PLACEHOLDER = '%queries%';\nconst MUTATIONS_PLACEHOLDER = '%mutations%';\n\nconst sfapiDefaultInterfaceExtensionCode = `\ndeclare module '@shopify/hydrogen' {\n interface StorefrontQueries extends ${QUERIES_PLACEHOLDER} {}\n interface StorefrontMutations extends ${MUTATIONS_PLACEHOLDER} {}\n}`;\n\nconst caapiDefaultInterfaceExtensionCode = `\ndeclare module '@shopify/hydrogen' {\n interface CustomerAccountQueries extends ${QUERIES_PLACEHOLDER} {}\n interface CustomerAccountMutations extends ${MUTATIONS_PLACEHOLDER} {}\n}`;\n\nfunction replacePlaceholders(\n code: string,\n queryType: string,\n mutationType: string,\n) {\n return code\n .replace(QUERIES_PLACEHOLDER, queryType)\n .replace(MUTATIONS_PLACEHOLDER, mutationType);\n}\n\ntype DefaultValues = {\n importTypesFrom: string;\n namespacedImportName: string;\n interfaceExtensionCode: NonNullable<PresetConfig['interfaceExtension']>;\n};\n\nconst sfapiDefaultValues: DefaultValues = {\n importTypesFrom: '@shopify/hydrogen/storefront-api-types',\n namespacedImportName: 'StorefrontAPI',\n interfaceExtensionCode: ({queryType, mutationType}) =>\n replacePlaceholders(\n sfapiDefaultInterfaceExtensionCode,\n queryType,\n mutationType,\n ),\n};\n\nconst caapiDefaultValues: DefaultValues = {\n importTypesFrom: '@shopify/hydrogen/customer-account-api-types',\n namespacedImportName: 'CustomerAccountAPI',\n interfaceExtensionCode: ({queryType, mutationType}) =>\n replacePlaceholders(\n caapiDefaultInterfaceExtensionCode,\n queryType,\n mutationType,\n ),\n};\n\nexport function getDefaultOptions(outputFile = '') {\n return /^(customer|caapi\\.)/i.test(outputFile)\n ? caapiDefaultValues\n : sfapiDefaultValues;\n}\n"]}