@shopify/hydrogen-codegen 0.0.1 → 0.0.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.
- package/dist/cjs/plugin.cjs +7 -3
- package/dist/cjs/plugin.cjs.map +1 -1
- package/dist/cjs/preset.cjs +28 -12
- package/dist/cjs/preset.cjs.map +1 -1
- package/dist/esm/plugin.d.ts +3 -1
- package/dist/esm/plugin.js +6 -4
- package/dist/esm/plugin.js.map +1 -1
- package/dist/esm/preset.d.ts +32 -5
- package/dist/esm/preset.js +28 -12
- package/dist/esm/preset.js.map +1 -1
- package/package.json +2 -1
package/dist/cjs/plugin.cjs
CHANGED
|
@@ -5,6 +5,8 @@ const plugin = (_, __, { sourcesWithOperations, interfaceExtensionCode }, _info)
|
|
|
5
5
|
code.push(interfaceExtensionCode);
|
|
6
6
|
return code.join("") + "\n";
|
|
7
7
|
};
|
|
8
|
+
const GENERATED_QUERY_INTERFACE_NAME = "GeneratedQueryTypes";
|
|
9
|
+
const GENERATED_MUTATION_INTERFACE_NAME = "GeneratedMutationTypes";
|
|
8
10
|
const isMutationRE = /(^|}\s|\n\s*)mutation[\s({]/im;
|
|
9
11
|
const normalizeOperation = (rawSDL, variablesMap) => {
|
|
10
12
|
let variableNotFound = false;
|
|
@@ -26,7 +28,7 @@ const normalizeOperation = (rawSDL, variablesMap) => {
|
|
|
26
28
|
return rawSDL;
|
|
27
29
|
};
|
|
28
30
|
const buildTypeLines = (name, operations) => {
|
|
29
|
-
const lines = [`interface
|
|
31
|
+
const lines = [`interface ${name} {
|
|
30
32
|
`];
|
|
31
33
|
for (const [originalString, typeNames] of operations) {
|
|
32
34
|
lines.push(
|
|
@@ -63,12 +65,14 @@ function getDocumentRegistryChunk(sourcesWithOperations = []) {
|
|
|
63
65
|
);
|
|
64
66
|
}
|
|
65
67
|
return [
|
|
66
|
-
...buildTypeLines(
|
|
68
|
+
...buildTypeLines(GENERATED_QUERY_INTERFACE_NAME, queries),
|
|
67
69
|
"\n",
|
|
68
|
-
...buildTypeLines(
|
|
70
|
+
...buildTypeLines(GENERATED_MUTATION_INTERFACE_NAME, mutations)
|
|
69
71
|
];
|
|
70
72
|
}
|
|
71
73
|
|
|
74
|
+
exports.GENERATED_MUTATION_INTERFACE_NAME = GENERATED_MUTATION_INTERFACE_NAME;
|
|
75
|
+
exports.GENERATED_QUERY_INTERFACE_NAME = GENERATED_QUERY_INTERFACE_NAME;
|
|
72
76
|
exports.plugin = plugin;
|
|
73
77
|
//# sourceMappingURL=out.js.map
|
|
74
78
|
//# sourceMappingURL=plugin.cjs.map
|
package/dist/cjs/plugin.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/plugin.ts"],"names":[],"mappings":"AAiBO,MAAM,SAGR,CAAC,GAAG,IAAI,EAAC,uBAAuB,uBAAsB,GAAG,UAAU;AACtE,QAAM,OAAO,yBAAyB,qBAAqB;AAE3D,OAAK,KAAK,sBAAsB;AAEhC,SAAO,KAAK,KAAK,EAAE,IAAI;AACzB;
|
|
1
|
+
{"version":3,"sources":["../../src/plugin.ts"],"names":[],"mappings":"AAiBO,MAAM,SAGR,CAAC,GAAG,IAAI,EAAC,uBAAuB,uBAAsB,GAAG,UAAU;AACtE,QAAM,OAAO,yBAAyB,qBAAqB;AAE3D,OAAK,KAAK,sBAAsB;AAEhC,SAAO,KAAK,KAAK,EAAE,IAAI;AACzB;AAEO,MAAM,iCAAiC;AACvC,MAAM,oCAAoC;AAEjD,MAAM,eAAe;AAIrB,MAAM,qBAAqB,CACzB,QACA,iBACG;AACH,MAAI,mBAAmB;AAEvB,SAAO,iBAAiB,KAAK,MAAM,KAAK,CAAC,kBAAkB;AACzD,QAAI,oBAAoB,OAAO,SAAS,sBAAsB;AAC9D,eAAW,CAAC,OAAO,YAAY,KAAK,mBAAmB;AACrD,UAAI,aAAa,IAAI,YAAY,GAAG;AAClC,iBAAS,OAAO,QAAQ,OAAO,aAAa,IAAI,YAAY,CAAE;AAAA,MAChE,OAAO;AAIL,2BAAmB;AACnB,gBAAQ;AAAA,UACN,IAAI;AAAA,YACF,aAAa;AAAA,UACf;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAEA,SAAO;AACT;AAEA,MAAM,iBAAiB,CAAC,MAAc,eAAsC;AAC1E,QAAM,QAAQ,CAAC,aAAa;AAAA,CAAU;AAEtC,aAAW,CAAC,gBAAgB,SAAS,KAAK,YAAY;AACpD,UAAM;AAAA,MACJ,KAAK,KAAK,UAAU,cAAc,eAIhC,UAAU,WAAW,IAAI,UAAU,KAAK,uBAC1B,UAAU,IAAI,CAAC,MAAM,IAAI,WAAW,EAAE,KAAK,KAAK;AAAA;AAAA,IAClE;AAAA,EACF;AAEA,QAAM,KAAK;AAAA,CAAK;AAEhB,SAAO;AACT;AAEA,SAAS,yBACP,wBAAqD,CAAC,GACtD;AACA,QAAM,UAAU,oBAAI,IAAsB;AAC1C,QAAM,YAAY,oBAAI,IAAsB;AAE5C,QAAM,eAAe,oBAAI,IAAoB;AAC7C,aAAW,EAAC,OAAM,KAAK,uBAAuB;AAC5C,UAAM,eAAe,OAAO,QAAQ,MAAM,iBAAiB,IAAI;AAC/D,QAAI,cAAc;AAChB,aAAO,SAAS,OAAO,OAAQ,QAAQ,kBAAkB,EAAE;AAC3D,mBAAa,IAAI,cAAc,OAAO,MAAO;AAAA,IAC/C;AAAA,EACF;AAEA,aAAW,EAAC,YAAY,OAAM,KAAK,uBAAuB;AACxD,UAAM,mBAAmB,WAAW;AAAA,MAClC,CAAC,OAAO,GAAG,WAAW,SAAS;AAAA,IACjC;AAEA,QAAI,iBAAiB,WAAW;AAAG;AAEnC,UAAM,YAAY,OAAO;AACzB,UAAM,SAAS,aAAa,KAAK,SAAS,IAAI,YAAY;AAC1D,WAAO;AAAA,MACL,mBAAmB,WAAW,YAAY;AAAA,MAC1C,iBAAiB,IAAI,CAAC,MAAM,EAAE,WAAW;AAAA,IAC3C;AAAA,EACF;AAEA,SAAO;AAAA,IACL,GAAG,eAAe,gCAAgC,OAAO;AAAA,IACzD;AAAA,IACA,GAAG,eAAe,mCAAmC,SAAS;AAAA,EAChE;AACF","sourcesContent":["// This plugin is based on `gql-tag-operations-preset`\n// https://www.npmjs.com/package/@graphql-codegen/gql-tag-operations-preset\n\nimport type {PluginFunction} from '@graphql-codegen/plugin-helpers';\nimport type {Source} from '@graphql-tools/utils';\nimport type {FragmentDefinitionNode, OperationDefinitionNode} from 'graphql';\n\nexport type OperationOrFragment = {\n initialName: string;\n definition: OperationDefinitionNode | FragmentDefinitionNode;\n};\n\nexport type SourceWithOperations = {\n source: Source;\n operations: Array<OperationOrFragment>;\n};\n\nexport const plugin: PluginFunction<{\n sourcesWithOperations: Array<SourceWithOperations>;\n interfaceExtensionCode: string;\n}> = (_, __, {sourcesWithOperations, interfaceExtensionCode}, _info) => {\n const code = getDocumentRegistryChunk(sourcesWithOperations);\n\n code.push(interfaceExtensionCode);\n\n return code.join('') + '\\n';\n};\n\nexport const GENERATED_QUERY_INTERFACE_NAME = 'GeneratedQueryTypes';\nexport const GENERATED_MUTATION_INTERFACE_NAME = 'GeneratedMutationTypes';\n\nconst isMutationRE = /(^|}\\s|\\n\\s*)mutation[\\s({]/im;\n\n// Iteratively replace fragment annotations with the actual fragment content\n// until there are no more annotations in the operation.\nconst normalizeOperation = (\n rawSDL: string,\n variablesMap: Map<string, string>,\n) => {\n let variableNotFound = false;\n\n while (/#REQUIRED_VAR=/.test(rawSDL) && !variableNotFound) {\n let requiredVariables = rawSDL.matchAll(/#REQUIRED_VAR=(\\w+)/g);\n for (const [match, variableName] of requiredVariables) {\n if (variablesMap.has(variableName)) {\n rawSDL = rawSDL.replace(match, variablesMap.get(variableName)!);\n } else {\n // An annotation cannot be replaced, so the operation is invalid.\n // This should not happen, but we'll handle it just in case\n // to prevent infinite loops. This should be logged as an error and fixed.\n variableNotFound = true; // break;\n console.error(\n new Error(\n `Variable \"${variableName}\" not found. This might be a bug in hydrogen-codegen, please report it.`,\n ),\n );\n }\n }\n }\n\n return rawSDL;\n};\n\nconst buildTypeLines = (name: string, operations: Map<string, string[]>) => {\n const lines = [`interface ${name} {\\n`];\n\n for (const [originalString, typeNames] of operations) {\n lines.push(\n ` ${JSON.stringify(originalString)}: {return: ${\n // SFAPI does not support multiple operations in a single document.\n // Use 'never' here if that's the case so that the user gets a type error.\n // e.g. `'query q1 {...} query q2 {...}'` is invalid.\n typeNames.length === 1 ? typeNames[0] : 'never'\n }, variables: ${typeNames.map((n) => n + 'Variables').join(' & ')}},\\n`,\n );\n }\n\n lines.push(`}\\n`);\n\n return lines;\n};\n\nfunction getDocumentRegistryChunk(\n sourcesWithOperations: Array<SourceWithOperations> = [],\n) {\n const queries = new Map<string, string[]>();\n const mutations = new Map<string, string[]>();\n\n const variablesMap = new Map<string, string>();\n for (const {source} of sourcesWithOperations) {\n const variableName = source.rawSDL?.match(/#VAR_NAME=(\\w+)/)?.[1];\n if (variableName) {\n source.rawSDL = source.rawSDL!.replace(/#VAR_NAME=\\w+$/, '');\n variablesMap.set(variableName, source.rawSDL!);\n }\n }\n\n for (const {operations, source} of sourcesWithOperations) {\n const actualOperations = operations.filter(\n (op) => op.definition.kind === 'OperationDefinition',\n );\n\n if (actualOperations.length === 0) continue;\n\n const sdlString = source.rawSDL!;\n const target = isMutationRE.test(sdlString) ? mutations : queries;\n target.set(\n normalizeOperation(sdlString, variablesMap),\n actualOperations.map((o) => o.initialName),\n );\n }\n\n return [\n ...buildTypeLines(GENERATED_QUERY_INTERFACE_NAME, queries),\n '\\n',\n ...buildTypeLines(GENERATED_MUTATION_INTERFACE_NAME, mutations),\n ];\n}\n"]}
|
package/dist/cjs/preset.cjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var addPlugin = require('@graphql-codegen/add');
|
|
4
|
+
var typescriptPlugin = require('@graphql-codegen/typescript');
|
|
4
5
|
var typescriptOperationPlugin = require('@graphql-codegen/typescript-operations');
|
|
5
6
|
var sources_js = require('./sources.cjs');
|
|
6
7
|
var plugin_js = require('./plugin.cjs');
|
|
@@ -23,13 +24,13 @@ function _interopNamespaceDefault(e) {
|
|
|
23
24
|
}
|
|
24
25
|
|
|
25
26
|
var addPlugin__namespace = /*#__PURE__*/_interopNamespaceDefault(addPlugin);
|
|
27
|
+
var typescriptPlugin__namespace = /*#__PURE__*/_interopNamespaceDefault(typescriptPlugin);
|
|
26
28
|
var typescriptOperationPlugin__namespace = /*#__PURE__*/_interopNamespaceDefault(typescriptOperationPlugin);
|
|
27
29
|
|
|
28
|
-
const
|
|
29
|
-
const interfaceExtensionCode = `
|
|
30
|
+
const defaultInterfaceExtensionCode = `
|
|
30
31
|
declare module '@shopify/hydrogen' {
|
|
31
|
-
interface StorefrontQueries extends
|
|
32
|
-
interface StorefrontMutations extends
|
|
32
|
+
interface StorefrontQueries extends ${plugin_js.GENERATED_QUERY_INTERFACE_NAME} {}
|
|
33
|
+
interface StorefrontMutations extends ${plugin_js.GENERATED_MUTATION_INTERFACE_NAME} {}
|
|
33
34
|
}`;
|
|
34
35
|
const preset = {
|
|
35
36
|
buildGeneratesSection: (options) => {
|
|
@@ -43,9 +44,17 @@ const preset = {
|
|
|
43
44
|
}
|
|
44
45
|
const sourcesWithOperations = sources_js.processSources(options.documents);
|
|
45
46
|
const sources = sourcesWithOperations.map(({ source }) => source);
|
|
47
|
+
const importTypes = options.presetConfig.importTypes ?? true;
|
|
48
|
+
const namespacedImportName = options.presetConfig.namespacedImportName ?? "StorefrontAPI";
|
|
49
|
+
const importTypesFrom = options.presetConfig.importTypesFrom ?? "@shopify/hydrogen/storefront-api-types";
|
|
50
|
+
const interfaceExtensionCode = options.presetConfig.interfaceExtension?.({
|
|
51
|
+
queryType: plugin_js.GENERATED_QUERY_INTERFACE_NAME,
|
|
52
|
+
mutationType: plugin_js.GENERATED_MUTATION_INTERFACE_NAME
|
|
53
|
+
}) ?? defaultInterfaceExtensionCode;
|
|
46
54
|
const pluginMap = {
|
|
47
55
|
...options.pluginMap,
|
|
48
56
|
[`add`]: addPlugin__namespace,
|
|
57
|
+
[`typescript`]: typescriptPlugin__namespace,
|
|
49
58
|
[`typescript-operations`]: typescriptOperationPlugin__namespace,
|
|
50
59
|
[`gen-dts`]: { plugin: plugin_js.plugin }
|
|
51
60
|
};
|
|
@@ -57,17 +66,25 @@ const preset = {
|
|
|
57
66
|
/* eslint-disable */`
|
|
58
67
|
}
|
|
59
68
|
},
|
|
60
|
-
{
|
|
69
|
+
importTypes ? {
|
|
61
70
|
[`add`]: {
|
|
62
|
-
content: `import * as ${namespacedImportName} from '
|
|
71
|
+
content: `import * as ${namespacedImportName} from '${importTypesFrom}';
|
|
63
72
|
`
|
|
64
73
|
}
|
|
74
|
+
} : {
|
|
75
|
+
[`typescript`]: {
|
|
76
|
+
useTypeImports: true,
|
|
77
|
+
useImplementingTypes: true,
|
|
78
|
+
enumsAsTypes: true
|
|
79
|
+
}
|
|
65
80
|
},
|
|
66
81
|
{
|
|
67
82
|
[`typescript-operations`]: {
|
|
68
|
-
skipTypename: true,
|
|
69
83
|
useTypeImports: true,
|
|
70
|
-
preResolveTypes: false
|
|
84
|
+
preResolveTypes: false,
|
|
85
|
+
mergeFragmentTypes: true,
|
|
86
|
+
skipTypename: options.presetConfig.skipTypenameInOperations ?? true,
|
|
87
|
+
namespacedImportName: importTypes ? namespacedImportName : void 0
|
|
71
88
|
}
|
|
72
89
|
},
|
|
73
90
|
{ [`gen-dts`]: { sourcesWithOperations, interfaceExtensionCode } },
|
|
@@ -80,8 +97,8 @@ const preset = {
|
|
|
80
97
|
pluginMap,
|
|
81
98
|
schema: options.schema,
|
|
82
99
|
config: {
|
|
83
|
-
|
|
84
|
-
|
|
100
|
+
defaultScalarType: "unknown",
|
|
101
|
+
...options.config
|
|
85
102
|
},
|
|
86
103
|
documents: sources,
|
|
87
104
|
documentTransforms: options.documentTransforms
|
|
@@ -90,8 +107,7 @@ const preset = {
|
|
|
90
107
|
}
|
|
91
108
|
};
|
|
92
109
|
|
|
93
|
-
exports.
|
|
94
|
-
exports.namespacedImportName = namespacedImportName;
|
|
110
|
+
exports.defaultInterfaceExtensionCode = defaultInterfaceExtensionCode;
|
|
95
111
|
exports.preset = preset;
|
|
96
112
|
//# sourceMappingURL=out.js.map
|
|
97
113
|
//# sourceMappingURL=preset.cjs.map
|
package/dist/cjs/preset.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/preset.ts"],"names":[],"mappings":"AACA,YAAY,eAAe;AAC3B,YAAY,+BAA+B;AAC3C,SAAQ,sBAAqB;AAC7B,
|
|
1
|
+
{"version":3,"sources":["../../src/preset.ts"],"names":[],"mappings":"AACA,YAAY,eAAe;AAC3B,YAAY,sBAAsB;AAClC,YAAY,+BAA+B;AAC3C,SAAQ,sBAAqB;AAC7B;AAAA,EACE,UAAU;AAAA,EACV;AAAA,EACA;AAAA,OACK;AAgCA,MAAM,gCAAgC;AAAA;AAAA,wCAEL;AAAA,0CACE;AAAA;AAGnC,MAAM,SAAmD;AAAA,EAC9D,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,cAAc,QAAQ,aAAa,eAAe;AACxD,UAAM,uBACJ,QAAQ,aAAa,wBAAwB;AAC/C,UAAM,kBACJ,QAAQ,aAAa,mBACrB;AAEF,UAAM,yBACJ,QAAQ,aAAa,qBAAqB;AAAA,MACxC,WAAW;AAAA,MACX,cAAc;AAAA,IAChB,CAAC,KAAK;AAER,UAAM,YAAY;AAAA,MAChB,GAAG,QAAQ;AAAA,MACX,CAAC,QAAQ;AAAA,MACT,CAAC,eAAe;AAAA,MAChB,CAAC,0BAA0B;AAAA,MAC3B,CAAC,YAAY,EAAC,QAAQ,UAAS;AAAA,IACjC;AAEA,UAAM,UAAyC;AAAA,MAE7C;AAAA,QACE,CAAC,QAAQ;AAAA,UACP,SAAS;AAAA;AAAA;AAAA,QACX;AAAA,MACF;AAAA,MAEA,cACI;AAAA,QACE,CAAC,QAAQ;AAAA,UACP,SAAS,eAAe,8BAA8B;AAAA;AAAA,QACxD;AAAA,MACF,IACA;AAAA,QACE,CAAC,eAAe;AAAA,UACd,gBAAgB;AAAA,UAChB,sBAAsB;AAAA,UACtB,cAAc;AAAA,QAChB;AAAA,MACF;AAAA,MAEJ;AAAA,QACE,CAAC,0BAA0B;AAAA,UACzB,gBAAgB;AAAA,UAChB,iBAAiB;AAAA,UACjB,oBAAoB;AAAA,UACpB,cAAc,QAAQ,aAAa,4BAA4B;AAAA,UAC/D,sBAAsB,cAAc,uBAAuB;AAAA,QAC7D;AAAA,MACF;AAAA,MAEA,EAAC,CAAC,YAAY,EAAC,uBAAuB,uBAAsB,EAAC;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,UAEN,mBAAmB;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 {\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 defaultInterfaceExtensionCode = `\ndeclare module '@shopify/hydrogen' {\n interface StorefrontQueries extends ${GENERATED_QUERY_INTERFACE_NAME} {}\n interface StorefrontMutations extends ${GENERATED_MUTATION_INTERFACE_NAME} {}\n}`;\n\nexport const preset: Types.OutputPreset<HydrogenPresetConfig> = {\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 importTypes = options.presetConfig.importTypes ?? true;\n const namespacedImportName =\n options.presetConfig.namespacedImportName ?? 'StorefrontAPI';\n const importTypesFrom =\n options.presetConfig.importTypesFrom ??\n '@shopify/hydrogen/storefront-api-types';\n\n const interfaceExtensionCode =\n options.presetConfig.interfaceExtension?.({\n queryType: GENERATED_QUERY_INTERFACE_NAME,\n mutationType: GENERATED_MUTATION_INTERFACE_NAME,\n }) ?? defaultInterfaceExtensionCode;\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"]}
|
package/dist/esm/plugin.d.ts
CHANGED
|
@@ -14,5 +14,7 @@ declare const plugin: PluginFunction<{
|
|
|
14
14
|
sourcesWithOperations: Array<SourceWithOperations>;
|
|
15
15
|
interfaceExtensionCode: string;
|
|
16
16
|
}>;
|
|
17
|
+
declare const GENERATED_QUERY_INTERFACE_NAME = "GeneratedQueryTypes";
|
|
18
|
+
declare const GENERATED_MUTATION_INTERFACE_NAME = "GeneratedMutationTypes";
|
|
17
19
|
|
|
18
|
-
export { OperationOrFragment, SourceWithOperations, plugin };
|
|
20
|
+
export { GENERATED_MUTATION_INTERFACE_NAME, GENERATED_QUERY_INTERFACE_NAME, OperationOrFragment, SourceWithOperations, plugin };
|
package/dist/esm/plugin.js
CHANGED
|
@@ -3,6 +3,8 @@ const plugin = (_, __, { sourcesWithOperations, interfaceExtensionCode }, _info)
|
|
|
3
3
|
code.push(interfaceExtensionCode);
|
|
4
4
|
return code.join("") + "\n";
|
|
5
5
|
};
|
|
6
|
+
const GENERATED_QUERY_INTERFACE_NAME = "GeneratedQueryTypes";
|
|
7
|
+
const GENERATED_MUTATION_INTERFACE_NAME = "GeneratedMutationTypes";
|
|
6
8
|
const isMutationRE = /(^|}\s|\n\s*)mutation[\s({]/im;
|
|
7
9
|
const normalizeOperation = (rawSDL, variablesMap) => {
|
|
8
10
|
let variableNotFound = false;
|
|
@@ -24,7 +26,7 @@ const normalizeOperation = (rawSDL, variablesMap) => {
|
|
|
24
26
|
return rawSDL;
|
|
25
27
|
};
|
|
26
28
|
const buildTypeLines = (name, operations) => {
|
|
27
|
-
const lines = [`interface
|
|
29
|
+
const lines = [`interface ${name} {
|
|
28
30
|
`];
|
|
29
31
|
for (const [originalString, typeNames] of operations) {
|
|
30
32
|
lines.push(
|
|
@@ -61,12 +63,12 @@ function getDocumentRegistryChunk(sourcesWithOperations = []) {
|
|
|
61
63
|
);
|
|
62
64
|
}
|
|
63
65
|
return [
|
|
64
|
-
...buildTypeLines(
|
|
66
|
+
...buildTypeLines(GENERATED_QUERY_INTERFACE_NAME, queries),
|
|
65
67
|
"\n",
|
|
66
|
-
...buildTypeLines(
|
|
68
|
+
...buildTypeLines(GENERATED_MUTATION_INTERFACE_NAME, mutations)
|
|
67
69
|
];
|
|
68
70
|
}
|
|
69
71
|
|
|
70
|
-
export { plugin };
|
|
72
|
+
export { GENERATED_MUTATION_INTERFACE_NAME, GENERATED_QUERY_INTERFACE_NAME, plugin };
|
|
71
73
|
//# sourceMappingURL=out.js.map
|
|
72
74
|
//# sourceMappingURL=plugin.js.map
|
package/dist/esm/plugin.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/plugin.ts"],"names":[],"mappings":"AAiBO,MAAM,SAGR,CAAC,GAAG,IAAI,EAAC,uBAAuB,uBAAsB,GAAG,UAAU;AACtE,QAAM,OAAO,yBAAyB,qBAAqB;AAE3D,OAAK,KAAK,sBAAsB;AAEhC,SAAO,KAAK,KAAK,EAAE,IAAI;AACzB;
|
|
1
|
+
{"version":3,"sources":["../../src/plugin.ts"],"names":[],"mappings":"AAiBO,MAAM,SAGR,CAAC,GAAG,IAAI,EAAC,uBAAuB,uBAAsB,GAAG,UAAU;AACtE,QAAM,OAAO,yBAAyB,qBAAqB;AAE3D,OAAK,KAAK,sBAAsB;AAEhC,SAAO,KAAK,KAAK,EAAE,IAAI;AACzB;AAEO,MAAM,iCAAiC;AACvC,MAAM,oCAAoC;AAEjD,MAAM,eAAe;AAIrB,MAAM,qBAAqB,CACzB,QACA,iBACG;AACH,MAAI,mBAAmB;AAEvB,SAAO,iBAAiB,KAAK,MAAM,KAAK,CAAC,kBAAkB;AACzD,QAAI,oBAAoB,OAAO,SAAS,sBAAsB;AAC9D,eAAW,CAAC,OAAO,YAAY,KAAK,mBAAmB;AACrD,UAAI,aAAa,IAAI,YAAY,GAAG;AAClC,iBAAS,OAAO,QAAQ,OAAO,aAAa,IAAI,YAAY,CAAE;AAAA,MAChE,OAAO;AAIL,2BAAmB;AACnB,gBAAQ;AAAA,UACN,IAAI;AAAA,YACF,aAAa;AAAA,UACf;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAEA,SAAO;AACT;AAEA,MAAM,iBAAiB,CAAC,MAAc,eAAsC;AAC1E,QAAM,QAAQ,CAAC,aAAa;AAAA,CAAU;AAEtC,aAAW,CAAC,gBAAgB,SAAS,KAAK,YAAY;AACpD,UAAM;AAAA,MACJ,KAAK,KAAK,UAAU,cAAc,eAIhC,UAAU,WAAW,IAAI,UAAU,KAAK,uBAC1B,UAAU,IAAI,CAAC,MAAM,IAAI,WAAW,EAAE,KAAK,KAAK;AAAA;AAAA,IAClE;AAAA,EACF;AAEA,QAAM,KAAK;AAAA,CAAK;AAEhB,SAAO;AACT;AAEA,SAAS,yBACP,wBAAqD,CAAC,GACtD;AACA,QAAM,UAAU,oBAAI,IAAsB;AAC1C,QAAM,YAAY,oBAAI,IAAsB;AAE5C,QAAM,eAAe,oBAAI,IAAoB;AAC7C,aAAW,EAAC,OAAM,KAAK,uBAAuB;AAC5C,UAAM,eAAe,OAAO,QAAQ,MAAM,iBAAiB,IAAI;AAC/D,QAAI,cAAc;AAChB,aAAO,SAAS,OAAO,OAAQ,QAAQ,kBAAkB,EAAE;AAC3D,mBAAa,IAAI,cAAc,OAAO,MAAO;AAAA,IAC/C;AAAA,EACF;AAEA,aAAW,EAAC,YAAY,OAAM,KAAK,uBAAuB;AACxD,UAAM,mBAAmB,WAAW;AAAA,MAClC,CAAC,OAAO,GAAG,WAAW,SAAS;AAAA,IACjC;AAEA,QAAI,iBAAiB,WAAW;AAAG;AAEnC,UAAM,YAAY,OAAO;AACzB,UAAM,SAAS,aAAa,KAAK,SAAS,IAAI,YAAY;AAC1D,WAAO;AAAA,MACL,mBAAmB,WAAW,YAAY;AAAA,MAC1C,iBAAiB,IAAI,CAAC,MAAM,EAAE,WAAW;AAAA,IAC3C;AAAA,EACF;AAEA,SAAO;AAAA,IACL,GAAG,eAAe,gCAAgC,OAAO;AAAA,IACzD;AAAA,IACA,GAAG,eAAe,mCAAmC,SAAS;AAAA,EAChE;AACF","sourcesContent":["// This plugin is based on `gql-tag-operations-preset`\n// https://www.npmjs.com/package/@graphql-codegen/gql-tag-operations-preset\n\nimport type {PluginFunction} from '@graphql-codegen/plugin-helpers';\nimport type {Source} from '@graphql-tools/utils';\nimport type {FragmentDefinitionNode, OperationDefinitionNode} from 'graphql';\n\nexport type OperationOrFragment = {\n initialName: string;\n definition: OperationDefinitionNode | FragmentDefinitionNode;\n};\n\nexport type SourceWithOperations = {\n source: Source;\n operations: Array<OperationOrFragment>;\n};\n\nexport const plugin: PluginFunction<{\n sourcesWithOperations: Array<SourceWithOperations>;\n interfaceExtensionCode: string;\n}> = (_, __, {sourcesWithOperations, interfaceExtensionCode}, _info) => {\n const code = getDocumentRegistryChunk(sourcesWithOperations);\n\n code.push(interfaceExtensionCode);\n\n return code.join('') + '\\n';\n};\n\nexport const GENERATED_QUERY_INTERFACE_NAME = 'GeneratedQueryTypes';\nexport const GENERATED_MUTATION_INTERFACE_NAME = 'GeneratedMutationTypes';\n\nconst isMutationRE = /(^|}\\s|\\n\\s*)mutation[\\s({]/im;\n\n// Iteratively replace fragment annotations with the actual fragment content\n// until there are no more annotations in the operation.\nconst normalizeOperation = (\n rawSDL: string,\n variablesMap: Map<string, string>,\n) => {\n let variableNotFound = false;\n\n while (/#REQUIRED_VAR=/.test(rawSDL) && !variableNotFound) {\n let requiredVariables = rawSDL.matchAll(/#REQUIRED_VAR=(\\w+)/g);\n for (const [match, variableName] of requiredVariables) {\n if (variablesMap.has(variableName)) {\n rawSDL = rawSDL.replace(match, variablesMap.get(variableName)!);\n } else {\n // An annotation cannot be replaced, so the operation is invalid.\n // This should not happen, but we'll handle it just in case\n // to prevent infinite loops. This should be logged as an error and fixed.\n variableNotFound = true; // break;\n console.error(\n new Error(\n `Variable \"${variableName}\" not found. This might be a bug in hydrogen-codegen, please report it.`,\n ),\n );\n }\n }\n }\n\n return rawSDL;\n};\n\nconst buildTypeLines = (name: string, operations: Map<string, string[]>) => {\n const lines = [`interface ${name} {\\n`];\n\n for (const [originalString, typeNames] of operations) {\n lines.push(\n ` ${JSON.stringify(originalString)}: {return: ${\n // SFAPI does not support multiple operations in a single document.\n // Use 'never' here if that's the case so that the user gets a type error.\n // e.g. `'query q1 {...} query q2 {...}'` is invalid.\n typeNames.length === 1 ? typeNames[0] : 'never'\n }, variables: ${typeNames.map((n) => n + 'Variables').join(' & ')}},\\n`,\n );\n }\n\n lines.push(`}\\n`);\n\n return lines;\n};\n\nfunction getDocumentRegistryChunk(\n sourcesWithOperations: Array<SourceWithOperations> = [],\n) {\n const queries = new Map<string, string[]>();\n const mutations = new Map<string, string[]>();\n\n const variablesMap = new Map<string, string>();\n for (const {source} of sourcesWithOperations) {\n const variableName = source.rawSDL?.match(/#VAR_NAME=(\\w+)/)?.[1];\n if (variableName) {\n source.rawSDL = source.rawSDL!.replace(/#VAR_NAME=\\w+$/, '');\n variablesMap.set(variableName, source.rawSDL!);\n }\n }\n\n for (const {operations, source} of sourcesWithOperations) {\n const actualOperations = operations.filter(\n (op) => op.definition.kind === 'OperationDefinition',\n );\n\n if (actualOperations.length === 0) continue;\n\n const sdlString = source.rawSDL!;\n const target = isMutationRE.test(sdlString) ? mutations : queries;\n target.set(\n normalizeOperation(sdlString, variablesMap),\n actualOperations.map((o) => o.initialName),\n );\n }\n\n return [\n ...buildTypeLines(GENERATED_QUERY_INTERFACE_NAME, queries),\n '\\n',\n ...buildTypeLines(GENERATED_MUTATION_INTERFACE_NAME, mutations),\n ];\n}\n"]}
|
package/dist/esm/preset.d.ts
CHANGED
|
@@ -1,8 +1,35 @@
|
|
|
1
1
|
import { Types } from '@graphql-codegen/plugin-helpers';
|
|
2
2
|
|
|
3
|
-
type
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
type HydrogenPresetConfig = {
|
|
4
|
+
/**
|
|
5
|
+
* Name for the variable that contains the imported types.
|
|
6
|
+
* @default 'StorefrontAPI'
|
|
7
|
+
*/
|
|
8
|
+
namespacedImportName?: string;
|
|
9
|
+
/**
|
|
10
|
+
* Module to import the types from.
|
|
11
|
+
* @default '@shopify/hydrogen/storefront-api-types'
|
|
12
|
+
*/
|
|
13
|
+
importTypesFrom?: string;
|
|
14
|
+
/**
|
|
15
|
+
* Whether types should be imported from the `importTypesFrom` module, or generated inline.
|
|
16
|
+
* @default true
|
|
17
|
+
*/
|
|
18
|
+
importTypes?: boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Whether to skip adding `__typename` to generated operation types.
|
|
21
|
+
* @default true
|
|
22
|
+
*/
|
|
23
|
+
skipTypenameInOperations?: boolean;
|
|
24
|
+
/**
|
|
25
|
+
* Override the default interface extension.
|
|
26
|
+
*/
|
|
27
|
+
interfaceExtension?: (options: {
|
|
28
|
+
queryType: string;
|
|
29
|
+
mutationType: string;
|
|
30
|
+
}) => string;
|
|
31
|
+
};
|
|
32
|
+
declare const defaultInterfaceExtensionCode: string;
|
|
33
|
+
declare const preset: Types.OutputPreset<HydrogenPresetConfig>;
|
|
7
34
|
|
|
8
|
-
export {
|
|
35
|
+
export { HydrogenPresetConfig, defaultInterfaceExtensionCode, preset };
|
package/dist/esm/preset.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import * as addPlugin from '@graphql-codegen/add';
|
|
2
|
+
import * as typescriptPlugin from '@graphql-codegen/typescript';
|
|
2
3
|
import * as typescriptOperationPlugin from '@graphql-codegen/typescript-operations';
|
|
3
4
|
import { processSources } from './sources.js';
|
|
4
|
-
import { plugin } from './plugin.js';
|
|
5
|
+
import { GENERATED_QUERY_INTERFACE_NAME, GENERATED_MUTATION_INTERFACE_NAME, plugin } from './plugin.js';
|
|
5
6
|
|
|
6
|
-
const
|
|
7
|
-
const interfaceExtensionCode = `
|
|
7
|
+
const defaultInterfaceExtensionCode = `
|
|
8
8
|
declare module '@shopify/hydrogen' {
|
|
9
|
-
interface StorefrontQueries extends
|
|
10
|
-
interface StorefrontMutations extends
|
|
9
|
+
interface StorefrontQueries extends ${GENERATED_QUERY_INTERFACE_NAME} {}
|
|
10
|
+
interface StorefrontMutations extends ${GENERATED_MUTATION_INTERFACE_NAME} {}
|
|
11
11
|
}`;
|
|
12
12
|
const preset = {
|
|
13
13
|
buildGeneratesSection: (options) => {
|
|
@@ -21,9 +21,17 @@ const preset = {
|
|
|
21
21
|
}
|
|
22
22
|
const sourcesWithOperations = processSources(options.documents);
|
|
23
23
|
const sources = sourcesWithOperations.map(({ source }) => source);
|
|
24
|
+
const importTypes = options.presetConfig.importTypes ?? true;
|
|
25
|
+
const namespacedImportName = options.presetConfig.namespacedImportName ?? "StorefrontAPI";
|
|
26
|
+
const importTypesFrom = options.presetConfig.importTypesFrom ?? "@shopify/hydrogen/storefront-api-types";
|
|
27
|
+
const interfaceExtensionCode = options.presetConfig.interfaceExtension?.({
|
|
28
|
+
queryType: GENERATED_QUERY_INTERFACE_NAME,
|
|
29
|
+
mutationType: GENERATED_MUTATION_INTERFACE_NAME
|
|
30
|
+
}) ?? defaultInterfaceExtensionCode;
|
|
24
31
|
const pluginMap = {
|
|
25
32
|
...options.pluginMap,
|
|
26
33
|
[`add`]: addPlugin,
|
|
34
|
+
[`typescript`]: typescriptPlugin,
|
|
27
35
|
[`typescript-operations`]: typescriptOperationPlugin,
|
|
28
36
|
[`gen-dts`]: { plugin: plugin }
|
|
29
37
|
};
|
|
@@ -35,17 +43,25 @@ const preset = {
|
|
|
35
43
|
/* eslint-disable */`
|
|
36
44
|
}
|
|
37
45
|
},
|
|
38
|
-
{
|
|
46
|
+
importTypes ? {
|
|
39
47
|
[`add`]: {
|
|
40
|
-
content: `import * as ${namespacedImportName} from '
|
|
48
|
+
content: `import * as ${namespacedImportName} from '${importTypesFrom}';
|
|
41
49
|
`
|
|
42
50
|
}
|
|
51
|
+
} : {
|
|
52
|
+
[`typescript`]: {
|
|
53
|
+
useTypeImports: true,
|
|
54
|
+
useImplementingTypes: true,
|
|
55
|
+
enumsAsTypes: true
|
|
56
|
+
}
|
|
43
57
|
},
|
|
44
58
|
{
|
|
45
59
|
[`typescript-operations`]: {
|
|
46
|
-
skipTypename: true,
|
|
47
60
|
useTypeImports: true,
|
|
48
|
-
preResolveTypes: false
|
|
61
|
+
preResolveTypes: false,
|
|
62
|
+
mergeFragmentTypes: true,
|
|
63
|
+
skipTypename: options.presetConfig.skipTypenameInOperations ?? true,
|
|
64
|
+
namespacedImportName: importTypes ? namespacedImportName : void 0
|
|
49
65
|
}
|
|
50
66
|
},
|
|
51
67
|
{ [`gen-dts`]: { sourcesWithOperations, interfaceExtensionCode } },
|
|
@@ -58,8 +74,8 @@ const preset = {
|
|
|
58
74
|
pluginMap,
|
|
59
75
|
schema: options.schema,
|
|
60
76
|
config: {
|
|
61
|
-
|
|
62
|
-
|
|
77
|
+
defaultScalarType: "unknown",
|
|
78
|
+
...options.config
|
|
63
79
|
},
|
|
64
80
|
documents: sources,
|
|
65
81
|
documentTransforms: options.documentTransforms
|
|
@@ -68,6 +84,6 @@ const preset = {
|
|
|
68
84
|
}
|
|
69
85
|
};
|
|
70
86
|
|
|
71
|
-
export {
|
|
87
|
+
export { defaultInterfaceExtensionCode, preset };
|
|
72
88
|
//# sourceMappingURL=out.js.map
|
|
73
89
|
//# sourceMappingURL=preset.js.map
|
package/dist/esm/preset.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/preset.ts"],"names":[],"mappings":"AACA,YAAY,eAAe;AAC3B,YAAY,+BAA+B;AAC3C,SAAQ,sBAAqB;AAC7B,
|
|
1
|
+
{"version":3,"sources":["../../src/preset.ts"],"names":[],"mappings":"AACA,YAAY,eAAe;AAC3B,YAAY,sBAAsB;AAClC,YAAY,+BAA+B;AAC3C,SAAQ,sBAAqB;AAC7B;AAAA,EACE,UAAU;AAAA,EACV;AAAA,EACA;AAAA,OACK;AAgCA,MAAM,gCAAgC;AAAA;AAAA,wCAEL;AAAA,0CACE;AAAA;AAGnC,MAAM,SAAmD;AAAA,EAC9D,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,cAAc,QAAQ,aAAa,eAAe;AACxD,UAAM,uBACJ,QAAQ,aAAa,wBAAwB;AAC/C,UAAM,kBACJ,QAAQ,aAAa,mBACrB;AAEF,UAAM,yBACJ,QAAQ,aAAa,qBAAqB;AAAA,MACxC,WAAW;AAAA,MACX,cAAc;AAAA,IAChB,CAAC,KAAK;AAER,UAAM,YAAY;AAAA,MAChB,GAAG,QAAQ;AAAA,MACX,CAAC,QAAQ;AAAA,MACT,CAAC,eAAe;AAAA,MAChB,CAAC,0BAA0B;AAAA,MAC3B,CAAC,YAAY,EAAC,QAAQ,UAAS;AAAA,IACjC;AAEA,UAAM,UAAyC;AAAA,MAE7C;AAAA,QACE,CAAC,QAAQ;AAAA,UACP,SAAS;AAAA;AAAA;AAAA,QACX;AAAA,MACF;AAAA,MAEA,cACI;AAAA,QACE,CAAC,QAAQ;AAAA,UACP,SAAS,eAAe,8BAA8B;AAAA;AAAA,QACxD;AAAA,MACF,IACA;AAAA,QACE,CAAC,eAAe;AAAA,UACd,gBAAgB;AAAA,UAChB,sBAAsB;AAAA,UACtB,cAAc;AAAA,QAChB;AAAA,MACF;AAAA,MAEJ;AAAA,QACE,CAAC,0BAA0B;AAAA,UACzB,gBAAgB;AAAA,UAChB,iBAAiB;AAAA,UACjB,oBAAoB;AAAA,UACpB,cAAc,QAAQ,aAAa,4BAA4B;AAAA,UAC/D,sBAAsB,cAAc,uBAAuB;AAAA,QAC7D;AAAA,MACF;AAAA,MAEA,EAAC,CAAC,YAAY,EAAC,uBAAuB,uBAAsB,EAAC;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,UAEN,mBAAmB;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 {\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 defaultInterfaceExtensionCode = `\ndeclare module '@shopify/hydrogen' {\n interface StorefrontQueries extends ${GENERATED_QUERY_INTERFACE_NAME} {}\n interface StorefrontMutations extends ${GENERATED_MUTATION_INTERFACE_NAME} {}\n}`;\n\nexport const preset: Types.OutputPreset<HydrogenPresetConfig> = {\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 importTypes = options.presetConfig.importTypes ?? true;\n const namespacedImportName =\n options.presetConfig.namespacedImportName ?? 'StorefrontAPI';\n const importTypesFrom =\n options.presetConfig.importTypesFrom ??\n '@shopify/hydrogen/storefront-api-types';\n\n const interfaceExtensionCode =\n options.presetConfig.interfaceExtension?.({\n queryType: GENERATED_QUERY_INTERFACE_NAME,\n mutationType: GENERATED_MUTATION_INTERFACE_NAME,\n }) ?? defaultInterfaceExtensionCode;\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"]}
|
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"access": "public",
|
|
5
5
|
"@shopify:registry": "https://registry.npmjs.org"
|
|
6
6
|
},
|
|
7
|
-
"version": "0.0.
|
|
7
|
+
"version": "0.0.2",
|
|
8
8
|
"license": "MIT",
|
|
9
9
|
"type": "module",
|
|
10
10
|
"main": "dist/cjs/index.cjs",
|
|
@@ -43,6 +43,7 @@
|
|
|
43
43
|
},
|
|
44
44
|
"dependencies": {
|
|
45
45
|
"@graphql-codegen/add": "^4.0.1",
|
|
46
|
+
"@graphql-codegen/typescript": "^3.0.1",
|
|
46
47
|
"@graphql-codegen/typescript-operations": "^3.0.1"
|
|
47
48
|
},
|
|
48
49
|
"peerDependencies": {
|