Skip to content

Commit

Permalink
Update codegen deps and remove patches (#1705)
Browse files Browse the repository at this point in the history
* Update codegen dependencies to fix known bugs

* Remove patch for codegen subdependencies

* Changesets

* Update package-lock
  • Loading branch information
frandiox authored Jan 31, 2024
1 parent ae47da4 commit c7b2017
Show file tree
Hide file tree
Showing 10 changed files with 1,200 additions and 1,862 deletions.
6 changes: 6 additions & 0 deletions .changeset/tidy-islands-complain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@shopify/hydrogen-codegen': patch
'@shopify/cli-hydrogen': patch
---

Bump Codegen dependencies to fix known bugs and remove patches.
2,190 changes: 1,176 additions & 1,014 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
},
"dependencies": {
"@ast-grep/napi": "0.11.0",
"@graphql-codegen/cli": "5.0.0",
"@graphql-codegen/cli": "5.0.1",
"@oclif/core": "2.11.7",
"@shopify/cli-kit": "3.52.0",
"@shopify/hydrogen-codegen": "^0.2.0",
Expand Down
11 changes: 1 addition & 10 deletions packages/cli/src/lib/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import {formatCode, getCodeFormatOptions} from './format-code.js';
import {renderFatalError, renderWarning} from '@shopify/cli-kit/node/ui';
import {joinPath, relativePath, basename} from '@shopify/cli-kit/node/path';
import {AbortError} from '@shopify/cli-kit/node/error';

// Do not import code synchronously from this dependency, it must be patched first
import type {LoadCodegenConfigResult} from '@graphql-codegen/cli';
import type {GraphQLConfig} from 'graphql-config';

Expand Down Expand Up @@ -103,14 +101,7 @@ type CodegenOptions = ProjectDirs & {
forceSfapiVersion?: string;
};

export async function codegen(options: CodegenOptions) {
await import('@shopify/hydrogen-codegen/patch').catch((error: Error) => {
throw new AbortError(
`Failed to patch dependencies for codegen.\n${error.stack}`,
'Please report this issue.',
);
});

export function codegen(options: CodegenOptions) {
return generateTypes(options).catch((error: Error) => {
const {message, details} = normalizeCodegenError(
error.message,
Expand Down
15 changes: 7 additions & 8 deletions packages/hydrogen-codegen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,18 @@
"directory": "packages/hydrogen-codegen"
},
"files": [
"dist",
"vendor"
"dist"
],
"devDependencies": {
"@graphql-codegen/cli": "^5.0.0",
"@graphql-codegen/plugin-helpers": "^5.0.0",
"@graphql-tools/utils": "^10.0.3",
"@graphql-codegen/cli": "^5.0.1",
"@graphql-codegen/plugin-helpers": "^5.0.2",
"@graphql-tools/utils": "^10.0.13",
"vitest": "^1.0.4"
},
"dependencies": {
"@graphql-codegen/add": "^5.0.0",
"@graphql-codegen/typescript": "^4.0.1",
"@graphql-codegen/typescript-operations": "^4.0.1",
"@graphql-codegen/add": "^5.0.1",
"@graphql-codegen/typescript": "^4.0.2",
"@graphql-codegen/typescript-operations": "^4.1.0",
"type-fest": "^4.5.0"
},
"peerDependencies": {
Expand Down
61 changes: 2 additions & 59 deletions packages/hydrogen-codegen/src/patch.ts
Original file line number Diff line number Diff line change
@@ -1,59 +1,2 @@
import path from 'node:path';
import fs from 'node:fs';
import {createRequire} from 'node:module';
import {fileURLToPath} from 'node:url';

/**
* Patch graphql-tag-pluck to allow it to work with `#graphql` comment and other features.
*/
const require = createRequire(import.meta.url);
const realGqlTagPluck = require.resolve('@graphql-tools/graphql-tag-pluck');
// During tests, this file is in src/xyz.ts but in dev/prod,
// the file is in dist/(esm|cjs)/xyz.js
const depth = path.extname(import.meta.url) === '.ts' ? '../' : '../../';
const vendorGqlTagPluck = fileURLToPath(
new URL(depth + '/vendor/graphql-tag-pluck', import.meta.url),
);

// Copy files sequencially to avoid `EBUSY` errors in Windows
fs.copyFileSync(
path.join(vendorGqlTagPluck, 'visitor.cjs'),
realGqlTagPluck.replace(/index\.js$/, 'visitor.js'),
);

fs.copyFileSync(
path.join(vendorGqlTagPluck, 'visitor.mjs'),
realGqlTagPluck.replace('cjs', 'esm').replace(/index\.js$/, 'visitor.js'),
);

/**
* Temporary patch for a bug in another package
* https://github.com/dotansimha/graphql-code-generator/pull/9709
*/
const visitorPluginCommon = require.resolve(
'@graphql-codegen/visitor-plugin-common',
);
const selectionSetToObjectFileCJS = visitorPluginCommon.replace(
'index.js',
'selection-set-to-object.js',
);
const selectionSetToObjectFileESM = selectionSetToObjectFileCJS.replace(
'cjs',
'esm',
);

fs.writeFileSync(
selectionSetToObjectFileCJS,
patchSelectionSet(fs.readFileSync(selectionSetToObjectFileCJS, 'utf-8')),
'utf-8',
);

fs.writeFileSync(
selectionSetToObjectFileESM,
patchSelectionSet(fs.readFileSync(selectionSetToObjectFileESM, 'utf-8')),
'utf-8',
);

function patchSelectionSet(content: string) {
return content.replace('&& s.union', '&& s?.union');
}
// Placeholder for subdependency patching
export {};
9 changes: 3 additions & 6 deletions packages/hydrogen-codegen/tests/codegen.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import {describe, it, expect} from 'vitest';
import path from 'node:path';
import {executeCodegen} from '@graphql-codegen/cli';
import {preset, schema, pluckConfig} from '../src/index.js';
import {getDefaultOptions} from '../src/defaults.js';

describe('Hydrogen Codegen', async () => {
// Patch dependency before importing the Codegen CLI
await import('../src/patch.js');
const {preset, schema, pluckConfig} = await import('../src/index.js');
const {getDefaultOptions} = await import('../src/defaults.js');
const {executeCodegen} = await import('@graphql-codegen/cli');

const getCodegenOptions = (fixture: string, output = 'out.d.ts') => ({
pluckConfig: pluckConfig as any,
generates: {
Expand Down
Loading

0 comments on commit c7b2017

Please sign in to comment.