From 6008b4d9ac8c570e6f8583873f9b567cf5fcf292 Mon Sep 17 00:00:00 2001 From: mizdra Date: Sun, 15 Sep 2024 22:03:46 +0900 Subject: [PATCH 1/2] add test case for #85 --- e2e/composite/.npmrc | 3 +++ e2e/composite/codegen.ts | 24 ++++++++++++++++++++++++ e2e/composite/package.json | 17 +++++++++++++++++ e2e/composite/schema.graphql | 4 ++++ e2e/composite/src/index.ts | 9 +++++++++ e2e/composite/tsconfig.json | 12 ++++++++++++ package.json | 5 ++++- 7 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 e2e/composite/.npmrc create mode 100644 e2e/composite/codegen.ts create mode 100644 e2e/composite/package.json create mode 100644 e2e/composite/schema.graphql create mode 100644 e2e/composite/src/index.ts create mode 100644 e2e/composite/tsconfig.json diff --git a/e2e/composite/.npmrc b/e2e/composite/.npmrc new file mode 100644 index 0000000..70ffab5 --- /dev/null +++ b/e2e/composite/.npmrc @@ -0,0 +1,3 @@ +# Options required to reproduce https://github.com/mizdra/graphql-codegen-typescript-fabbrica/issues/85 +install-links=true +package-lock=false diff --git a/e2e/composite/codegen.ts b/e2e/composite/codegen.ts new file mode 100644 index 0000000..89317b8 --- /dev/null +++ b/e2e/composite/codegen.ts @@ -0,0 +1,24 @@ +import type { CodegenConfig } from '@graphql-codegen/cli'; + +const config: CodegenConfig = { + schema: './schema.graphql', + generates: { + // The files other than those matching includes cannot be read when using `compisite`. + // Therefore, fabbrica artifacts are also output under `src/`. + 'src/__generated__/types.ts': { + plugins: ['typescript'], + config: { + enumsAsTypes: true, // required + avoidOptionals: true, // required + }, + }, + 'src/__generated__/fabbrica.ts': { + plugins: ['@mizdra/graphql-codegen-typescript-fabbrica'], + config: { + typesFile: './types.js', // required + }, + }, + }, +}; + +export default config; diff --git a/e2e/composite/package.json b/e2e/composite/package.json new file mode 100644 index 0000000..b59627e --- /dev/null +++ b/e2e/composite/package.json @@ -0,0 +1,17 @@ +{ + "name": "e2e-composite", + "type": "module", + "scripts": { + "start": "run-s clean _install gen build", + "clean": "node -e \"require('fs').rmSync('node_modules', { recursive: true, force: true })\"", + "_install": "npm install", + "gen": "graphql-codegen-esm", + "build": "tsc" + }, + "engines": { + "node": ">=18.0.0" + }, + "dependencies": { + "@mizdra/graphql-codegen-typescript-fabbrica": "../.." + } +} diff --git a/e2e/composite/schema.graphql b/e2e/composite/schema.graphql new file mode 100644 index 0000000..ab4cd3a --- /dev/null +++ b/e2e/composite/schema.graphql @@ -0,0 +1,4 @@ +type Book { + id: ID! + title: String! +} diff --git a/e2e/composite/src/index.ts b/e2e/composite/src/index.ts new file mode 100644 index 0000000..4aeeb74 --- /dev/null +++ b/e2e/composite/src/index.ts @@ -0,0 +1,9 @@ +import { defineBookFactory, dynamic } from './__generated__/fabbrica.js'; + +export const BookFactory = defineBookFactory({ + defaultFields: { + __typename: 'Book', + id: dynamic(({ seq }) => `Book-${seq}`), + title: 'Yuyushiki', + }, +}); diff --git a/e2e/composite/tsconfig.json b/e2e/composite/tsconfig.json new file mode 100644 index 0000000..93e0be1 --- /dev/null +++ b/e2e/composite/tsconfig.json @@ -0,0 +1,12 @@ +{ + "extends": "../../tsconfig.json", + "include": ["./src"], + "exclude": ["node_modules"], + "compilerOptions": { + "composite": true, + "declaration": true, + "noEmit": false, + "outDir": "dist", + "rootDir": "src" + } +} diff --git a/package.json b/package.json index 34366dc..b87df5f 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,10 @@ "lint:prettier": "prettier --check .", "test": "vitest", "pree2e": "npm run build", - "e2e": "npm -w e2e/esm -w e2e/cjs run start" + "e2e": "run-s -c e2e:*", + "e2e:esm": "npm -w e2e/esm run start", + "e2e:cjs": "npm -w e2e/cjs run start", + "e2e:composite": "npm --prefix e2e/composite run start" }, "prettier": "@mizdra/prettier-config-mizdra", "devDependencies": { From 5c68c53bed2a75fe74fd2522bdb8784be1992e35 Mon Sep 17 00:00:00 2001 From: mizdra Date: Sun, 15 Sep 2024 22:28:41 +0900 Subject: [PATCH 2/2] fix compile errors caused by `--composite` of TypeScript --- src/helper/index.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/helper/index.ts b/src/helper/index.ts index ba702ac..a95726a 100644 --- a/src/helper/index.ts +++ b/src/helper/index.ts @@ -1,3 +1,9 @@ -export { dynamic } from './field-resolver.js'; +export { dynamic, Dynamic } from './field-resolver.js'; export { resetAllSequence, resetSequence } from './sequence.js'; -export { DefineTypeFactoryInterface, DefineTypeFactoryInterfaceRequired, defineTypeFactory } from './factory.js'; +export { + DefineTypeFactoryInterface, + DefineTypeFactoryInterfaceRequired, + defineTypeFactory, + Traits, + TypeFactoryInterface, +} from './factory.js';