-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathschema_visitor.ts
57 lines (48 loc) · 1.63 KB
/
schema_visitor.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import type {
FieldDefinitionNode,
GraphQLSchema,
InputValueDefinitionNode,
InterfaceTypeDefinitionNode,
ObjectTypeDefinitionNode,
} from 'graphql';
import type { ValidationSchemaPluginConfig } from './config';
import type { SchemaVisitor } from './types';
import { Visitor } from './visitor';
export abstract class BaseSchemaVisitor implements SchemaVisitor {
protected importTypes: string[] = [];
protected enumDeclarations: string[] = [];
constructor(
protected schema: GraphQLSchema,
protected config: ValidationSchemaPluginConfig,
) {}
abstract importValidationSchema(): string;
buildImports(): string[] {
if (this.config.importFrom && this.importTypes.length > 0) {
return [
this.importValidationSchema(),
`import ${this.config.useTypeImports ? 'type ' : ''}{ ${this.importTypes.join(', ')} } from '${
this.config.importFrom
}'`,
];
}
return [this.importValidationSchema()];
}
abstract initialEmit(): string;
createVisitor(scalarDirection: 'input' | 'output' | 'both'): Visitor {
return new Visitor(scalarDirection, this.schema, this.config);
}
protected abstract buildInputFields(
fields: readonly (FieldDefinitionNode | InputValueDefinitionNode)[],
visitor: Visitor,
name: string
): string;
protected buildTypeDefinitionArguments(
node: ObjectTypeDefinitionNode | InterfaceTypeDefinitionNode,
visitor: Visitor,
) {
return visitor.buildArgumentsSchemaBlock(node, (typeName, field) => {
this.importTypes.push(typeName);
return this.buildInputFields(field.arguments ?? [], visitor, typeName);
});
}
}