Skip to content

Commit 6951799

Browse files
committed
progress
1 parent bb2a64f commit 6951799

File tree

11 files changed

+181
-184
lines changed

11 files changed

+181
-184
lines changed

packages/cli/src/codegen/schema.js

Lines changed: 33 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
const tsCodegen = require('./typescript')
22
const typesCodegen = require('./types')
33

4-
54
class IdField {
65
static BYTES = Symbol('Bytes')
76
static STRING = Symbol('String')
87

98
constructor(idField) {
10-
const typeName = idField.getIn(['type', 'type', 'name', 'value'])
9+
const typeName = idField.type?.type?.name?.value
1110
this.kind = typeName === 'Bytes' ? IdField.BYTES : IdField.STRING
1211
}
1312

@@ -40,12 +39,12 @@ class IdField {
4039
}
4140

4241
static fromFields(fields) {
43-
const idField = fields.find(field => field.getIn(['name', 'value']) === 'id')
42+
const idField = fields.find(field => field.name?.value === 'id')
4443
return new IdField(idField)
4544
}
4645

4746
static fromTypeDef(def) {
48-
return IdField.fromFields(def.get('fields'))
47+
return IdField.fromFields(def.fields)
4948
}
5049
}
5150

@@ -78,29 +77,26 @@ module.exports = class SchemaCodeGenerator {
7877
}
7978

8079
generateTypes() {
81-
return this.schema.ast
82-
.get('definitions')
80+
return this.schema.ast.definitions
8381
.filter(def => this._isEntityTypeDefinition(def))
8482
.map(def => this._generateEntityType(def))
8583
}
8684

8785
_isEntityTypeDefinition(def) {
8886
return (
89-
def.get('kind') === 'ObjectTypeDefinition' &&
90-
def
91-
.get('directives')
92-
.find(directive => directive.getIn(['name', 'value']) === 'entity') !== undefined
87+
def.kind === 'ObjectTypeDefinition' &&
88+
def.directives.find(directive => directive.name?.value === 'entity') !== undefined
9389
)
9490
}
9591

9692
_isInterfaceDefinition(def) {
97-
return def.get('kind') === 'InterfaceTypeDefinition'
93+
return def.kind === 'InterfaceTypeDefinition'
9894
}
9995

10096
_generateEntityType(def) {
101-
let name = def.getIn(['name', 'value'])
97+
let name = def.name?.value
10298
let klass = tsCodegen.klass(name, { export: true, extends: 'Entity' })
103-
const fields = def.get('fields')
99+
const fields = def.fields
104100
const idField = IdField.fromFields(fields)
105101

106102
// Generate and add a constructor
@@ -110,8 +106,7 @@ module.exports = class SchemaCodeGenerator {
110106
this._generateStoreMethods(name, idField).forEach(method => klass.addMethod(method))
111107

112108
// Generate and add entity field getters and setters
113-
def
114-
.get('fields')
109+
def.fields
115110
.reduce(
116111
(methods, field) => methods.concat(this._generateEntityFieldMethods(def, field)),
117112
[],
@@ -170,8 +165,8 @@ module.exports = class SchemaCodeGenerator {
170165
}
171166

172167
_generateEntityFieldGetter(entityDef, fieldDef) {
173-
let name = fieldDef.getIn(['name', 'value'])
174-
let gqlType = fieldDef.get('type')
168+
let name = fieldDef.name?.value
169+
let gqlType = fieldDef.type
175170
let fieldValueType = this._valueTypeFromGraphQl(gqlType)
176171
let returnType = this._typeFromGraphQl(gqlType)
177172
let isNullable = returnType instanceof tsCodegen.NullableType
@@ -195,8 +190,8 @@ module.exports = class SchemaCodeGenerator {
195190
}
196191

197192
_generateEntityFieldSetter(entityDef, fieldDef) {
198-
let name = fieldDef.getIn(['name', 'value'])
199-
let gqlType = fieldDef.get('type')
193+
let name = fieldDef.name?.value
194+
let gqlType = fieldDef.type
200195
let fieldValueType = this._valueTypeFromGraphQl(gqlType)
201196
let paramType = this._typeFromGraphQl(gqlType)
202197
let isNullable = paramType instanceof tsCodegen.NullableType
@@ -235,17 +230,15 @@ Suggestion: add an '!' to the member type of the List, change from '[${baseType}
235230
}
236231

237232
_resolveFieldType(gqlType) {
238-
let typeName = gqlType.getIn(['name', 'value'])
233+
let typeName = gqlType.name?.value
239234

240235
// If this is a reference to another type, the field has the type of
241236
// the referred type's id field
242-
const typeDef = this.schema.ast
243-
.get('definitions')
244-
.find(
245-
def =>
246-
(this._isEntityTypeDefinition(def) || this._isInterfaceDefinition(def)) &&
247-
def.getIn(['name', 'value']) === typeName,
248-
)
237+
const typeDef = this.schema.ast.definitions.find(
238+
def =>
239+
(this._isEntityTypeDefinition(def) || this._isInterfaceDefinition(def)) &&
240+
def.name?.value === typeName,
241+
)
249242
if (typeDef) {
250243
return IdField.fromTypeDef(typeDef).typeName()
251244
} else {
@@ -258,10 +251,10 @@ Suggestion: add an '!' to the member type of the List, change from '[${baseType}
258251
* other entity types, this is the same as the type of the id of the
259252
* referred type, i.e., `string` or `Bytes`*/
260253
_valueTypeFromGraphQl(gqlType) {
261-
if (gqlType.get('kind') === 'NonNullType') {
262-
return this._valueTypeFromGraphQl(gqlType.get('type'), false)
263-
} else if (gqlType.get('kind') === 'ListType') {
264-
return '[' + this._valueTypeFromGraphQl(gqlType.get('type')) + ']'
254+
if (gqlType.kind === 'NonNullType') {
255+
return this._valueTypeFromGraphQl(gqlType.type, false)
256+
} else if (gqlType.kind === 'ListType') {
257+
return '[' + this._valueTypeFromGraphQl(gqlType.type) + ']'
265258
} else {
266259
return this._resolveFieldType(gqlType)
267260
}
@@ -270,20 +263,20 @@ Suggestion: add an '!' to the member type of the List, change from '[${baseType}
270263
/** Determine the base type of `gqlType` by removing any non-null
271264
* constraints and using the type of elements of lists */
272265
_baseType(gqlType) {
273-
if (gqlType.get('kind') === 'NonNullType') {
274-
return this._baseType(gqlType.get('type'))
275-
} else if (gqlType.get('kind') === 'ListType') {
276-
return this._baseType(gqlType.get('type'))
266+
if (gqlType.kind === 'NonNullType') {
267+
return this._baseType(gqlType.type)
268+
} else if (gqlType.kind === 'ListType') {
269+
return this._baseType(gqlType.type)
277270
} else {
278-
return gqlType.getIn(['name', 'value'])
271+
return gqlType.name?.value
279272
}
280273
}
281274

282275
_typeFromGraphQl(gqlType, nullable = true) {
283-
if (gqlType.get('kind') === 'NonNullType') {
284-
return this._typeFromGraphQl(gqlType.get('type'), false)
285-
} else if (gqlType.get('kind') === 'ListType') {
286-
let type = tsCodegen.arrayType(this._typeFromGraphQl(gqlType.get('type')))
276+
if (gqlType.kind === 'NonNullType') {
277+
return this._typeFromGraphQl(gqlType.type, false)
278+
} else if (gqlType.kind === 'ListType') {
279+
let type = tsCodegen.arrayType(this._typeFromGraphQl(gqlType.type))
287280
return nullable ? tsCodegen.nullableType(type) : type
288281
} else {
289282
// NamedType

packages/cli/src/codegen/schema.test.js

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,7 @@ const {
1111
ArrayType,
1212
} = require('./typescript')
1313

14-
const formatTS = code =>
15-
prettier.format(
16-
code,
17-
{ parser: 'typescript', semi: false }
18-
)
14+
const formatTS = code => prettier.format(code, { parser: 'typescript', semi: false })
1915

2016
const createSchemaCodeGen = schema =>
2117
new SchemaCodeGenerator({
@@ -60,7 +56,7 @@ describe('Schema code generator', () => {
6056
}
6157
`)
6258

63-
expect(codegen.generateTypes().size).toBe(0)
59+
expect(codegen.generateTypes().length).toBe(0)
6460
})
6561

6662
describe('Should generate correct classes for each entity', () => {
@@ -98,7 +94,7 @@ describe('Schema code generator', () => {
9894
const foo = generatedTypes.find(type => type.name === 'Foo')
9995
expect(foo).toBe(undefined)
10096
// Account and Wallet
101-
expect(generatedTypes.size).toBe(2)
97+
expect(generatedTypes.length).toBe(2)
10298
})
10399

104100
test('Account is an entity with the correct methods', () => {
@@ -247,7 +243,12 @@ describe('Schema code generator', () => {
247243
},
248244
{
249245
name: 'set wallets',
250-
params: [new Param('value', new NullableType(new ArrayType(new NamedType('string'))))],
246+
params: [
247+
new Param(
248+
'value',
249+
new NullableType(new ArrayType(new NamedType('string'))),
250+
),
251+
],
251252
returnType: undefined,
252253
body: `
253254
if (!value) {
@@ -376,84 +377,89 @@ describe('Schema code generator', () => {
376377

377378
const generatedTypes = codegen.generateTypes()
378379
testEntity(generatedTypes, {
379-
name: "Task",
380+
name: 'Task',
380381
members: [],
381382
methods: [
382383
{
383384
name: 'constructor',
384385
params: [new Param('id', new NamedType('Bytes'))],
385386
returnType: undefined,
386-
body: "\n super()\n this.set('id', Value.fromBytes(id))\n "
387+
body: "\n super()\n this.set('id', Value.fromBytes(id))\n ",
387388
},
388389
{
389390
name: 'save',
390391
params: [],
391392
returnType: new NamedType('void'),
392-
body: '\n' +
393+
body:
394+
'\n' +
393395
" let id = this.get('id')\n" +
394396
' assert(id != null,\n' +
395397
" 'Cannot save Task entity without an ID')\n" +
396398
' if (id) {\n' +
397399
' assert(id.kind == ValueKind.BYTES,\n' +
398400
" `Entities of type Task must have an ID of type Bytes but the id '${id.displayData()}' is of type ${id.displayKind()}`)\n" +
399401
" store.set('Task', id.toBytes().toHexString(), this)\n" +
400-
' }'
402+
' }',
401403
},
402404
{
403405
name: 'load',
404406
static: true,
405407
params: [new Param('id', new NamedType('Bytes'))],
406408
returnType: new NullableType(new NamedType('Task')),
407-
body: '\n' +
409+
body:
410+
'\n' +
408411
" return changetype<Task | null>(store.get('Task', id.toHexString()))\n" +
409-
' '
412+
' ',
410413
},
411414
{
412415
name: 'get id',
413416
params: [],
414417
returnType: new NamedType('Bytes'),
415-
body: '\n' +
418+
body:
419+
'\n' +
416420
" let value = this.get('id')\n" +
417421
' return value!.toBytes()\n' +
418-
' '
422+
' ',
419423
},
420424
{
421425
name: 'set id',
422426
params: [new Param('value', new NamedType('Bytes'))],
423427
returnType: undefined,
424-
body: "\n this.set('id', Value.fromBytes(value))\n "
428+
body: "\n this.set('id', Value.fromBytes(value))\n ",
425429
},
426430
{
427431
name: 'get employee',
428432
params: [],
429433
returnType: new NamedType('Bytes'),
430-
body: '\n' +
434+
body:
435+
'\n' +
431436
" let value = this.get('employee')\n" +
432437
' return value!.toBytes()\n' +
433-
' '
438+
' ',
434439
},
435440
{
436441
name: 'set employee',
437442
params: [new Param('value', new NamedType('Bytes'))],
438443
returnType: undefined,
439-
body: "\n this.set('employee', Value.fromBytes(value))\n "
444+
body: "\n this.set('employee', Value.fromBytes(value))\n ",
440445
},
441446
{
442447
name: 'get worker',
443448
params: [],
444449
returnType: new NamedType('Bytes'),
445-
body: '\n' +
450+
body:
451+
'\n' +
446452
" let value = this.get('worker')\n" +
447453
' return value!.toBytes()\n' +
448-
' '
454+
' ',
449455
},
450456
{
451457
name: 'set worker',
452458
params: [new Param('value', new NamedType('Bytes'))],
453459
returnType: undefined,
454-
body: "\n this.set('worker', Value.fromBytes(value))\n "
455-
}
456-
]
460+
body: "\n this.set('worker', Value.fromBytes(value))\n ",
461+
},
462+
],
457463
})
458464
})
459465
})

0 commit comments

Comments
 (0)