-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathvisitor.ts
36 lines (32 loc) · 997 Bytes
/
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
import { ValidationSchemaPluginConfig } from './config';
import { TsVisitor } from '@graphql-codegen/typescript';
import { NameNode, GraphQLSchema } from 'graphql';
export class Visitor extends TsVisitor {
constructor(
private scalarDirection: 'input' | 'output' | 'both',
private schema: GraphQLSchema,
config: ValidationSchemaPluginConfig
) {
super(schema, config);
}
public getType(name: string) {
return this.schema.getType(name);
}
public getNameNodeConverter(node: NameNode) {
const typ = this.schema.getType(node.value);
const astNode = typ?.astNode;
if (astNode === undefined || astNode === null) {
return undefined;
}
return {
targetKind: astNode.kind,
convertName: () => this.convertName(astNode.name.value),
};
}
public getScalarType(scalarName: string): string | null {
if (this.scalarDirection === 'both') {
return null;
}
return this.scalars[scalarName][this.scalarDirection];
}
}