Skip to content

Commit bf0ad87

Browse files
committed
feat: correctly generate scalar types
1 parent 0237f45 commit bf0ad87

File tree

4 files changed

+44
-37
lines changed

4 files changed

+44
-37
lines changed

cmd/recodegen/recodegen.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"runtime"
1414
)
1515

16-
const VERSION = "v0.3.0"
16+
const VERSION = "v0.4.0"
1717

1818
func main() {
1919
configFileName := flag.String("config", "recodegen.json", "Configuration file name")
@@ -79,7 +79,7 @@ func processInput(schemaAst *ast.Schema, outputFileName string, genConfig config
7979

8080
// don't write anything to a file if no known plugins were used
8181
if !hadKnownPlugin {
82-
fmt.Printf("[skipping] %s\n", outputFileName)
82+
fmt.Printf("[unsupported] %s\n", outputFileName)
8383
return
8484
}
8585

@@ -88,7 +88,7 @@ func processInput(schemaAst *ast.Schema, outputFileName string, genConfig config
8888
fmt.Printf("[writing] %s\n", outputFileName)
8989
writeFile(outputFileName, output)
9090
} else {
91-
fmt.Printf("[skipping] %s\n", outputFileName)
91+
fmt.Printf("[unchanged] %s\n", outputFileName)
9292
}
9393
}
9494

npm/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@graphql-recodegen/cli",
3-
"version": "0.3.0",
3+
"version": "0.4.0",
44
"description": "Faster GraphQL codegen for TypeScript projects",
55
"main": "index.js",
66
"type": "module",

typescript/operation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ func generateVariable(varDef *ast.VariableDefinition, isImportTypes bool) string
325325
}
326326

327327
func generateFieldTypeImported(astType *ast.Type) string {
328-
normalName := wrapScalar(normalizedName(astType.Name()))
328+
normalName := wrapScalar(astType.Name())
329329
normalName = defExportName + "." + normalName
330330

331331
if astType.NamedType != "" {

typescript/schema.go

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212

1313
const spacing = " "
1414

15+
var scalarNames []string
16+
1517
type Schema struct {
1618
Ast *ast.Schema
1719
}
@@ -21,7 +23,15 @@ func (schema *Schema) String() string {
2123
var enumOnly = ""
2224
var typesOnly = ""
2325
var objectsOnly = ""
26+
2427
sortedTypeKeys := schema.getSortedTypeKeys()
28+
for _, key := range *sortedTypeKeys {
29+
def := schema.Ast.Types[key]
30+
if def.Kind == ast.Scalar {
31+
scalarNames = append(scalarNames, def.Name)
32+
}
33+
}
34+
2535
for _, key := range *sortedTypeKeys {
2636
def := schema.Ast.Types[key]
2737
if def.Kind == ast.Enum {
@@ -39,11 +49,10 @@ func (schema *Schema) String() string {
3949
//if def.Kind == ast.Union {
4050
// fmt.Printf("Union: %s\n", def.Name)
4151
//}
42-
//if def.Kind == ast.Scalar {
43-
//fmt.Printf("Scalar: %s\n", def.Name)
44-
//}
52+
4553
}
46-
return schema.getTypesHeader() + enumOnly + typesOnly + objectsOnly
54+
scalars := genScalars(scalarNames)
55+
return schema.getTypesHeader() + scalars + enumOnly + typesOnly + objectsOnly
4756
}
4857

4958
func (schema *Schema) getSortedTypeKeys() *[]string {
@@ -66,29 +75,31 @@ export type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K]
6675
export type MakeOptional<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]?: Maybe<T[SubKey]> };
6776
export type MakeMaybe<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]: Maybe<T[SubKey]> };
6877
/** All built-in and custom scalars, mapped to their actual values */
69-
export type Scalars = {
70-
ID: string;
71-
String: string;
72-
Boolean: boolean;
73-
Int: number;
74-
Float: number;
75-
Bigint: any;
76-
date: any;
77-
Date: any;
78-
Float8: any;
79-
Timestamp: any;
80-
Timestamptz: any;
81-
Json: any;
82-
Jsonb: any;
83-
Numeric: any;
84-
Point: any;
85-
Polygon: any;
86-
Uuid: any;
87-
};
88-
8978
`
9079
}
9180

81+
func genScalars(scalarNames []string) string {
82+
head := "export type Scalars = {\n"
83+
body := ""
84+
for _, name := range scalarNames {
85+
switch name {
86+
case "ID":
87+
body += spacing + "ID: string;\n"
88+
case "Int":
89+
body += spacing + "Int: number;\n"
90+
case "Float":
91+
body += spacing + "Float: number;\n"
92+
case "String":
93+
body += spacing + "String: string;\n"
94+
case "Boolean":
95+
body += spacing + "Boolean: boolean;\n"
96+
default:
97+
body += spacing + name + ": any;\n"
98+
}
99+
}
100+
return head + body + "}\n"
101+
}
102+
92103
func genEnum(def *ast.Definition) string {
93104
var tmpl = `export enum %s {
94105
%s
@@ -147,7 +158,7 @@ func genInputObject(def *ast.Definition) string {
147158
}
148159

149160
func generateFieldType(astType *ast.Type) string {
150-
normalName := wrapScalar(normalizedName(astType.Name()))
161+
normalName := wrapScalar(astType.Name())
151162

152163
if astType.NamedType != "" {
153164
if astType.NonNull == false {
@@ -167,16 +178,12 @@ func generateFieldType(astType *ast.Type) string {
167178
// given type like "Int" will wrap it into "Scalars['Int']"
168179
// if given type is not scalar, return as is
169180
func wrapScalar(typeName string) string {
170-
scalars := []string{
171-
"Boolean", "String", "Int", "Float8", "Float", "Bigint", "Timestamp", "Timestamptz",
172-
"Numeric", "Uuid", "Json", "Jsonb", "Polygon", "Point", "Date", "date",
173-
}
174-
for _, scalar := range scalars {
181+
for _, scalar := range scalarNames {
175182
if typeName == scalar {
176183
return "Scalars['" + typeName + "']"
177184
}
178185
}
179-
return typeName
186+
return normalizedName(typeName)
180187
}
181188

182189
func generateFieldName(astFieldDef *ast.FieldDefinition) string {
@@ -191,7 +198,7 @@ func genNullable(astFieldDef *ast.FieldDefinition) string {
191198
}
192199

193200
func genInputFieldType(astType *ast.Type) string {
194-
normalName := wrapScalar(normalizedName(astType.Name()))
201+
normalName := wrapScalar(astType.Name())
195202

196203
if astType.NamedType != "" {
197204
if astType.NonNull == false {

0 commit comments

Comments
 (0)