Skip to content

Commit

Permalink
Merge pull request #51 from mizdra/refactoring
Browse files Browse the repository at this point in the history
Refactoring
  • Loading branch information
mizdra authored Oct 2, 2023
2 parents 63045a9 + 077a10b commit 213f008
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 138 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ However, you must prepare a custom `define<Type>Factory` to use Transient Fields
import {
defineAuthorFactoryInternal,
dynamic,
DefaultFieldsResolver,
FieldsResolver,
Traits,
AuthorFactoryDefineOptions,
AuthorFactoryInterface,
Expand All @@ -272,7 +272,7 @@ type AuthorTransientFields = {
bookCount: number;
};
function defineAuthorFactoryWithTransientFields<
_DefaultFieldsResolver extends DefaultFieldsResolver<Author & AuthorTransientFields>,
_DefaultFieldsResolver extends FieldsResolver<Author & AuthorTransientFields>,
_Traits extends Traits<Author, AuthorTransientFields>,
>(
options: AuthorFactoryDefineOptions<AuthorTransientFields, _DefaultFieldsResolver, _Traits>,
Expand Down
4 changes: 2 additions & 2 deletions e2e/index.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { expect, it, describe, assertType, expectTypeOf, vi } from 'vitest';
import {
AuthorFactoryDefineOptions,
AuthorFactoryInterface,
DefaultFieldsResolver,
FieldsResolver,
Traits,
defineAuthorFactory,
defineAuthorFactoryInternal,
Expand Down Expand Up @@ -516,7 +516,7 @@ describe('defineTypeFactory', () => {
bookCount: number;
};
function defineAuthorFactoryWithTransientFields<
_DefaultFieldsResolver extends DefaultFieldsResolver<OptionalAuthor & AuthorTransientFields>,
_DefaultFieldsResolver extends FieldsResolver<OptionalAuthor & AuthorTransientFields>,
_Traits extends Traits<OptionalAuthor, AuthorTransientFields>,
>(
options: AuthorFactoryDefineOptions<AuthorTransientFields, _DefaultFieldsResolver, _Traits>,
Expand Down
18 changes: 9 additions & 9 deletions src/__snapshots__/code-generator.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ exports[`generateCode > generates code 1`] = `
type Traits,
type TypeFactoryDefineOptions,
type TypeFactoryInterface,
type DefaultFieldsResolver,
type FieldsResolver,
defineTypeFactoryInternal,
} from '@mizdra/graphql-codegen-typescript-fabbrica/helper';
import type { Maybe, Book, Author } from './types';
Expand All @@ -21,19 +21,19 @@ const BookFieldNames = ['id', 'title', 'author'] as const;
export type BookFactoryDefineOptions<
TransientFields extends Record<string, unknown>,
_DefaultFieldsResolver extends DefaultFieldsResolver<OptionalBook & TransientFields>,
_DefaultFieldsResolver extends FieldsResolver<OptionalBook & TransientFields>,
_Traits extends Traits<OptionalBook, TransientFields>,
> = TypeFactoryDefineOptions<OptionalBook, TransientFields, _DefaultFieldsResolver, _Traits>;
export type BookFactoryInterface<
TransientFields extends Record<string, unknown>,
_DefaultFieldsResolver extends DefaultFieldsResolver<OptionalBook & TransientFields>,
_DefaultFieldsResolver extends FieldsResolver<OptionalBook & TransientFields>,
_Traits extends Traits<OptionalBook, TransientFields>,
> = TypeFactoryInterface<OptionalBook, TransientFields, _DefaultFieldsResolver, _Traits>;
export function defineBookFactoryInternal<
TransientFields extends Record<string, unknown>,
_DefaultFieldsResolver extends DefaultFieldsResolver<OptionalBook & TransientFields>,
_DefaultFieldsResolver extends FieldsResolver<OptionalBook & TransientFields>,
_Traits extends Traits<OptionalBook, TransientFields>,
>(
options: BookFactoryDefineOptions<TransientFields, _DefaultFieldsResolver, _Traits>,
Expand All @@ -48,7 +48,7 @@ export function defineBookFactoryInternal<
* @returns factory {@link BookFactoryInterface}
*/
export function defineBookFactory<
_DefaultFieldsResolver extends DefaultFieldsResolver<OptionalBook>,
_DefaultFieldsResolver extends FieldsResolver<OptionalBook>,
_Traits extends Traits<OptionalBook, {}>,
>(
options: BookFactoryDefineOptions<{}, _DefaultFieldsResolver, _Traits>,
Expand All @@ -66,19 +66,19 @@ const AuthorFieldNames = ['id', 'name', 'books'] as const;
export type AuthorFactoryDefineOptions<
TransientFields extends Record<string, unknown>,
_DefaultFieldsResolver extends DefaultFieldsResolver<OptionalAuthor & TransientFields>,
_DefaultFieldsResolver extends FieldsResolver<OptionalAuthor & TransientFields>,
_Traits extends Traits<OptionalAuthor, TransientFields>,
> = TypeFactoryDefineOptions<OptionalAuthor, TransientFields, _DefaultFieldsResolver, _Traits>;
export type AuthorFactoryInterface<
TransientFields extends Record<string, unknown>,
_DefaultFieldsResolver extends DefaultFieldsResolver<OptionalAuthor & TransientFields>,
_DefaultFieldsResolver extends FieldsResolver<OptionalAuthor & TransientFields>,
_Traits extends Traits<OptionalAuthor, TransientFields>,
> = TypeFactoryInterface<OptionalAuthor, TransientFields, _DefaultFieldsResolver, _Traits>;
export function defineAuthorFactoryInternal<
TransientFields extends Record<string, unknown>,
_DefaultFieldsResolver extends DefaultFieldsResolver<OptionalAuthor & TransientFields>,
_DefaultFieldsResolver extends FieldsResolver<OptionalAuthor & TransientFields>,
_Traits extends Traits<OptionalAuthor, TransientFields>,
>(
options: AuthorFactoryDefineOptions<TransientFields, _DefaultFieldsResolver, _Traits>,
Expand All @@ -93,7 +93,7 @@ export function defineAuthorFactoryInternal<
* @returns factory {@link AuthorFactoryInterface}
*/
export function defineAuthorFactory<
_DefaultFieldsResolver extends DefaultFieldsResolver<OptionalAuthor>,
_DefaultFieldsResolver extends FieldsResolver<OptionalAuthor>,
_Traits extends Traits<OptionalAuthor, {}>,
>(
options: AuthorFactoryDefineOptions<{}, _DefaultFieldsResolver, _Traits>,
Expand Down
10 changes: 5 additions & 5 deletions src/code-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
type Traits,
type TypeFactoryDefineOptions,
type TypeFactoryInterface,
type DefaultFieldsResolver,
type FieldsResolver,
defineTypeFactoryInternal,
} from '@mizdra/graphql-codegen-typescript-fabbrica/helper';
import type { Maybe, ${joinedTypeNames} } from '${config.typesFile}';
Expand Down Expand Up @@ -48,19 +48,19 @@ function generateTypeFactoryCode(config: Config, typeInfo: TypeInfo): string {
return `
export type ${name}FactoryDefineOptions<
TransientFields extends Record<string, unknown>,
_DefaultFieldsResolver extends DefaultFieldsResolver<Optional${name} & TransientFields>,
_DefaultFieldsResolver extends FieldsResolver<Optional${name} & TransientFields>,
_Traits extends Traits<Optional${name}, TransientFields>,
> = TypeFactoryDefineOptions<Optional${name}, TransientFields, _DefaultFieldsResolver, _Traits>;
export type ${name}FactoryInterface<
TransientFields extends Record<string, unknown>,
_DefaultFieldsResolver extends DefaultFieldsResolver<Optional${name} & TransientFields>,
_DefaultFieldsResolver extends FieldsResolver<Optional${name} & TransientFields>,
_Traits extends Traits<Optional${name}, TransientFields>,
> = TypeFactoryInterface<Optional${name}, TransientFields, _DefaultFieldsResolver, _Traits>;
export function define${name}FactoryInternal<
TransientFields extends Record<string, unknown>,
_DefaultFieldsResolver extends ${wrapRequired(`DefaultFieldsResolver<Optional${name} & TransientFields>`)},
_DefaultFieldsResolver extends ${wrapRequired(`FieldsResolver<Optional${name} & TransientFields>`)},
_Traits extends Traits<Optional${name}, TransientFields>,
>(
options: ${name}FactoryDefineOptions<TransientFields, _DefaultFieldsResolver, _Traits>,
Expand All @@ -75,7 +75,7 @@ export function define${name}FactoryInternal<
* @returns factory {@link ${name}FactoryInterface}
*/
export function define${name}Factory<
_DefaultFieldsResolver extends ${wrapRequired(`DefaultFieldsResolver<Optional${name}>`)},
_DefaultFieldsResolver extends ${wrapRequired(`FieldsResolver<Optional${name}>`)},
_Traits extends Traits<Optional${name}, {}>,
>(
options: ${name}FactoryDefineOptions<{}, _DefaultFieldsResolver, _Traits>,
Expand Down
74 changes: 29 additions & 45 deletions src/helper/factory.ts
Original file line number Diff line number Diff line change
@@ -1,96 +1,80 @@
// MEMO: The tests for this module are covered by `e2e/*.e2e.ts`.

import {
ResolvedFields,
DefaultFieldsResolver,
InputFieldsResolver,
resolveFields,
FieldResolver,
} from './field-resolver.js';
import { ResolvedFields, FieldsResolver, resolveFields, FieldResolver } from './field-resolver.js';
import { getSequenceCounter, resetSequence } from './sequence.js';
import { Merge, StrictlyPick } from './util.js';

export type Traits<OptionalType extends Record<string, unknown>, TransientFields extends Record<string, unknown>> = {
export type Traits<Type extends Record<string, unknown>, TransientFields extends Record<string, unknown>> = {
[traitName: string]: {
defaultFields: InputFieldsResolver<OptionalType & TransientFields>;
defaultFields: FieldsResolver<Type & TransientFields>;
};
};

export interface TypeFactoryDefineOptions<
OptionalType extends Record<string, unknown>,
Type extends Record<string, unknown>,
TransientFields extends Record<string, unknown>,
_DefaultFieldsResolver extends DefaultFieldsResolver<OptionalType & TransientFields>,
_Traits extends Traits<OptionalType, TransientFields>,
_DefaultFieldsResolver extends FieldsResolver<Type & TransientFields>,
_Traits extends Traits<Type, TransientFields>,
> {
defaultFields: _DefaultFieldsResolver;
traits?: _Traits;
}

export interface TypeFactoryInterface<
OptionalType extends Record<string, unknown>,
Type extends Record<string, unknown>,
TransientFields extends Record<string, unknown>,
// NOTE: The constraints of _DefaultFieldsResolver are loose so that `Merge<_DefaultFieldsResolver, _Traits[T]['defaultFields']>` is accepted.
_DefaultFieldsResolver extends Partial<
Record<keyof OptionalType, FieldResolver<OptionalType & TransientFields, unknown>>
>,
_Traits extends Traits<OptionalType, TransientFields>,
_DefaultFieldsResolver extends Partial<Record<keyof Type, FieldResolver<Type & TransientFields, unknown>>>,
_Traits extends Traits<Type, TransientFields>,
> {
build(): Promise<StrictlyPick<Merge<ResolvedFields<_DefaultFieldsResolver>, ResolvedFields<{}>>, keyof OptionalType>>;
build<T extends InputFieldsResolver<OptionalType & TransientFields>>(
build(): Promise<StrictlyPick<Merge<ResolvedFields<_DefaultFieldsResolver>, ResolvedFields<{}>>, keyof Type>>;
build<T extends FieldsResolver<Type & TransientFields>>(
inputFieldsResolver: T,
): Promise<StrictlyPick<Merge<ResolvedFields<_DefaultFieldsResolver>, ResolvedFields<T>>, keyof OptionalType>>;
): Promise<StrictlyPick<Merge<ResolvedFields<_DefaultFieldsResolver>, ResolvedFields<T>>, keyof Type>>;
buildList(
count: number,
): Promise<StrictlyPick<Merge<ResolvedFields<_DefaultFieldsResolver>, ResolvedFields<{}>>, keyof OptionalType>[]>;
buildList<T extends InputFieldsResolver<OptionalType & TransientFields>>(
): Promise<StrictlyPick<Merge<ResolvedFields<_DefaultFieldsResolver>, ResolvedFields<{}>>, keyof Type>[]>;
buildList<T extends FieldsResolver<Type & TransientFields>>(
count: number,
inputFieldsResolver: T,
): Promise<StrictlyPick<Merge<ResolvedFields<_DefaultFieldsResolver>, ResolvedFields<T>>, keyof OptionalType>[]>;
): Promise<StrictlyPick<Merge<ResolvedFields<_DefaultFieldsResolver>, ResolvedFields<T>>, keyof Type>[]>;
use<T extends keyof _Traits>(
traitName: T,
): TypeFactoryInterface<
OptionalType,
TransientFields,
Merge<_DefaultFieldsResolver, _Traits[T]['defaultFields']>,
_Traits
>;
): TypeFactoryInterface<Type, TransientFields, Merge<_DefaultFieldsResolver, _Traits[T]['defaultFields']>, _Traits>;
resetSequence(): void;
}

export function defineTypeFactoryInternal<
OptionalType extends Record<string, unknown>,
Type extends Record<string, unknown>,
TransientFields extends Record<string, unknown>,
_DefaultFieldsResolver extends DefaultFieldsResolver<OptionalType & TransientFields>,
_Traits extends Traits<OptionalType, TransientFields>,
_DefaultFieldsResolver extends FieldsResolver<Type & TransientFields>,
_Traits extends Traits<Type, TransientFields>,
>(
typeFieldNames: readonly (keyof OptionalType)[],
typeFieldNames: readonly (keyof Type)[],
{
defaultFields: defaultFieldsResolver,
traits,
}: TypeFactoryDefineOptions<OptionalType, TransientFields, _DefaultFieldsResolver, _Traits>,
): TypeFactoryInterface<OptionalType, TransientFields, _DefaultFieldsResolver, _Traits> {
}: TypeFactoryDefineOptions<Type, TransientFields, _DefaultFieldsResolver, _Traits>,
): TypeFactoryInterface<Type, TransientFields, _DefaultFieldsResolver, _Traits> {
const seqKey = {};
const getSeq = () => getSequenceCounter(seqKey);
return {
async build<T extends InputFieldsResolver<OptionalType & TransientFields>>(
async build<T extends FieldsResolver<Type & TransientFields>>(
inputFieldsResolver?: T,
): Promise<StrictlyPick<Merge<ResolvedFields<_DefaultFieldsResolver>, ResolvedFields<T>>, keyof OptionalType>> {
): Promise<StrictlyPick<Merge<ResolvedFields<_DefaultFieldsResolver>, ResolvedFields<T>>, keyof Type>> {
const seq = getSeq();
return resolveFields<OptionalType, TransientFields, _DefaultFieldsResolver, T>(
return resolveFields<Type, TransientFields, _DefaultFieldsResolver, T>(
typeFieldNames,
seq,
defaultFieldsResolver,
inputFieldsResolver ?? ({} as T),
);
},
async buildList<T extends InputFieldsResolver<OptionalType & TransientFields>>(
async buildList<T extends FieldsResolver<Type & TransientFields>>(
count: number,
inputFieldsResolver?: T,
): Promise<StrictlyPick<Merge<ResolvedFields<_DefaultFieldsResolver>, ResolvedFields<T>>, keyof OptionalType>[]> {
const array: StrictlyPick<
Merge<ResolvedFields<_DefaultFieldsResolver>, ResolvedFields<T>>,
keyof OptionalType
>[] = [];
): Promise<StrictlyPick<Merge<ResolvedFields<_DefaultFieldsResolver>, ResolvedFields<T>>, keyof Type>[]> {
const array: StrictlyPick<Merge<ResolvedFields<_DefaultFieldsResolver>, ResolvedFields<T>>, keyof Type>[] = [];
for (let i = 0; i < count; i++) {
if (inputFieldsResolver) {
// eslint-disable-next-line no-await-in-loop, @typescript-eslint/no-explicit-any
Expand All @@ -105,7 +89,7 @@ export function defineTypeFactoryInternal<
use<T extends keyof _Traits>(
traitName: T,
): TypeFactoryInterface<
OptionalType,
Type,
TransientFields,
Merge<_DefaultFieldsResolver, _Traits[T]['defaultFields']>,
_Traits
Expand Down
Loading

0 comments on commit 213f008

Please sign in to comment.