@shopify/hydrogen-codegen 0.1.0 → 0.2.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.
- package/README.md +53 -0
- package/dist/cjs/client.cjs +4 -0
- package/dist/cjs/client.cjs.map +1 -0
- package/dist/cjs/defaults.cjs +31 -0
- package/dist/cjs/defaults.cjs.map +1 -0
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/patch.cjs +0 -49
- package/dist/cjs/patch.cjs.map +1 -1
- package/dist/cjs/preset.cjs +6 -9
- package/dist/cjs/preset.cjs.map +1 -1
- package/dist/cjs/schema.cjs +11 -4
- package/dist/cjs/schema.cjs.map +1 -1
- package/dist/esm/client.js +3 -0
- package/dist/esm/client.js.map +1 -0
- package/dist/esm/defaults.js +29 -0
- package/dist/esm/defaults.js.map +1 -0
- package/dist/esm/index.d.ts +142 -8
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/patch.js +0 -42
- package/dist/esm/patch.js.map +1 -1
- package/dist/esm/preset.js +7 -9
- package/dist/esm/preset.js.map +1 -1
- package/dist/esm/schema.js +11 -4
- package/dist/esm/schema.js.map +1 -1
- package/package.json +12 -12
- package/dist/esm/pluck.d.ts +0 -22
- package/dist/esm/plugin.d.ts +0 -20
- package/dist/esm/preset.d.ts +0 -35
- package/dist/esm/schema.d.ts +0 -4
- package/dist/esm/sources.d.ts +0 -8
- package/vendor/graphql-tag-pluck/visitor.cjs +0 -377
- package/vendor/graphql-tag-pluck/visitor.mjs +0 -383
package/README.md
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# Hydrogen Codegen
|
|
2
|
+
|
|
3
|
+
A codegen plugin and preset for generating TypeScript types from GraphQL queries in a `d.ts` file. It does not require any function wrapper and adds no runtime overhead (0 bytes to the bundle).
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
const {shop} = await client.query(`#graphql
|
|
7
|
+
query {
|
|
8
|
+
shop {
|
|
9
|
+
name
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
`);
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
The GraphQL client must use TypeScript interfaces that are extended in the generated `d.ts` file. See an example in [Hydrogen's Storefront client](https://github.com/Shopify/hydrogen/blob/081b41e0d43c9e1090933e908362625b9dfe7166/packages/hydrogen/src/storefront.ts#L58-L143).
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
When using Hydrogen CLI, this package is already included and configured for you to generate types for the Shopify Storefront API. However, if you want to use it standalone with the GraphQL CLI or just want to add other APIs to Hydrogen, you can use the following example configuration:
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
// <root>/codegen.ts
|
|
23
|
+
|
|
24
|
+
import type {CodegenConfig} from '@graphql-codegen/cli';
|
|
25
|
+
import {pluckConfig, preset, getSchema} from '@shopify/hydrogen-codegen';
|
|
26
|
+
|
|
27
|
+
export default {
|
|
28
|
+
overwrite: true,
|
|
29
|
+
pluckConfig,
|
|
30
|
+
generates: {
|
|
31
|
+
'storefrontapi.generated.d.ts': {
|
|
32
|
+
preset,
|
|
33
|
+
schema: getSchema('storefront'),
|
|
34
|
+
documents: [
|
|
35
|
+
'./*.{ts,tsx,js,jsx}',
|
|
36
|
+
'./app/**/*.{ts,tsx,js,jsx}',
|
|
37
|
+
'!./app/graphql/customer-account/*.{ts,tsx,js,jsx}',
|
|
38
|
+
'!./app/graphql/my-cms/*.{ts,tsx,js,jsx}',
|
|
39
|
+
],
|
|
40
|
+
},
|
|
41
|
+
'customeraccountapi.generated.d.ts': {
|
|
42
|
+
preset,
|
|
43
|
+
schema: getSchema('customer-account'),
|
|
44
|
+
documents: ['./app/graphql/customer-account/*.{ts,tsx,js,jsx}'],
|
|
45
|
+
},
|
|
46
|
+
'mycms.generated.d.ts': {
|
|
47
|
+
preset,
|
|
48
|
+
schema: './my-cms.json',
|
|
49
|
+
documents: ['./app/graphql/my-cms/*.{ts,tsx,js,jsx}'],
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
} as CodegenConfig;
|
|
53
|
+
```
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":""}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var plugin_js = require('./plugin.cjs');
|
|
4
|
+
|
|
5
|
+
const sfapiDefaultInterfaceExtensionCode = `
|
|
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} {}
|
|
9
|
+
}`;
|
|
10
|
+
const caapiDefaultInterfaceExtensionCode = `
|
|
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} {}
|
|
14
|
+
}`;
|
|
15
|
+
const sfapiDefaultValues = {
|
|
16
|
+
importTypesFrom: "@shopify/hydrogen/storefront-api-types",
|
|
17
|
+
namespacedImportName: "StorefrontAPI",
|
|
18
|
+
interfaceExtensionCode: sfapiDefaultInterfaceExtensionCode
|
|
19
|
+
};
|
|
20
|
+
const caapiDefaultValues = {
|
|
21
|
+
importTypesFrom: "@shopify/hydrogen/customer-account-api-types",
|
|
22
|
+
namespacedImportName: "CustomerAccountAPI",
|
|
23
|
+
interfaceExtensionCode: caapiDefaultInterfaceExtensionCode
|
|
24
|
+
};
|
|
25
|
+
function getDefaultOptions(outputFile = "") {
|
|
26
|
+
return /^(customer|caapi\.)/i.test(outputFile) ? caapiDefaultValues : sfapiDefaultValues;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
exports.getDefaultOptions = getDefaultOptions;
|
|
30
|
+
//# sourceMappingURL=out.js.map
|
|
31
|
+
//# sourceMappingURL=defaults.cjs.map
|
|
@@ -0,0 +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"]}
|
package/dist/cjs/index.cjs.map
CHANGED
|
@@ -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';\n"]}
|
|
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"]}
|
package/dist/cjs/patch.cjs
CHANGED
|
@@ -1,53 +1,4 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var path = require('node:path');
|
|
4
|
-
var fs = require('node:fs');
|
|
5
|
-
var node_module = require('node:module');
|
|
6
|
-
var node_url = require('node:url');
|
|
7
|
-
|
|
8
|
-
var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
|
|
9
|
-
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
10
|
-
|
|
11
|
-
var path__default = /*#__PURE__*/_interopDefault(path);
|
|
12
|
-
var fs__default = /*#__PURE__*/_interopDefault(fs);
|
|
13
|
-
|
|
14
|
-
const require2 = node_module.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.src || new URL('out.js', document.baseURI).href)));
|
|
15
|
-
const realGqlTagPluck = require2.resolve("@graphql-tools/graphql-tag-pluck");
|
|
16
|
-
const depth = path__default.default.extname((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.src || new URL('out.js', document.baseURI).href))) === ".ts" ? "../" : "../../";
|
|
17
|
-
const vendorGqlTagPluck = node_url.fileURLToPath(
|
|
18
|
-
new URL(depth + "/vendor/graphql-tag-pluck", (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.src || new URL('out.js', document.baseURI).href)))
|
|
19
|
-
);
|
|
20
|
-
fs__default.default.copyFileSync(
|
|
21
|
-
path__default.default.join(vendorGqlTagPluck, "visitor.cjs"),
|
|
22
|
-
realGqlTagPluck.replace(/index\.js$/, "visitor.js")
|
|
23
|
-
);
|
|
24
|
-
fs__default.default.copyFileSync(
|
|
25
|
-
path__default.default.join(vendorGqlTagPluck, "visitor.mjs"),
|
|
26
|
-
realGqlTagPluck.replace("cjs", "esm").replace(/index\.js$/, "visitor.js")
|
|
27
|
-
);
|
|
28
|
-
const visitorPluginCommon = require2.resolve(
|
|
29
|
-
"@graphql-codegen/visitor-plugin-common"
|
|
30
|
-
);
|
|
31
|
-
const selectionSetToObjectFileCJS = visitorPluginCommon.replace(
|
|
32
|
-
"index.js",
|
|
33
|
-
"selection-set-to-object.js"
|
|
34
|
-
);
|
|
35
|
-
const selectionSetToObjectFileESM = selectionSetToObjectFileCJS.replace(
|
|
36
|
-
"cjs",
|
|
37
|
-
"esm"
|
|
38
|
-
);
|
|
39
|
-
fs__default.default.writeFileSync(
|
|
40
|
-
selectionSetToObjectFileCJS,
|
|
41
|
-
patchSelectionSet(fs__default.default.readFileSync(selectionSetToObjectFileCJS, "utf-8")),
|
|
42
|
-
"utf-8"
|
|
43
|
-
);
|
|
44
|
-
fs__default.default.writeFileSync(
|
|
45
|
-
selectionSetToObjectFileESM,
|
|
46
|
-
patchSelectionSet(fs__default.default.readFileSync(selectionSetToObjectFileESM, "utf-8")),
|
|
47
|
-
"utf-8"
|
|
48
|
-
);
|
|
49
|
-
function patchSelectionSet(content) {
|
|
50
|
-
return content.replace("&& s.union", "&& s?.union");
|
|
51
|
-
}
|
|
52
3
|
//# sourceMappingURL=out.js.map
|
|
53
4
|
//# sourceMappingURL=patch.cjs.map
|
package/dist/cjs/patch.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":[
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":""}
|
package/dist/cjs/preset.cjs
CHANGED
|
@@ -4,6 +4,7 @@ var addPlugin = require('@graphql-codegen/add');
|
|
|
4
4
|
var typescriptPlugin = require('@graphql-codegen/typescript');
|
|
5
5
|
var typescriptOperationPlugin = require('@graphql-codegen/typescript-operations');
|
|
6
6
|
var sources_js = require('./sources.cjs');
|
|
7
|
+
var defaults_js = require('./defaults.cjs');
|
|
7
8
|
var plugin_js = require('./plugin.cjs');
|
|
8
9
|
|
|
9
10
|
function _interopNamespace(e) {
|
|
@@ -28,12 +29,8 @@ var addPlugin__namespace = /*#__PURE__*/_interopNamespace(addPlugin);
|
|
|
28
29
|
var typescriptPlugin__namespace = /*#__PURE__*/_interopNamespace(typescriptPlugin);
|
|
29
30
|
var typescriptOperationPlugin__namespace = /*#__PURE__*/_interopNamespace(typescriptOperationPlugin);
|
|
30
31
|
|
|
31
|
-
const defaultInterfaceExtensionCode = `
|
|
32
|
-
declare module '@shopify/hydrogen' {
|
|
33
|
-
interface StorefrontQueries extends ${plugin_js.GENERATED_QUERY_INTERFACE_NAME} {}
|
|
34
|
-
interface StorefrontMutations extends ${plugin_js.GENERATED_MUTATION_INTERFACE_NAME} {}
|
|
35
|
-
}`;
|
|
36
32
|
const preset = {
|
|
33
|
+
[Symbol.for("name")]: "hydrogen",
|
|
37
34
|
buildGeneratesSection: (options) => {
|
|
38
35
|
if (!options.baseOutputDir.endsWith(".d.ts")) {
|
|
39
36
|
throw new Error("[hydrogen-preset] target output should be a .d.ts file");
|
|
@@ -45,13 +42,14 @@ const preset = {
|
|
|
45
42
|
}
|
|
46
43
|
const sourcesWithOperations = sources_js.processSources(options.documents);
|
|
47
44
|
const sources = sourcesWithOperations.map(({ source }) => source);
|
|
45
|
+
const defaultOptions = defaults_js.getDefaultOptions(options.baseOutputDir);
|
|
48
46
|
const importTypes = options.presetConfig.importTypes ?? true;
|
|
49
|
-
const namespacedImportName = options.presetConfig.namespacedImportName ??
|
|
50
|
-
const importTypesFrom = options.presetConfig.importTypesFrom ??
|
|
47
|
+
const namespacedImportName = options.presetConfig.namespacedImportName ?? defaultOptions.namespacedImportName;
|
|
48
|
+
const importTypesFrom = options.presetConfig.importTypesFrom ?? defaultOptions.importTypesFrom;
|
|
51
49
|
const interfaceExtensionCode = options.presetConfig.interfaceExtension?.({
|
|
52
50
|
queryType: plugin_js.GENERATED_QUERY_INTERFACE_NAME,
|
|
53
51
|
mutationType: plugin_js.GENERATED_MUTATION_INTERFACE_NAME
|
|
54
|
-
}) ??
|
|
52
|
+
}) ?? defaultOptions.interfaceExtensionCode;
|
|
55
53
|
const pluginMap = {
|
|
56
54
|
...options.pluginMap,
|
|
57
55
|
[`add`]: addPlugin__namespace,
|
|
@@ -119,7 +117,6 @@ const preset = {
|
|
|
119
117
|
}
|
|
120
118
|
};
|
|
121
119
|
|
|
122
|
-
exports.defaultInterfaceExtensionCode = defaultInterfaceExtensionCode;
|
|
123
120
|
exports.preset = preset;
|
|
124
121
|
//# sourceMappingURL=out.js.map
|
|
125
122
|
//# 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,sBAAsB;AAClC,YAAY,+BAA+B;AAC3C,SAAQ,sBAAqB;AAC7B;AAAA,EACE,UAAU;AAAA,EACV;AAAA,EACA;AAAA,OACK;AAgCA,MAAM,
|
|
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"]}
|
package/dist/cjs/schema.cjs
CHANGED
|
@@ -1,13 +1,20 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
//! import {createRequire} from 'module'; const require = createRequire(import.meta.url);
|
|
4
|
-
const getSchema = (
|
|
5
|
-
|
|
4
|
+
const getSchema = (api = "storefront") => {
|
|
5
|
+
if (api !== "storefront" && api !== "customer-account") {
|
|
6
|
+
throw new Error(
|
|
7
|
+
`The provided API type "${api}" is unknown. Please use "storefront" or "customer-account".`
|
|
8
|
+
);
|
|
9
|
+
}
|
|
10
|
+
return require.resolve(`@shopify/hydrogen-react/${api}.schema.json`);
|
|
11
|
+
};
|
|
12
|
+
let staticSFAPISchema = "";
|
|
6
13
|
try {
|
|
7
|
-
|
|
14
|
+
staticSFAPISchema = getSchema("storefront");
|
|
8
15
|
} catch (error) {
|
|
9
16
|
}
|
|
10
|
-
const schema =
|
|
17
|
+
const schema = staticSFAPISchema;
|
|
11
18
|
|
|
12
19
|
exports.getSchema = getSchema;
|
|
13
20
|
exports.schema = schema;
|
package/dist/cjs/schema.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/schema.ts"],"names":[],"mappings":"AACA;
|
|
1
|
+
{"version":3,"sources":["../../src/schema.ts"],"names":[],"mappings":"AACA;AAQO,MAAM,YAAY,CACvB,MAAM,iBACH;AACH,MAAI,QAAQ,gBAAgB,QAAQ,oBAAoB;AACtD,UAAM,IAAI;AAAA,MACR,0BAA0B,GAAG;AAAA,IAC/B;AAAA,EACF;AAEA,SAAO,QAAQ,QAAQ,2BAA2B,GAAG,cAAc;AACrE;AAEA,IAAI,oBAAoB;AAExB,IAAI;AACF,sBAAoB,UAAU,YAAY;AAC5C,SAAS,OAAO;AAIhB;AAKO,MAAM,SAAS","sourcesContent":["// This comment is used during ESM build:\n//! import {createRequire} from 'module'; const require = createRequire(import.meta.url);\n\n/**\n * Resolves a schema path for the provided API type. Only the API types currently\n * bundled in Hydrogen are allowed: \"storefront\" and \"customer\".\n * @param api\n * @returns\n */\nexport const getSchema = (\n api = 'storefront' as 'storefront' | 'customer-account',\n) => {\n if (api !== 'storefront' && api !== 'customer-account') {\n throw new Error(\n `The provided API type \"${api}\" is unknown. Please use \"storefront\" or \"customer-account\".`,\n );\n }\n\n return require.resolve(`@shopify/hydrogen-react/${api}.schema.json`);\n};\n\nlet staticSFAPISchema = '';\n\ntry {\n staticSFAPISchema = getSchema('storefront');\n} catch (error) {\n // This can happen at build time or when '@shopify/hydrogen-react' is not found.\n // Generally this shouldn't be an issue in real apps so let's ignore the error.\n // Also, this package could be used in non-Hydrogen apps.\n}\n\n/**\n * The resolved schema path for the Storefront API.\n */\nexport const schema = staticSFAPISchema;\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":""}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { GENERATED_QUERY_INTERFACE_NAME, GENERATED_MUTATION_INTERFACE_NAME } from './plugin.js';
|
|
2
|
+
|
|
3
|
+
const sfapiDefaultInterfaceExtensionCode = `
|
|
4
|
+
declare module '@shopify/hydrogen' {
|
|
5
|
+
interface StorefrontQueries extends ${GENERATED_QUERY_INTERFACE_NAME} {}
|
|
6
|
+
interface StorefrontMutations extends ${GENERATED_MUTATION_INTERFACE_NAME} {}
|
|
7
|
+
}`;
|
|
8
|
+
const caapiDefaultInterfaceExtensionCode = `
|
|
9
|
+
declare module '@shopify/hydrogen' {
|
|
10
|
+
interface CustomerAccountQueries extends ${GENERATED_QUERY_INTERFACE_NAME} {}
|
|
11
|
+
interface CustomerAccountMutations extends ${GENERATED_MUTATION_INTERFACE_NAME} {}
|
|
12
|
+
}`;
|
|
13
|
+
const sfapiDefaultValues = {
|
|
14
|
+
importTypesFrom: "@shopify/hydrogen/storefront-api-types",
|
|
15
|
+
namespacedImportName: "StorefrontAPI",
|
|
16
|
+
interfaceExtensionCode: sfapiDefaultInterfaceExtensionCode
|
|
17
|
+
};
|
|
18
|
+
const caapiDefaultValues = {
|
|
19
|
+
importTypesFrom: "@shopify/hydrogen/customer-account-api-types",
|
|
20
|
+
namespacedImportName: "CustomerAccountAPI",
|
|
21
|
+
interfaceExtensionCode: caapiDefaultInterfaceExtensionCode
|
|
22
|
+
};
|
|
23
|
+
function getDefaultOptions(outputFile = "") {
|
|
24
|
+
return /^(customer|caapi\.)/i.test(outputFile) ? caapiDefaultValues : sfapiDefaultValues;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export { getDefaultOptions };
|
|
28
|
+
//# sourceMappingURL=out.js.map
|
|
29
|
+
//# sourceMappingURL=defaults.js.map
|
|
@@ -0,0 +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"]}
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -1,8 +1,142 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
import { Types, PluginFunction } from '@graphql-codegen/plugin-helpers';
|
|
2
|
+
import { Source } from '@graphql-tools/utils';
|
|
3
|
+
import { OperationDefinitionNode, FragmentDefinitionNode, ExecutionArgs } from 'graphql';
|
|
4
|
+
import { IsNever, SetOptional } from 'type-fest';
|
|
5
|
+
|
|
6
|
+
type HydrogenPresetConfig = {
|
|
7
|
+
/**
|
|
8
|
+
* Name for the variable that contains the imported types.
|
|
9
|
+
* @default 'StorefrontAPI'
|
|
10
|
+
*/
|
|
11
|
+
namespacedImportName?: string;
|
|
12
|
+
/**
|
|
13
|
+
* Module to import the types from.
|
|
14
|
+
* @default '@shopify/hydrogen/storefront-api-types'
|
|
15
|
+
*/
|
|
16
|
+
importTypesFrom?: string;
|
|
17
|
+
/**
|
|
18
|
+
* Whether types should be imported from the `importTypesFrom` module, or generated inline.
|
|
19
|
+
* @default true
|
|
20
|
+
*/
|
|
21
|
+
importTypes?: boolean;
|
|
22
|
+
/**
|
|
23
|
+
* Whether to skip adding `__typename` to generated operation types.
|
|
24
|
+
* @default true
|
|
25
|
+
*/
|
|
26
|
+
skipTypenameInOperations?: boolean;
|
|
27
|
+
/**
|
|
28
|
+
* Override the default interface extension.
|
|
29
|
+
*/
|
|
30
|
+
interfaceExtension?: (options: {
|
|
31
|
+
queryType: string;
|
|
32
|
+
mutationType: string;
|
|
33
|
+
}) => string;
|
|
34
|
+
};
|
|
35
|
+
declare const preset: Types.OutputPreset<HydrogenPresetConfig>;
|
|
36
|
+
|
|
37
|
+
type OperationOrFragment = {
|
|
38
|
+
initialName: string;
|
|
39
|
+
definition: OperationDefinitionNode | FragmentDefinitionNode;
|
|
40
|
+
};
|
|
41
|
+
type SourceWithOperations = {
|
|
42
|
+
source: Source;
|
|
43
|
+
operations: Array<OperationOrFragment>;
|
|
44
|
+
};
|
|
45
|
+
declare const plugin: PluginFunction<{
|
|
46
|
+
sourcesWithOperations: Array<SourceWithOperations>;
|
|
47
|
+
interfaceExtensionCode: string;
|
|
48
|
+
}>;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Resolves a schema path for the provided API type. Only the API types currently
|
|
52
|
+
* bundled in Hydrogen are allowed: "storefront" and "customer".
|
|
53
|
+
* @param api
|
|
54
|
+
* @returns
|
|
55
|
+
*/
|
|
56
|
+
declare const getSchema: (api?: "storefront" | "customer-account") => string;
|
|
57
|
+
/**
|
|
58
|
+
* The resolved schema path for the Storefront API.
|
|
59
|
+
*/
|
|
60
|
+
declare const schema: string;
|
|
61
|
+
|
|
62
|
+
declare function processSources(sources: Array<Source>, buildName?: (node: OperationDefinitionNode | FragmentDefinitionNode) => string): SourceWithOperations[];
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* This is a modified version of graphql-tag-pluck's default config.
|
|
66
|
+
* https://github.com/ardatan/graphql-tools/issues/5127
|
|
67
|
+
*/
|
|
68
|
+
declare const pluckConfig: {
|
|
69
|
+
/**
|
|
70
|
+
* Hook to determine if a node is a gql template literal.
|
|
71
|
+
* By default, graphql-tag-pluck only looks for leading comments or `gql` tag.
|
|
72
|
+
*/
|
|
73
|
+
isGqlTemplateLiteral: (node: any, options: any) => boolean;
|
|
74
|
+
/**
|
|
75
|
+
* Instruct how to extract the gql template literal from the code.
|
|
76
|
+
* By default, embedded expressions in template literals (e.g. ${foo})
|
|
77
|
+
* are removed from the template string. This hook allows us to annotate
|
|
78
|
+
* the template string with the required embedded expressions instead of
|
|
79
|
+
* removing them. Later, we can use this information to reconstruct the
|
|
80
|
+
* embedded expressions.
|
|
81
|
+
*/
|
|
82
|
+
pluckStringFromFile: (code: string, { start, end, leadingComments }: any) => string;
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* This file has utilities to create GraphQL clients
|
|
87
|
+
* that consume the types generated by the Hydrogen preset.
|
|
88
|
+
*/
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* A generic type for `variables` in GraphQL clients
|
|
92
|
+
*/
|
|
93
|
+
type GenericVariables = ExecutionArgs['variableValues'];
|
|
94
|
+
/**
|
|
95
|
+
* Use this type to make parameters optional in GraphQL clients
|
|
96
|
+
* when no variables need to be passed.
|
|
97
|
+
*/
|
|
98
|
+
type EmptyVariables = {
|
|
99
|
+
[key: string]: never;
|
|
100
|
+
};
|
|
101
|
+
/**
|
|
102
|
+
* GraphQL client's generic operation interface.
|
|
103
|
+
*/
|
|
104
|
+
interface CodegenOperations {
|
|
105
|
+
[key: string]: any;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Used as the return type for GraphQL clients. It picks
|
|
109
|
+
* the return type from the generated operation types.
|
|
110
|
+
* @example
|
|
111
|
+
* graphqlQuery: (...) => Promise<ClientReturn<...>>
|
|
112
|
+
* graphqlQuery: (...) => Promise<{data: ClientReturn<...>}>
|
|
113
|
+
*/
|
|
114
|
+
type ClientReturn<GeneratedOperations extends CodegenOperations, RawGqlString extends string, OverrideReturnType extends any = never> = IsNever<OverrideReturnType> extends true ? RawGqlString extends keyof GeneratedOperations ? GeneratedOperations[RawGqlString]['return'] : any : OverrideReturnType;
|
|
115
|
+
/**
|
|
116
|
+
* Checks if the generated variables for an operation
|
|
117
|
+
* are optional or required.
|
|
118
|
+
*/
|
|
119
|
+
type IsOptionalVariables<VariablesParam, OptionalVariableNames extends string = never, VariablesWithoutOptionals = Omit<VariablesParam, OptionalVariableNames>> = VariablesWithoutOptionals extends EmptyVariables ? true : GenericVariables extends VariablesParam ? true : Partial<VariablesWithoutOptionals> extends VariablesWithoutOptionals ? true : false;
|
|
120
|
+
/**
|
|
121
|
+
* Used as the type for the GraphQL client's variables. It checks
|
|
122
|
+
* the generated operation types to see if variables are optional.
|
|
123
|
+
* @example
|
|
124
|
+
* graphqlQuery: (query: string, param: ClientVariables<...>) => Promise<...>
|
|
125
|
+
* Where `param` is required.
|
|
126
|
+
*/
|
|
127
|
+
type ClientVariables<GeneratedOperations extends CodegenOperations, RawGqlString extends string, OptionalVariableNames extends string = never, VariablesKey extends string = 'variables', GeneratedVariables = RawGqlString extends keyof GeneratedOperations ? SetOptional<GeneratedOperations[RawGqlString]['variables'], Extract<keyof GeneratedOperations[RawGqlString]['variables'], OptionalVariableNames>> : GenericVariables, VariablesWrapper = Record<VariablesKey, GeneratedVariables>> = IsOptionalVariables<GeneratedVariables, OptionalVariableNames> extends true ? Partial<VariablesWrapper> : VariablesWrapper;
|
|
128
|
+
/**
|
|
129
|
+
* Similar to ClientVariables, but makes the whole wrapper optional:
|
|
130
|
+
* @example
|
|
131
|
+
* graphqlQuery: (query: string, ...params: ClientVariablesInRestParams<...>) => Promise<...>
|
|
132
|
+
* Where the first item in `params` might be optional depending on the query.
|
|
133
|
+
*/
|
|
134
|
+
type ClientVariablesInRestParams<GeneratedOperations extends CodegenOperations, RawGqlString extends string, OtherParams extends Record<string, any> = {}, OptionalVariableNames extends string = never, ProcessedVariables = OtherParams & ClientVariables<GeneratedOperations, RawGqlString, OptionalVariableNames>> = Partial<OtherParams> extends OtherParams ? IsOptionalVariables<GeneratedOperations[RawGqlString]['variables'], OptionalVariableNames> extends true ? [
|
|
135
|
+
ProcessedVariables?
|
|
136
|
+
] : [
|
|
137
|
+
ProcessedVariables
|
|
138
|
+
] : [
|
|
139
|
+
ProcessedVariables
|
|
140
|
+
];
|
|
141
|
+
|
|
142
|
+
export { ClientReturn, ClientVariables, ClientVariablesInRestParams, EmptyVariables, GenericVariables, IsOptionalVariables, getSchema, pluckConfig, plugin, preset, processSources, schema };
|
package/dist/esm/index.js.map
CHANGED
|
@@ -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';\n"]}
|
|
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"]}
|
package/dist/esm/patch.js
CHANGED
|
@@ -1,45 +1,3 @@
|
|
|
1
|
-
import path from 'node:path';
|
|
2
|
-
import fs from 'node:fs';
|
|
3
|
-
import { createRequire } from 'node:module';
|
|
4
|
-
import { fileURLToPath } from 'node:url';
|
|
5
1
|
|
|
6
|
-
const require2 = createRequire(import.meta.url);
|
|
7
|
-
const realGqlTagPluck = require2.resolve("@graphql-tools/graphql-tag-pluck");
|
|
8
|
-
const depth = path.extname(import.meta.url) === ".ts" ? "../" : "../../";
|
|
9
|
-
const vendorGqlTagPluck = fileURLToPath(
|
|
10
|
-
new URL(depth + "/vendor/graphql-tag-pluck", import.meta.url)
|
|
11
|
-
);
|
|
12
|
-
fs.copyFileSync(
|
|
13
|
-
path.join(vendorGqlTagPluck, "visitor.cjs"),
|
|
14
|
-
realGqlTagPluck.replace(/index\.js$/, "visitor.js")
|
|
15
|
-
);
|
|
16
|
-
fs.copyFileSync(
|
|
17
|
-
path.join(vendorGqlTagPluck, "visitor.mjs"),
|
|
18
|
-
realGqlTagPluck.replace("cjs", "esm").replace(/index\.js$/, "visitor.js")
|
|
19
|
-
);
|
|
20
|
-
const visitorPluginCommon = require2.resolve(
|
|
21
|
-
"@graphql-codegen/visitor-plugin-common"
|
|
22
|
-
);
|
|
23
|
-
const selectionSetToObjectFileCJS = visitorPluginCommon.replace(
|
|
24
|
-
"index.js",
|
|
25
|
-
"selection-set-to-object.js"
|
|
26
|
-
);
|
|
27
|
-
const selectionSetToObjectFileESM = selectionSetToObjectFileCJS.replace(
|
|
28
|
-
"cjs",
|
|
29
|
-
"esm"
|
|
30
|
-
);
|
|
31
|
-
fs.writeFileSync(
|
|
32
|
-
selectionSetToObjectFileCJS,
|
|
33
|
-
patchSelectionSet(fs.readFileSync(selectionSetToObjectFileCJS, "utf-8")),
|
|
34
|
-
"utf-8"
|
|
35
|
-
);
|
|
36
|
-
fs.writeFileSync(
|
|
37
|
-
selectionSetToObjectFileESM,
|
|
38
|
-
patchSelectionSet(fs.readFileSync(selectionSetToObjectFileESM, "utf-8")),
|
|
39
|
-
"utf-8"
|
|
40
|
-
);
|
|
41
|
-
function patchSelectionSet(content) {
|
|
42
|
-
return content.replace("&& s.union", "&& s?.union");
|
|
43
|
-
}
|
|
44
2
|
//# sourceMappingURL=out.js.map
|
|
45
3
|
//# sourceMappingURL=patch.js.map
|
package/dist/esm/patch.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":[
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":""}
|
package/dist/esm/preset.js
CHANGED
|
@@ -2,14 +2,11 @@ import * as addPlugin from '@graphql-codegen/add';
|
|
|
2
2
|
import * as typescriptPlugin from '@graphql-codegen/typescript';
|
|
3
3
|
import * as typescriptOperationPlugin from '@graphql-codegen/typescript-operations';
|
|
4
4
|
import { processSources } from './sources.js';
|
|
5
|
+
import { getDefaultOptions } from './defaults.js';
|
|
5
6
|
import { GENERATED_QUERY_INTERFACE_NAME, GENERATED_MUTATION_INTERFACE_NAME, plugin } from './plugin.js';
|
|
6
7
|
|
|
7
|
-
const defaultInterfaceExtensionCode = `
|
|
8
|
-
declare module '@shopify/hydrogen' {
|
|
9
|
-
interface StorefrontQueries extends ${GENERATED_QUERY_INTERFACE_NAME} {}
|
|
10
|
-
interface StorefrontMutations extends ${GENERATED_MUTATION_INTERFACE_NAME} {}
|
|
11
|
-
}`;
|
|
12
8
|
const preset = {
|
|
9
|
+
[Symbol.for("name")]: "hydrogen",
|
|
13
10
|
buildGeneratesSection: (options) => {
|
|
14
11
|
if (!options.baseOutputDir.endsWith(".d.ts")) {
|
|
15
12
|
throw new Error("[hydrogen-preset] target output should be a .d.ts file");
|
|
@@ -21,13 +18,14 @@ const preset = {
|
|
|
21
18
|
}
|
|
22
19
|
const sourcesWithOperations = processSources(options.documents);
|
|
23
20
|
const sources = sourcesWithOperations.map(({ source }) => source);
|
|
21
|
+
const defaultOptions = getDefaultOptions(options.baseOutputDir);
|
|
24
22
|
const importTypes = options.presetConfig.importTypes ?? true;
|
|
25
|
-
const namespacedImportName = options.presetConfig.namespacedImportName ??
|
|
26
|
-
const importTypesFrom = options.presetConfig.importTypesFrom ??
|
|
23
|
+
const namespacedImportName = options.presetConfig.namespacedImportName ?? defaultOptions.namespacedImportName;
|
|
24
|
+
const importTypesFrom = options.presetConfig.importTypesFrom ?? defaultOptions.importTypesFrom;
|
|
27
25
|
const interfaceExtensionCode = options.presetConfig.interfaceExtension?.({
|
|
28
26
|
queryType: GENERATED_QUERY_INTERFACE_NAME,
|
|
29
27
|
mutationType: GENERATED_MUTATION_INTERFACE_NAME
|
|
30
|
-
}) ??
|
|
28
|
+
}) ?? defaultOptions.interfaceExtensionCode;
|
|
31
29
|
const pluginMap = {
|
|
32
30
|
...options.pluginMap,
|
|
33
31
|
[`add`]: addPlugin,
|
|
@@ -95,6 +93,6 @@ const preset = {
|
|
|
95
93
|
}
|
|
96
94
|
};
|
|
97
95
|
|
|
98
|
-
export {
|
|
96
|
+
export { preset };
|
|
99
97
|
//# sourceMappingURL=out.js.map
|
|
100
98
|
//# 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,sBAAsB;AAClC,YAAY,+BAA+B;AAC3C,SAAQ,sBAAqB;AAC7B;AAAA,EACE,UAAU;AAAA,EACV;AAAA,EACA;AAAA,OACK;AAgCA,MAAM,
|
|
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"]}
|