Skip to content
This repository was archived by the owner on Oct 8, 2024. It is now read-only.

Commit 45b8328

Browse files
authored
Refactoring2 (Code-Hex#2)
* getType * reduce visitor * rm redundant factories * fixed 2 indent * wip * wip2 * wip * wip * wip * wip * wip * wip * wip * wip * wip * lazy * wip * reduce * fix * pass fieldMetadata * ren * fix * fix * create lazy * apply lazy * islazy * wip * wip * lazier * rm shouldBeLazy * todo * note * todo * ref * rm unused imports * pass renderer * pass render 2 * pass renderer 3 * rules render * refactor rules * cleanup * fix deps * FieldFactory * metad * wip * factory of rule * rm awkward object * ref * rm directiveFactory * name in metadata * optional * super rename * wip * AST * split schemaASTRenderer * fix * reduce * prv * ref * interim * reduce visitor * reduce field * wip * ren * simplify1 * kind * SchemaASTFac * name * robustness * reduce deps * rm scalarRenderer * robust kind * factory method * 2 * split scalar types * refactor * lintfix * bump 0.7.2
1 parent 0245190 commit 45b8328

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+738
-599
lines changed

example/test.graphql

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ type Guest {
1515

1616
union UserKind = Admin | Guest
1717

18-
type User {
18+
interface Node {
19+
id: ID
20+
}
21+
22+
type User implements Node {
1923
id: ID
2024
name: String
2125
email: String

example/types.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ export type MyTypeFooArgs = {
103103
d: Scalars['Float']['input'];
104104
};
105105

106+
export type Node = {
107+
id?: Maybe<Scalars['ID']['output']>;
108+
};
109+
106110
export type PageInput = {
107111
attributes?: InputMaybe<Array<AttributeInput>>;
108112
date?: InputMaybe<Scalars['Date']['input']>;
@@ -124,7 +128,7 @@ export enum PageType {
124128
Service = 'SERVICE'
125129
}
126130

127-
export type User = {
131+
export type User = Node & {
128132
__typename?: 'User';
129133
createdAt?: Maybe<Scalars['Date']['output']>;
130134
email?: Maybe<Scalars['String']['output']>;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "graphql-codegen-lighthouse-yup-schema",
3-
"version": "0.7.1",
3+
"version": "0.7.2",
44
"description": "GraphQL Code Generator plugin to generate form validation schema from your GraphQL schema",
55
"respository": {
66
"type": "git",

src/LaravelValidationRule.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/TsValidationMethodCall.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/TsValidationMethodCallMapper.spec.ts

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/TsValidationMethodCallMapper.ts

Lines changed: 0 additions & 45 deletions
This file was deleted.

src/visitor.ts

Lines changed: 29 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,51 @@
11
import { TsVisitor } from '@graphql-codegen/typescript';
22
import { NormalizedScalarsMap } from '@graphql-codegen/visitor-plugin-common';
3-
import { FieldDefinitionNode, GraphQLSchema, NameNode, ObjectTypeDefinitionNode } from 'graphql';
3+
import { GraphQLSchema, Kind } from 'graphql';
44

55
import { ValidationSchemaPluginConfig } from './config';
6-
import { isSpecifiedScalarName } from './graphql';
76

87
export class Visitor extends TsVisitor {
98
constructor(
109
private schema: GraphQLSchema,
11-
private pluginConfig: ValidationSchemaPluginConfig
10+
pluginConfig: ValidationSchemaPluginConfig
1211
) {
1312
super(schema, pluginConfig);
1413
}
1514

16-
public getType(name: string) {
17-
return this.schema.getType(name);
18-
}
15+
public getKind(graphQLTypeName: string) {
16+
const foundType = this.schema.getType(graphQLTypeName);
17+
if (!foundType) {
18+
throw new Error(`type ${graphQLTypeName} not found in schema`);
19+
}
1920

20-
public getNameNodeConverter(node: NameNode) {
21-
const typ = this.schema.getType(node.value);
22-
const astNode = typ?.astNode;
23-
if (astNode === undefined || astNode === null) {
24-
return undefined;
21+
// String 等の組み込みの scalar の場合、 astNode がない
22+
if (!foundType.astNode) {
23+
return null;
2524
}
26-
return {
27-
targetKind: astNode.kind,
28-
convertName: () => this.convertName(astNode.name.value),
29-
};
30-
}
3125

32-
public getScalarType(scalarName: string, scalarDirection: keyof NormalizedScalarsMap[string]): string | null {
33-
return this.scalars[scalarName][scalarDirection];
26+
const kind = foundType.astNode.kind;
27+
assertsNotInterface(kind);
28+
29+
return kind;
3430
}
3531

36-
public shouldEmitAsNotAllowEmptyString(name: string, scalarDirection: keyof NormalizedScalarsMap[string]): boolean {
37-
if (this.pluginConfig.notAllowEmptyString !== true) {
38-
return false;
39-
}
40-
const typ = this.getType(name);
41-
if (typ?.astNode?.kind !== 'ScalarTypeDefinition' && !isSpecifiedScalarName(name)) {
42-
return false;
43-
}
44-
const tsType = this.getScalarType(name, scalarDirection);
45-
return tsType === 'string';
32+
public getTypeScriptScalarType(
33+
graphQLTypeName: string,
34+
scalarDirection: keyof NormalizedScalarsMap[string]
35+
): string | null {
36+
return this.scalars[graphQLTypeName]?.[scalarDirection] ?? null;
4637
}
38+
}
4739

48-
public buildArgumentsSchemaBlock(
49-
node: ObjectTypeDefinitionNode,
50-
callback: (typeName: string, field: FieldDefinitionNode) => string
51-
) {
52-
const fieldsWithArguments = node.fields?.filter(field => field.arguments && field.arguments.length > 0) ?? [];
53-
if (fieldsWithArguments.length === 0) {
54-
return undefined;
55-
}
56-
return fieldsWithArguments
57-
.map(field => {
58-
const name =
59-
node.name.value +
60-
(this.config.addUnderscoreToArgsType ? '_' : '') +
61-
this.convertName(field, {
62-
useTypesPrefix: false,
63-
useTypesSuffix: false,
64-
}) +
65-
'Args';
40+
/**
41+
* String 等の組み込みの scalar の場合は null
42+
*/
43+
export type GetKindResult = ReturnType<Visitor['getKind']>;
6644

67-
return callback(name, field);
68-
})
69-
.join('\n');
45+
function assertsNotInterface<TKind extends Kind>(
46+
kind: TKind
47+
): asserts kind is Exclude<TKind, Kind.INTERFACE_TYPE_DEFINITION> {
48+
if (kind === Kind.INTERFACE_TYPE_DEFINITION) {
49+
throw new Error(`unexpected kind: ${kind}`);
7050
}
7151
}

src/yup/DirectiveRenderer.ts

Lines changed: 0 additions & 112 deletions
This file was deleted.

0 commit comments

Comments
 (0)