-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
visitor.ts
167 lines (136 loc) · 5.15 KB
/
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import {
ListTypeNode,
NamedTypeNode,
NonNullTypeNode,
GraphQLSchema,
ScalarTypeDefinitionNode,
InputValueDefinitionNode,
EnumTypeDefinitionNode,
} from 'graphql';
import autoBind from 'auto-bind';
import {
RawResolversConfig,
indent,
ParsedResolversConfig,
BaseResolversVisitor,
DeclarationBlock,
DeclarationKind,
} from '@graphql-codegen/visitor-plugin-common';
import { FlowOperationVariablesToObject } from '@graphql-codegen/flow';
import { FLOW_REQUIRE_FIELDS_TYPE } from './flow-util-types';
export const ENUM_RESOLVERS_SIGNATURE =
'export type EnumResolverSignature<T, AllowedValues = any> = $ObjMap<T, () => AllowedValues>;';
export interface ParsedFlorResolversConfig extends ParsedResolversConfig {}
export class FlowResolversVisitor extends BaseResolversVisitor<RawResolversConfig, ParsedFlorResolversConfig> {
constructor(pluginConfig: RawResolversConfig, schema: GraphQLSchema) {
super(pluginConfig, null, schema);
autoBind(this);
this.setVariablesTransformer(
new FlowOperationVariablesToObject(this.scalars, this.convertName, this.config.namespacedImportName)
);
}
protected _getScalar(name: string): string {
return `$ElementType<Scalars, '${name}'>`;
}
protected applyRequireFields(argsType: string, fields: InputValueDefinitionNode[]): string {
this._globalDeclarations.add(FLOW_REQUIRE_FIELDS_TYPE);
return `$RequireFields<${argsType}, { ${fields.map(f => `${f.name.value}: *`).join(', ')} }>`;
}
protected applyOptionalFields(argsType: string, _fields: readonly InputValueDefinitionNode[]): string {
return argsType;
}
protected buildMapperImport(source: string, types: { identifier: string; asDefault?: boolean }[]): string {
if (types[0] && types[0].asDefault) {
return `import type ${types[0].identifier} from '${source}';`;
}
return `import { ${types.map(t => `type ${t.identifier}`).join(', ')} } from '${source}';`;
}
protected formatRootResolver(schemaTypeName: string, resolverType: string, declarationKind: DeclarationKind): string {
return `${schemaTypeName}?: ${resolverType}${resolverType.includes('<') ? '' : '<>'}${this.getPunctuation(
declarationKind
)}`;
}
protected transformParentGenericType(parentType: string): string {
return `ParentType = ${parentType}`;
}
ListType(node: ListTypeNode): string {
return `?${super.ListType(node)}`;
}
NamedType(node: NamedTypeNode): string {
return `?${super.NamedType(node)}`;
}
NonNullType(node: NonNullTypeNode): string {
const baseValue = super.NonNullType(node);
if (baseValue.startsWith('?')) {
return baseValue.substr(1);
}
return baseValue;
}
protected applyMaybe(str: string): string {
return `?${str}`;
}
protected clearMaybe(str: string): string {
if (str.startsWith('?')) {
return str.substr(1);
}
return str;
}
protected getTypeToUse(name: string): string {
const resolversType = this.convertName('ResolversTypes');
return `$ElementType<${resolversType}, '${name}'>`;
}
protected getParentTypeToUse(name: string): string {
const resolversType = this.convertName('ResolversParentTypes');
return `$ElementType<${resolversType}, '${name}'>`;
}
protected replaceFieldsInType(
typeName: string,
relevantFields: { fieldName: string; replaceWithType: string }[]
): string {
return `$Diff<${typeName}, { ${relevantFields.map(f => `${f.fieldName}: * `).join(', ')} }> & { ${relevantFields
.map(f => `${f.fieldName}: ${f.replaceWithType}`)
.join(', ')} }`;
}
ScalarTypeDefinition(node: ScalarTypeDefinitionNode): string {
const nameAsString = node.name as any as string;
const baseName = this.getTypeToUse(nameAsString);
this._collectedResolvers[node.name as any] = 'GraphQLScalarType';
return new DeclarationBlock({
...this._declarationBlockConfig,
blockTransformer(block) {
return block;
},
})
.export()
.asKind('type')
.withName(
this.convertName(node, {
suffix: 'ScalarConfig',
})
)
.withBlock([indent(`...GraphQLScalarTypeConfig<${baseName}, any>`), indent(`name: '${node.name}'`)].join(', \n'))
.string;
}
protected getPunctuation(declarationKind: DeclarationKind): string {
return declarationKind === 'type' ? ',' : ';';
}
protected buildEnumResolverContentBlock(node: EnumTypeDefinitionNode, mappedEnumType: string): string {
const valuesMap = `{| ${(node.values || [])
.map(v => `${v.name as any as string}${this.config.avoidOptionals ? '' : '?'}: *`)
.join(', ')} |}`;
this._globalDeclarations.add(ENUM_RESOLVERS_SIGNATURE);
return `EnumResolverSignature<${valuesMap}, ${mappedEnumType}>`;
}
protected buildEnumResolversExplicitMappedValues(
node: EnumTypeDefinitionNode,
valuesMapping: { [valueName: string]: string | number }
): string {
return `{| ${(node.values || [])
.map(v => {
const valueName = v.name as any as string;
const mappedValue = valuesMapping[valueName];
return `${valueName}: ${typeof mappedValue === 'number' ? mappedValue : `'${mappedValue}'`}`;
})
.join(', ')} |}`;
}
}