@graphprotocol/graph-cli 0.37.3-alpha-20230111150012-96ec4db → 0.37.3-alpha-20230111190205-1b7965c
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1 -1
- package/dist/codegen/schema.js +16 -13
- package/dist/codegen/schema.test.js +46 -47
- package/dist/codegen/util.js +13 -17
- package/dist/codegen/util.test.js +2 -2
- package/dist/scaffold/schema.js +10 -5
- package/package.json +1 -1
- package/src/codegen/{schema.test.js → schema.test.ts} +10 -10
- package/src/codegen/{schema.js → schema.ts} +34 -32
- package/src/codegen/typescript.ts +22 -14
- package/src/codegen/{util.test.js → util.test.ts} +2 -2
- package/src/codegen/{util.js → util.ts} +23 -21
- package/src/scaffold/index.ts +3 -3
- package/src/scaffold/schema.ts +11 -6
- package/src/schema.ts +1 -1
package/CHANGELOG.md
CHANGED
package/dist/codegen/schema.js
CHANGED
|
@@ -29,10 +29,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
29
29
|
const immutable_1 = __importDefault(require("immutable"));
|
|
30
30
|
const tsCodegen = __importStar(require("./typescript"));
|
|
31
31
|
const typesCodegen = __importStar(require("./types"));
|
|
32
|
-
const List = immutable_1.default.List;
|
|
33
32
|
class IdField {
|
|
34
33
|
static BYTES = Symbol('Bytes');
|
|
35
34
|
static STRING = Symbol('String');
|
|
35
|
+
kind;
|
|
36
36
|
constructor(idField) {
|
|
37
37
|
const typeName = idField.getIn(['type', 'type', 'name', 'value']);
|
|
38
38
|
this.kind = typeName === 'Bytes' ? IdField.BYTES : IdField.STRING;
|
|
@@ -67,8 +67,10 @@ class IdField {
|
|
|
67
67
|
}
|
|
68
68
|
}
|
|
69
69
|
class SchemaCodeGenerator {
|
|
70
|
+
schema;
|
|
70
71
|
constructor(schema) {
|
|
71
72
|
this.schema = schema;
|
|
73
|
+
this.schema = schema;
|
|
72
74
|
}
|
|
73
75
|
generateModuleImports() {
|
|
74
76
|
return [
|
|
@@ -90,14 +92,15 @@ class SchemaCodeGenerator {
|
|
|
90
92
|
generateTypes() {
|
|
91
93
|
return this.schema.ast
|
|
92
94
|
.get('definitions')
|
|
93
|
-
.filter(def => this._isEntityTypeDefinition(def))
|
|
94
|
-
.map(def => this._generateEntityType(def));
|
|
95
|
+
.filter((def) => this._isEntityTypeDefinition(def))
|
|
96
|
+
.map((def) => this._generateEntityType(def));
|
|
95
97
|
}
|
|
96
98
|
_isEntityTypeDefinition(def) {
|
|
97
99
|
return (def.get('kind') === 'ObjectTypeDefinition' &&
|
|
98
100
|
def
|
|
99
101
|
.get('directives')
|
|
100
|
-
.find(directive => directive.getIn(['name', 'value']) === 'entity') !==
|
|
102
|
+
.find((directive) => directive.getIn(['name', 'value']) === 'entity') !==
|
|
103
|
+
undefined);
|
|
101
104
|
}
|
|
102
105
|
_isInterfaceDefinition(def) {
|
|
103
106
|
return def.get('kind') === 'InterfaceTypeDefinition';
|
|
@@ -114,11 +117,11 @@ class SchemaCodeGenerator {
|
|
|
114
117
|
// Generate and add entity field getters and setters
|
|
115
118
|
def
|
|
116
119
|
.get('fields')
|
|
117
|
-
.reduce((methods, field) => methods.concat(this._generateEntityFieldMethods(def, field)), List())
|
|
118
|
-
.forEach(method => klass.addMethod(method));
|
|
120
|
+
.reduce((methods, field) => methods.concat(this._generateEntityFieldMethods(def, field)), immutable_1.default.List())
|
|
121
|
+
.forEach((method) => klass.addMethod(method));
|
|
119
122
|
return klass;
|
|
120
123
|
}
|
|
121
|
-
_generateConstructor(
|
|
124
|
+
_generateConstructor(_entityName, fields) {
|
|
122
125
|
const idField = IdField.fromFields(fields);
|
|
123
126
|
return tsCodegen.method('constructor', [tsCodegen.param('id', idField.tsNamedType())], undefined, `
|
|
124
127
|
super()
|
|
@@ -126,7 +129,7 @@ class SchemaCodeGenerator {
|
|
|
126
129
|
`);
|
|
127
130
|
}
|
|
128
131
|
_generateStoreMethods(entityName, idField) {
|
|
129
|
-
return List.of(tsCodegen.method('save', [], tsCodegen.namedType('void'), `
|
|
132
|
+
return immutable_1.default.List.of(tsCodegen.method('save', [], tsCodegen.namedType('void'), `
|
|
130
133
|
let id = this.get('id')
|
|
131
134
|
assert(id != null,
|
|
132
135
|
'Cannot save ${entityName} entity without an ID')
|
|
@@ -139,12 +142,12 @@ class SchemaCodeGenerator {
|
|
|
139
142
|
`));
|
|
140
143
|
}
|
|
141
144
|
_generateEntityFieldMethods(entityDef, fieldDef) {
|
|
142
|
-
return List([
|
|
145
|
+
return immutable_1.default.List([
|
|
143
146
|
this._generateEntityFieldGetter(entityDef, fieldDef),
|
|
144
147
|
this._generateEntityFieldSetter(entityDef, fieldDef),
|
|
145
148
|
]);
|
|
146
149
|
}
|
|
147
|
-
_generateEntityFieldGetter(
|
|
150
|
+
_generateEntityFieldGetter(_entityDef, fieldDef) {
|
|
148
151
|
let name = fieldDef.getIn(['name', 'value']);
|
|
149
152
|
let gqlType = fieldDef.get('type');
|
|
150
153
|
let fieldValueType = this._valueTypeFromGraphQl(gqlType);
|
|
@@ -161,7 +164,7 @@ class SchemaCodeGenerator {
|
|
|
161
164
|
${isNullable ? getNullable : getNonNullable}
|
|
162
165
|
`);
|
|
163
166
|
}
|
|
164
|
-
_generateEntityFieldSetter(
|
|
167
|
+
_generateEntityFieldSetter(_entityDef, fieldDef) {
|
|
165
168
|
let name = fieldDef.getIn(['name', 'value']);
|
|
166
169
|
let gqlType = fieldDef.get('type');
|
|
167
170
|
let fieldValueType = this._valueTypeFromGraphQl(gqlType);
|
|
@@ -194,7 +197,7 @@ Suggestion: add an '!' to the member type of the List, change from '[${baseType}
|
|
|
194
197
|
// the referred type's id field
|
|
195
198
|
const typeDef = this.schema.ast
|
|
196
199
|
.get('definitions')
|
|
197
|
-
.find(def => (this._isEntityTypeDefinition(def) || this._isInterfaceDefinition(def)) &&
|
|
200
|
+
.find((def) => (this._isEntityTypeDefinition(def) || this._isInterfaceDefinition(def)) &&
|
|
198
201
|
def.getIn(['name', 'value']) === typeName);
|
|
199
202
|
if (typeDef) {
|
|
200
203
|
return IdField.fromTypeDef(typeDef).typeName();
|
|
@@ -209,7 +212,7 @@ Suggestion: add an '!' to the member type of the List, change from '[${baseType}
|
|
|
209
212
|
* referred type, i.e., `string` or `Bytes`*/
|
|
210
213
|
_valueTypeFromGraphQl(gqlType) {
|
|
211
214
|
if (gqlType.get('kind') === 'NonNullType') {
|
|
212
|
-
return this._valueTypeFromGraphQl(gqlType.get('type')
|
|
215
|
+
return this._valueTypeFromGraphQl(gqlType.get('type'));
|
|
213
216
|
}
|
|
214
217
|
else if (gqlType.get('kind') === 'ListType') {
|
|
215
218
|
return '[' + this._valueTypeFromGraphQl(gqlType.get('type')) + ']';
|
|
@@ -29,24 +29,23 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
29
29
|
const prettier_1 = __importDefault(require("prettier"));
|
|
30
30
|
const graphql = __importStar(require("graphql/language"));
|
|
31
31
|
const immutable_1 = __importDefault(require("immutable"));
|
|
32
|
-
const schema_1 = __importDefault(require("
|
|
33
|
-
const
|
|
34
|
-
const
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
});
|
|
32
|
+
const schema_1 = __importDefault(require("../schema"));
|
|
33
|
+
const schema_2 = __importDefault(require("./schema"));
|
|
34
|
+
const typescript_1 = require("./typescript");
|
|
35
|
+
const formatTS = (code) => prettier_1.default.format(code, { parser: 'typescript', semi: false });
|
|
36
|
+
const createSchemaCodeGen = (schema) => new schema_2.default(new schema_1.default('', schema, immutable_1.default.fromJS(graphql.parse(schema))));
|
|
38
37
|
const testEntity = (generatedTypes, expectedEntity) => {
|
|
39
38
|
const entity = generatedTypes.find(type => type.name === expectedEntity.name);
|
|
40
|
-
expect(entity instanceof Class).toBe(true);
|
|
39
|
+
expect(entity instanceof typescript_1.Class).toBe(true);
|
|
41
40
|
expect(entity.extends).toBe('Entity');
|
|
42
41
|
expect(entity.export).toBe(true);
|
|
43
42
|
const { members, methods } = entity;
|
|
44
43
|
expect(members).toStrictEqual(expectedEntity.members);
|
|
45
44
|
for (const expectedMethod of expectedEntity.methods) {
|
|
46
|
-
const method = methods.find(method => method.name === expectedMethod.name);
|
|
45
|
+
const method = methods.find((method) => method.name === expectedMethod.name);
|
|
47
46
|
expectedMethod.static
|
|
48
|
-
? expect(method instanceof StaticMethod).toBe(true)
|
|
49
|
-
: expect(method instanceof Method).toBe(true);
|
|
47
|
+
? expect(method instanceof typescript_1.StaticMethod).toBe(true)
|
|
48
|
+
: expect(method instanceof typescript_1.Method).toBe(true);
|
|
50
49
|
expect(method.params).toStrictEqual(expectedMethod.params);
|
|
51
50
|
expect(method.returnType).toStrictEqual(expectedMethod.returnType);
|
|
52
51
|
expect(formatTS(method.body)).toBe(formatTS(expectedMethod.body));
|
|
@@ -96,7 +95,7 @@ describe('Schema code generator', () => {
|
|
|
96
95
|
`);
|
|
97
96
|
const generatedTypes = codegen.generateTypes();
|
|
98
97
|
test('Foo is NOT an entity', () => {
|
|
99
|
-
const foo = generatedTypes.find(type => type.name === 'Foo');
|
|
98
|
+
const foo = generatedTypes.find((type) => type.name === 'Foo');
|
|
100
99
|
expect(foo).toBe(undefined);
|
|
101
100
|
// Account and Wallet
|
|
102
101
|
expect(generatedTypes.size).toBe(2);
|
|
@@ -108,7 +107,7 @@ describe('Schema code generator', () => {
|
|
|
108
107
|
methods: [
|
|
109
108
|
{
|
|
110
109
|
name: 'constructor',
|
|
111
|
-
params: [new Param('id', new NamedType('string'))],
|
|
110
|
+
params: [new typescript_1.Param('id', new typescript_1.NamedType('string'))],
|
|
112
111
|
returnType: undefined,
|
|
113
112
|
body: `
|
|
114
113
|
super()
|
|
@@ -118,7 +117,7 @@ describe('Schema code generator', () => {
|
|
|
118
117
|
{
|
|
119
118
|
name: 'save',
|
|
120
119
|
params: [],
|
|
121
|
-
returnType: new NamedType('void'),
|
|
120
|
+
returnType: new typescript_1.NamedType('void'),
|
|
122
121
|
body: `
|
|
123
122
|
let id = this.get('id')
|
|
124
123
|
assert(id != null, 'Cannot save Account entity without an ID')
|
|
@@ -133,8 +132,8 @@ describe('Schema code generator', () => {
|
|
|
133
132
|
{
|
|
134
133
|
name: 'load',
|
|
135
134
|
static: true,
|
|
136
|
-
params: [new Param('id', new NamedType('string'))],
|
|
137
|
-
returnType: new NullableType(new NamedType('Account')),
|
|
135
|
+
params: [new typescript_1.Param('id', new typescript_1.NamedType('string'))],
|
|
136
|
+
returnType: new typescript_1.NullableType(new typescript_1.NamedType('Account')),
|
|
138
137
|
body: `
|
|
139
138
|
return changetype<Account | null>(store.get('Account', id))
|
|
140
139
|
`,
|
|
@@ -142,7 +141,7 @@ describe('Schema code generator', () => {
|
|
|
142
141
|
{
|
|
143
142
|
name: 'get id',
|
|
144
143
|
params: [],
|
|
145
|
-
returnType: new NamedType('string'),
|
|
144
|
+
returnType: new typescript_1.NamedType('string'),
|
|
146
145
|
body: `
|
|
147
146
|
let value = this.get('id')
|
|
148
147
|
return value!.toString()
|
|
@@ -150,7 +149,7 @@ describe('Schema code generator', () => {
|
|
|
150
149
|
},
|
|
151
150
|
{
|
|
152
151
|
name: 'set id',
|
|
153
|
-
params: [new Param('value', new NamedType('string'))],
|
|
152
|
+
params: [new typescript_1.Param('value', new typescript_1.NamedType('string'))],
|
|
154
153
|
returnType: undefined,
|
|
155
154
|
body: `
|
|
156
155
|
this.set('id', Value.fromString(value))
|
|
@@ -159,7 +158,7 @@ describe('Schema code generator', () => {
|
|
|
159
158
|
{
|
|
160
159
|
name: 'get description',
|
|
161
160
|
params: [],
|
|
162
|
-
returnType: new NullableType(new NamedType('string')),
|
|
161
|
+
returnType: new typescript_1.NullableType(new typescript_1.NamedType('string')),
|
|
163
162
|
body: `
|
|
164
163
|
let value = this.get('description')
|
|
165
164
|
if (!value || value.kind == ValueKind.NULL) {
|
|
@@ -171,7 +170,7 @@ describe('Schema code generator', () => {
|
|
|
171
170
|
},
|
|
172
171
|
{
|
|
173
172
|
name: 'set description',
|
|
174
|
-
params: [new Param('value', new NullableType(new NamedType('string')))],
|
|
173
|
+
params: [new typescript_1.Param('value', new typescript_1.NullableType(new typescript_1.NamedType('string')))],
|
|
175
174
|
returnType: undefined,
|
|
176
175
|
body: `
|
|
177
176
|
if (!value) {
|
|
@@ -184,7 +183,7 @@ describe('Schema code generator', () => {
|
|
|
184
183
|
{
|
|
185
184
|
name: 'get name',
|
|
186
185
|
params: [],
|
|
187
|
-
returnType: new NamedType('string'),
|
|
186
|
+
returnType: new typescript_1.NamedType('string'),
|
|
188
187
|
body: `
|
|
189
188
|
let value = this.get('name')
|
|
190
189
|
return value!.toString()
|
|
@@ -192,7 +191,7 @@ describe('Schema code generator', () => {
|
|
|
192
191
|
},
|
|
193
192
|
{
|
|
194
193
|
name: 'set name',
|
|
195
|
-
params: [new Param('value', new NamedType('string'))],
|
|
194
|
+
params: [new typescript_1.Param('value', new typescript_1.NamedType('string'))],
|
|
196
195
|
returnType: undefined,
|
|
197
196
|
body: `
|
|
198
197
|
this.set('name', Value.fromString(value))
|
|
@@ -201,7 +200,7 @@ describe('Schema code generator', () => {
|
|
|
201
200
|
{
|
|
202
201
|
name: 'get age',
|
|
203
202
|
params: [],
|
|
204
|
-
returnType: new NamedType('i32'),
|
|
203
|
+
returnType: new typescript_1.NamedType('i32'),
|
|
205
204
|
body: `
|
|
206
205
|
let value = this.get('age')
|
|
207
206
|
return value!.toI32()
|
|
@@ -209,7 +208,7 @@ describe('Schema code generator', () => {
|
|
|
209
208
|
},
|
|
210
209
|
{
|
|
211
210
|
name: 'set age',
|
|
212
|
-
params: [new Param('value', new NamedType('i32'))],
|
|
211
|
+
params: [new typescript_1.Param('value', new typescript_1.NamedType('i32'))],
|
|
213
212
|
returnType: undefined,
|
|
214
213
|
body: `
|
|
215
214
|
this.set('age', Value.fromI32(value))
|
|
@@ -218,7 +217,7 @@ describe('Schema code generator', () => {
|
|
|
218
217
|
{
|
|
219
218
|
name: 'get count',
|
|
220
219
|
params: [],
|
|
221
|
-
returnType: new NamedType('i32'),
|
|
220
|
+
returnType: new typescript_1.NamedType('i32'),
|
|
222
221
|
body: `
|
|
223
222
|
let value = this.get('count')
|
|
224
223
|
return value!.toI32()
|
|
@@ -226,7 +225,7 @@ describe('Schema code generator', () => {
|
|
|
226
225
|
},
|
|
227
226
|
{
|
|
228
227
|
name: 'set count',
|
|
229
|
-
params: [new Param('value', new NamedType('i32'))],
|
|
228
|
+
params: [new typescript_1.Param('value', new typescript_1.NamedType('i32'))],
|
|
230
229
|
returnType: undefined,
|
|
231
230
|
body: `
|
|
232
231
|
this.set('count', Value.fromI32(value))
|
|
@@ -235,7 +234,7 @@ describe('Schema code generator', () => {
|
|
|
235
234
|
{
|
|
236
235
|
name: 'get wallets',
|
|
237
236
|
params: [],
|
|
238
|
-
returnType: new NullableType(new ArrayType(new NamedType('string'))),
|
|
237
|
+
returnType: new typescript_1.NullableType(new typescript_1.ArrayType(new typescript_1.NamedType('string'))),
|
|
239
238
|
body: `
|
|
240
239
|
let value = this.get('wallets')
|
|
241
240
|
if (!value || value.kind == ValueKind.NULL) {
|
|
@@ -248,7 +247,7 @@ describe('Schema code generator', () => {
|
|
|
248
247
|
{
|
|
249
248
|
name: 'set wallets',
|
|
250
249
|
params: [
|
|
251
|
-
new Param('value', new NullableType(new ArrayType(new NamedType('string')))),
|
|
250
|
+
new typescript_1.Param('value', new typescript_1.NullableType(new typescript_1.ArrayType(new typescript_1.NamedType('string')))),
|
|
252
251
|
],
|
|
253
252
|
returnType: undefined,
|
|
254
253
|
body: `
|
|
@@ -269,7 +268,7 @@ describe('Schema code generator', () => {
|
|
|
269
268
|
methods: [
|
|
270
269
|
{
|
|
271
270
|
name: 'constructor',
|
|
272
|
-
params: [new Param('id', new NamedType('string'))],
|
|
271
|
+
params: [new typescript_1.Param('id', new typescript_1.NamedType('string'))],
|
|
273
272
|
returnType: undefined,
|
|
274
273
|
body: `
|
|
275
274
|
super()
|
|
@@ -279,7 +278,7 @@ describe('Schema code generator', () => {
|
|
|
279
278
|
{
|
|
280
279
|
name: 'save',
|
|
281
280
|
params: [],
|
|
282
|
-
returnType: new NamedType('void'),
|
|
281
|
+
returnType: new typescript_1.NamedType('void'),
|
|
283
282
|
body: `
|
|
284
283
|
let id = this.get('id')
|
|
285
284
|
assert(id != null, 'Cannot save Wallet entity without an ID')
|
|
@@ -294,8 +293,8 @@ describe('Schema code generator', () => {
|
|
|
294
293
|
{
|
|
295
294
|
name: 'load',
|
|
296
295
|
static: true,
|
|
297
|
-
params: [new Param('id', new NamedType('string'))],
|
|
298
|
-
returnType: new NullableType(new NamedType('Wallet')),
|
|
296
|
+
params: [new typescript_1.Param('id', new typescript_1.NamedType('string'))],
|
|
297
|
+
returnType: new typescript_1.NullableType(new typescript_1.NamedType('Wallet')),
|
|
299
298
|
body: `
|
|
300
299
|
return changetype<Wallet | null>(store.get('Wallet', id))
|
|
301
300
|
`,
|
|
@@ -303,7 +302,7 @@ describe('Schema code generator', () => {
|
|
|
303
302
|
{
|
|
304
303
|
name: 'get id',
|
|
305
304
|
params: [],
|
|
306
|
-
returnType: new NamedType('string'),
|
|
305
|
+
returnType: new typescript_1.NamedType('string'),
|
|
307
306
|
body: `
|
|
308
307
|
let value = this.get('id')
|
|
309
308
|
return value!.toString()
|
|
@@ -311,7 +310,7 @@ describe('Schema code generator', () => {
|
|
|
311
310
|
},
|
|
312
311
|
{
|
|
313
312
|
name: 'set id',
|
|
314
|
-
params: [new Param('value', new NamedType('string'))],
|
|
313
|
+
params: [new typescript_1.Param('value', new typescript_1.NamedType('string'))],
|
|
315
314
|
returnType: undefined,
|
|
316
315
|
body: `
|
|
317
316
|
this.set('id', Value.fromString(value))
|
|
@@ -320,7 +319,7 @@ describe('Schema code generator', () => {
|
|
|
320
319
|
{
|
|
321
320
|
name: 'get amount',
|
|
322
321
|
params: [],
|
|
323
|
-
returnType: new NamedType('BigInt'),
|
|
322
|
+
returnType: new typescript_1.NamedType('BigInt'),
|
|
324
323
|
body: `
|
|
325
324
|
let value = this.get('amount')
|
|
326
325
|
return value!.toBigInt()
|
|
@@ -328,7 +327,7 @@ describe('Schema code generator', () => {
|
|
|
328
327
|
},
|
|
329
328
|
{
|
|
330
329
|
name: 'set amount',
|
|
331
|
-
params: [new Param('value', new NamedType('BigInt'))],
|
|
330
|
+
params: [new typescript_1.Param('value', new typescript_1.NamedType('BigInt'))],
|
|
332
331
|
returnType: undefined,
|
|
333
332
|
body: `
|
|
334
333
|
this.set('amount', Value.fromBigInt(value))
|
|
@@ -337,7 +336,7 @@ describe('Schema code generator', () => {
|
|
|
337
336
|
{
|
|
338
337
|
name: 'get account',
|
|
339
338
|
params: [],
|
|
340
|
-
returnType: new NamedType('string'),
|
|
339
|
+
returnType: new typescript_1.NamedType('string'),
|
|
341
340
|
body: `
|
|
342
341
|
let value = this.get('account')
|
|
343
342
|
return value!.toString()
|
|
@@ -345,7 +344,7 @@ describe('Schema code generator', () => {
|
|
|
345
344
|
},
|
|
346
345
|
{
|
|
347
346
|
name: 'set account',
|
|
348
|
-
params: [new Param('value', new NamedType('string'))],
|
|
347
|
+
params: [new typescript_1.Param('value', new typescript_1.NamedType('string'))],
|
|
349
348
|
returnType: undefined,
|
|
350
349
|
body: `
|
|
351
350
|
this.set('account', Value.fromString(value))
|
|
@@ -380,14 +379,14 @@ describe('Schema code generator', () => {
|
|
|
380
379
|
methods: [
|
|
381
380
|
{
|
|
382
381
|
name: 'constructor',
|
|
383
|
-
params: [new Param('id', new NamedType('Bytes'))],
|
|
382
|
+
params: [new typescript_1.Param('id', new typescript_1.NamedType('Bytes'))],
|
|
384
383
|
returnType: undefined,
|
|
385
384
|
body: "\n super()\n this.set('id', Value.fromBytes(id))\n ",
|
|
386
385
|
},
|
|
387
386
|
{
|
|
388
387
|
name: 'save',
|
|
389
388
|
params: [],
|
|
390
|
-
returnType: new NamedType('void'),
|
|
389
|
+
returnType: new typescript_1.NamedType('void'),
|
|
391
390
|
body: '\n' +
|
|
392
391
|
" let id = this.get('id')\n" +
|
|
393
392
|
' assert(id != null,\n' +
|
|
@@ -401,8 +400,8 @@ describe('Schema code generator', () => {
|
|
|
401
400
|
{
|
|
402
401
|
name: 'load',
|
|
403
402
|
static: true,
|
|
404
|
-
params: [new Param('id', new NamedType('Bytes'))],
|
|
405
|
-
returnType: new NullableType(new NamedType('Task')),
|
|
403
|
+
params: [new typescript_1.Param('id', new typescript_1.NamedType('Bytes'))],
|
|
404
|
+
returnType: new typescript_1.NullableType(new typescript_1.NamedType('Task')),
|
|
406
405
|
body: '\n' +
|
|
407
406
|
" return changetype<Task | null>(store.get('Task', id.toHexString()))\n" +
|
|
408
407
|
' ',
|
|
@@ -410,7 +409,7 @@ describe('Schema code generator', () => {
|
|
|
410
409
|
{
|
|
411
410
|
name: 'get id',
|
|
412
411
|
params: [],
|
|
413
|
-
returnType: new NamedType('Bytes'),
|
|
412
|
+
returnType: new typescript_1.NamedType('Bytes'),
|
|
414
413
|
body: '\n' +
|
|
415
414
|
" let value = this.get('id')\n" +
|
|
416
415
|
' return value!.toBytes()\n' +
|
|
@@ -418,14 +417,14 @@ describe('Schema code generator', () => {
|
|
|
418
417
|
},
|
|
419
418
|
{
|
|
420
419
|
name: 'set id',
|
|
421
|
-
params: [new Param('value', new NamedType('Bytes'))],
|
|
420
|
+
params: [new typescript_1.Param('value', new typescript_1.NamedType('Bytes'))],
|
|
422
421
|
returnType: undefined,
|
|
423
422
|
body: "\n this.set('id', Value.fromBytes(value))\n ",
|
|
424
423
|
},
|
|
425
424
|
{
|
|
426
425
|
name: 'get employee',
|
|
427
426
|
params: [],
|
|
428
|
-
returnType: new NamedType('Bytes'),
|
|
427
|
+
returnType: new typescript_1.NamedType('Bytes'),
|
|
429
428
|
body: '\n' +
|
|
430
429
|
" let value = this.get('employee')\n" +
|
|
431
430
|
' return value!.toBytes()\n' +
|
|
@@ -433,14 +432,14 @@ describe('Schema code generator', () => {
|
|
|
433
432
|
},
|
|
434
433
|
{
|
|
435
434
|
name: 'set employee',
|
|
436
|
-
params: [new Param('value', new NamedType('Bytes'))],
|
|
435
|
+
params: [new typescript_1.Param('value', new typescript_1.NamedType('Bytes'))],
|
|
437
436
|
returnType: undefined,
|
|
438
437
|
body: "\n this.set('employee', Value.fromBytes(value))\n ",
|
|
439
438
|
},
|
|
440
439
|
{
|
|
441
440
|
name: 'get worker',
|
|
442
441
|
params: [],
|
|
443
|
-
returnType: new NamedType('Bytes'),
|
|
442
|
+
returnType: new typescript_1.NamedType('Bytes'),
|
|
444
443
|
body: '\n' +
|
|
445
444
|
" let value = this.get('worker')\n" +
|
|
446
445
|
' return value!.toBytes()\n' +
|
|
@@ -448,7 +447,7 @@ describe('Schema code generator', () => {
|
|
|
448
447
|
},
|
|
449
448
|
{
|
|
450
449
|
name: 'set worker',
|
|
451
|
-
params: [new Param('value', new NamedType('Bytes'))],
|
|
450
|
+
params: [new typescript_1.Param('value', new typescript_1.NamedType('Bytes'))],
|
|
452
451
|
returnType: undefined,
|
|
453
452
|
body: "\n this.set('worker', Value.fromBytes(value))\n ",
|
|
454
453
|
},
|
package/dist/codegen/util.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.unrollTuple = exports.isTupleMatrixType = exports.isTupleArrayType = exports.
|
|
4
|
-
|
|
3
|
+
exports.unrollTuple = exports.isTupleMatrixType = exports.isTupleArrayType = exports.containsTupleType = exports.isTupleType = exports.disambiguateNames = void 0;
|
|
4
|
+
function disambiguateNames({ values, getName, setName, }) {
|
|
5
5
|
let collisionCounter = new Map();
|
|
6
6
|
return values.map((value, index) => {
|
|
7
7
|
let name = getName(value, index);
|
|
@@ -15,32 +15,28 @@ const disambiguateNames = ({ values, getName, setName }) => {
|
|
|
15
15
|
return setName(value, `${name}${counter}`);
|
|
16
16
|
}
|
|
17
17
|
});
|
|
18
|
-
}
|
|
18
|
+
}
|
|
19
19
|
exports.disambiguateNames = disambiguateNames;
|
|
20
|
-
|
|
20
|
+
function isTupleType(t) {
|
|
21
21
|
return t === 'tuple';
|
|
22
|
-
}
|
|
22
|
+
}
|
|
23
23
|
exports.isTupleType = isTupleType;
|
|
24
|
-
|
|
24
|
+
function containsTupleType(t) {
|
|
25
25
|
return isTupleType(t) || isTupleArrayType(t) || isTupleMatrixType(t);
|
|
26
|
-
}
|
|
26
|
+
}
|
|
27
27
|
exports.containsTupleType = containsTupleType;
|
|
28
|
-
|
|
28
|
+
function isTupleArrayType(t) {
|
|
29
29
|
return t.match(/^tuple\[([0-9]+)?\]$/);
|
|
30
|
-
}
|
|
30
|
+
}
|
|
31
31
|
exports.isTupleArrayType = isTupleArrayType;
|
|
32
|
-
|
|
32
|
+
function isTupleMatrixType(t) {
|
|
33
33
|
return t.match(/^tuple\[([0-9]+)?\]\[([0-9]+)?\]$/);
|
|
34
|
-
}
|
|
34
|
+
}
|
|
35
35
|
exports.isTupleMatrixType = isTupleMatrixType;
|
|
36
|
-
const unrollTuple = ({ path,
|
|
36
|
+
const unrollTuple = ({ path, value, }) => value.components.reduce((acc, component, index) => {
|
|
37
37
|
let name = component.name || `value${index}`;
|
|
38
38
|
return acc.concat(component.type === 'tuple'
|
|
39
|
-
? unrollTuple({
|
|
40
|
-
path: [...path, name],
|
|
41
|
-
index,
|
|
42
|
-
value: component,
|
|
43
|
-
})
|
|
39
|
+
? (0, exports.unrollTuple)({ path: [...path, name], index, value: component })
|
|
44
40
|
: [{ path: [...path, name], type: component.type }]);
|
|
45
41
|
}, []);
|
|
46
42
|
exports.unrollTuple = unrollTuple;
|
|
@@ -6,12 +6,12 @@ describe('Codegen utilities', () => {
|
|
|
6
6
|
expect((0, util_1.disambiguateNames)({
|
|
7
7
|
values: ['a', 'b', 'c'],
|
|
8
8
|
getName: x => x,
|
|
9
|
-
setName: (
|
|
9
|
+
setName: (_x, name) => name,
|
|
10
10
|
})).toEqual(['a', 'b', 'c']);
|
|
11
11
|
expect((0, util_1.disambiguateNames)({
|
|
12
12
|
values: ['a', 'a', 'a'],
|
|
13
13
|
getName: x => x,
|
|
14
|
-
setName: (
|
|
14
|
+
setName: (_x, name) => name,
|
|
15
15
|
})).toEqual(['a', 'a1', 'a2']);
|
|
16
16
|
expect((0, util_1.disambiguateNames)({
|
|
17
17
|
values: [
|
package/dist/scaffold/schema.js
CHANGED
|
@@ -26,11 +26,16 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
26
26
|
exports.generateExampleEntityType = exports.generateEventType = exports.generateEventFields = exports.generateField = exports.protocolTypeToGraphQL = exports.abiEvents = void 0;
|
|
27
27
|
const types_1 = require("../codegen/types");
|
|
28
28
|
const util = __importStar(require("../codegen/util"));
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
29
|
+
function abiEvents(abi) {
|
|
30
|
+
return util.disambiguateNames({
|
|
31
|
+
// @ts-expect-error improve typings of disambiguateNames to handle iterables
|
|
32
|
+
values: abi.data.filter(item => item.get('type') === 'event'),
|
|
33
|
+
// @ts-expect-error improve typings of disambiguateNames to handle iterables
|
|
34
|
+
getName: event => event.get('name'),
|
|
35
|
+
// @ts-expect-error improve typings of disambiguateNames to handle iterables
|
|
36
|
+
setName: (event, name) => event.set('_alias', name),
|
|
37
|
+
});
|
|
38
|
+
}
|
|
34
39
|
exports.abiEvents = abiEvents;
|
|
35
40
|
const protocolTypeToGraphQL = (protocol, name) => {
|
|
36
41
|
let ascType = (0, types_1.ascTypeForProtocol)(protocol, name);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@graphprotocol/graph-cli",
|
|
3
|
-
"version": "0.37.3-alpha-
|
|
3
|
+
"version": "0.37.3-alpha-20230111190205-1b7965c",
|
|
4
4
|
"license": "(Apache-2.0 OR MIT)",
|
|
5
5
|
"description": "CLI for building for and deploying to The Graph",
|
|
6
6
|
"main": "dist/cli.js",
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import prettier from 'prettier'
|
|
2
2
|
import * as graphql from 'graphql/language'
|
|
3
3
|
import immutable from 'immutable'
|
|
4
|
+
import Schema from '../schema'
|
|
4
5
|
import SchemaCodeGenerator from './schema'
|
|
5
|
-
|
|
6
|
+
import {
|
|
6
7
|
Class,
|
|
7
8
|
Method,
|
|
8
9
|
StaticMethod,
|
|
@@ -10,16 +11,15 @@ const {
|
|
|
10
11
|
NamedType,
|
|
11
12
|
NullableType,
|
|
12
13
|
ArrayType,
|
|
13
|
-
}
|
|
14
|
+
} from './typescript'
|
|
14
15
|
|
|
15
|
-
const formatTS =
|
|
16
|
+
const formatTS = (code: string) =>
|
|
17
|
+
prettier.format(code, { parser: 'typescript', semi: false })
|
|
16
18
|
|
|
17
|
-
const createSchemaCodeGen = schema =>
|
|
18
|
-
new SchemaCodeGenerator(
|
|
19
|
-
ast: immutable.fromJS(graphql.parse(schema)),
|
|
20
|
-
})
|
|
19
|
+
const createSchemaCodeGen = (schema: string) =>
|
|
20
|
+
new SchemaCodeGenerator(new Schema('', schema, immutable.fromJS(graphql.parse(schema))))
|
|
21
21
|
|
|
22
|
-
const testEntity = (generatedTypes, expectedEntity) => {
|
|
22
|
+
const testEntity = (generatedTypes: any[], expectedEntity: any) => {
|
|
23
23
|
const entity = generatedTypes.find(type => type.name === expectedEntity.name)
|
|
24
24
|
|
|
25
25
|
expect(entity instanceof Class).toBe(true)
|
|
@@ -31,7 +31,7 @@ const testEntity = (generatedTypes, expectedEntity) => {
|
|
|
31
31
|
expect(members).toStrictEqual(expectedEntity.members)
|
|
32
32
|
|
|
33
33
|
for (const expectedMethod of expectedEntity.methods) {
|
|
34
|
-
const method = methods.find(method => method.name === expectedMethod.name)
|
|
34
|
+
const method = methods.find((method: any) => method.name === expectedMethod.name)
|
|
35
35
|
|
|
36
36
|
expectedMethod.static
|
|
37
37
|
? expect(method instanceof StaticMethod).toBe(true)
|
|
@@ -92,7 +92,7 @@ describe('Schema code generator', () => {
|
|
|
92
92
|
const generatedTypes = codegen.generateTypes()
|
|
93
93
|
|
|
94
94
|
test('Foo is NOT an entity', () => {
|
|
95
|
-
const foo = generatedTypes.find(type => type.name === 'Foo')
|
|
95
|
+
const foo = generatedTypes.find((type: any) => type.name === 'Foo')
|
|
96
96
|
expect(foo).toBe(undefined)
|
|
97
97
|
// Account and Wallet
|
|
98
98
|
expect(generatedTypes.size).toBe(2)
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import immutable from 'immutable'
|
|
2
|
-
|
|
3
2
|
import * as tsCodegen from './typescript'
|
|
4
3
|
import * as typesCodegen from './types'
|
|
5
|
-
|
|
6
|
-
const List = immutable.List
|
|
4
|
+
import Schema from '../schema'
|
|
7
5
|
|
|
8
6
|
class IdField {
|
|
9
7
|
static BYTES = Symbol('Bytes')
|
|
10
8
|
static STRING = Symbol('String')
|
|
11
9
|
|
|
12
|
-
|
|
10
|
+
private kind: typeof IdField.BYTES | typeof IdField.STRING
|
|
11
|
+
|
|
12
|
+
constructor(idField: immutable.Map<any, any>) {
|
|
13
13
|
const typeName = idField.getIn(['type', 'type', 'name', 'value'])
|
|
14
14
|
this.kind = typeName === 'Bytes' ? IdField.BYTES : IdField.STRING
|
|
15
15
|
}
|
|
@@ -42,18 +42,18 @@ class IdField {
|
|
|
42
42
|
return this.kind == IdField.BYTES ? 'id.toHexString()' : 'id'
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
-
static fromFields(fields) {
|
|
45
|
+
static fromFields(fields: immutable.List<any>) {
|
|
46
46
|
const idField = fields.find(field => field.getIn(['name', 'value']) === 'id')
|
|
47
47
|
return new IdField(idField)
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
-
static fromTypeDef(def) {
|
|
50
|
+
static fromTypeDef(def: immutable.Map<any, any>) {
|
|
51
51
|
return IdField.fromFields(def.get('fields'))
|
|
52
52
|
}
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
export default class SchemaCodeGenerator {
|
|
56
|
-
constructor(schema) {
|
|
56
|
+
constructor(private schema: Schema) {
|
|
57
57
|
this.schema = schema
|
|
58
58
|
}
|
|
59
59
|
|
|
@@ -83,25 +83,26 @@ export default class SchemaCodeGenerator {
|
|
|
83
83
|
generateTypes() {
|
|
84
84
|
return this.schema.ast
|
|
85
85
|
.get('definitions')
|
|
86
|
-
.filter(def => this._isEntityTypeDefinition(def))
|
|
87
|
-
.map(def => this._generateEntityType(def))
|
|
86
|
+
.filter((def: any) => this._isEntityTypeDefinition(def))
|
|
87
|
+
.map((def: any) => this._generateEntityType(def))
|
|
88
88
|
}
|
|
89
89
|
|
|
90
|
-
_isEntityTypeDefinition(def) {
|
|
90
|
+
_isEntityTypeDefinition(def: immutable.Map<any, any>) {
|
|
91
91
|
return (
|
|
92
92
|
def.get('kind') === 'ObjectTypeDefinition' &&
|
|
93
93
|
def
|
|
94
94
|
.get('directives')
|
|
95
|
-
.find(directive => directive.getIn(['name', 'value']) === 'entity') !==
|
|
95
|
+
.find((directive: any) => directive.getIn(['name', 'value']) === 'entity') !==
|
|
96
|
+
undefined
|
|
96
97
|
)
|
|
97
98
|
}
|
|
98
99
|
|
|
99
|
-
_isInterfaceDefinition(def) {
|
|
100
|
+
_isInterfaceDefinition(def: immutable.Map<any, any>) {
|
|
100
101
|
return def.get('kind') === 'InterfaceTypeDefinition'
|
|
101
102
|
}
|
|
102
103
|
|
|
103
|
-
_generateEntityType(def) {
|
|
104
|
-
let name = def.getIn(['name', 'value'])
|
|
104
|
+
_generateEntityType(def: immutable.Map<any, any>) {
|
|
105
|
+
let name = def.getIn(['name', 'value']) as string
|
|
105
106
|
let klass = tsCodegen.klass(name, { export: true, extends: 'Entity' })
|
|
106
107
|
const fields = def.get('fields')
|
|
107
108
|
const idField = IdField.fromFields(fields)
|
|
@@ -116,15 +117,16 @@ export default class SchemaCodeGenerator {
|
|
|
116
117
|
def
|
|
117
118
|
.get('fields')
|
|
118
119
|
.reduce(
|
|
119
|
-
(methods, field) =>
|
|
120
|
-
|
|
120
|
+
(methods: any, field: any) =>
|
|
121
|
+
methods.concat(this._generateEntityFieldMethods(def, field)),
|
|
122
|
+
immutable.List(),
|
|
121
123
|
)
|
|
122
|
-
.forEach(method => klass.addMethod(method))
|
|
124
|
+
.forEach((method: any) => klass.addMethod(method))
|
|
123
125
|
|
|
124
126
|
return klass
|
|
125
127
|
}
|
|
126
128
|
|
|
127
|
-
_generateConstructor(
|
|
129
|
+
_generateConstructor(_entityName: string, fields: immutable.List<any>) {
|
|
128
130
|
const idField = IdField.fromFields(fields)
|
|
129
131
|
return tsCodegen.method(
|
|
130
132
|
'constructor',
|
|
@@ -137,8 +139,8 @@ export default class SchemaCodeGenerator {
|
|
|
137
139
|
)
|
|
138
140
|
}
|
|
139
141
|
|
|
140
|
-
_generateStoreMethods(entityName, idField) {
|
|
141
|
-
return List.of(
|
|
142
|
+
_generateStoreMethods(entityName: any, idField: any) {
|
|
143
|
+
return immutable.List.of<tsCodegen.Method | tsCodegen.StaticMethod>(
|
|
142
144
|
tsCodegen.method(
|
|
143
145
|
'save',
|
|
144
146
|
[],
|
|
@@ -165,14 +167,14 @@ export default class SchemaCodeGenerator {
|
|
|
165
167
|
)
|
|
166
168
|
}
|
|
167
169
|
|
|
168
|
-
_generateEntityFieldMethods(entityDef, fieldDef) {
|
|
169
|
-
return List([
|
|
170
|
+
_generateEntityFieldMethods(entityDef: any, fieldDef: immutable.Map<any, any>) {
|
|
171
|
+
return immutable.List([
|
|
170
172
|
this._generateEntityFieldGetter(entityDef, fieldDef),
|
|
171
173
|
this._generateEntityFieldSetter(entityDef, fieldDef),
|
|
172
174
|
])
|
|
173
175
|
}
|
|
174
176
|
|
|
175
|
-
_generateEntityFieldGetter(
|
|
177
|
+
_generateEntityFieldGetter(_entityDef: any, fieldDef: immutable.Map<any, any>) {
|
|
176
178
|
let name = fieldDef.getIn(['name', 'value'])
|
|
177
179
|
let gqlType = fieldDef.get('type')
|
|
178
180
|
let fieldValueType = this._valueTypeFromGraphQl(gqlType)
|
|
@@ -197,7 +199,7 @@ export default class SchemaCodeGenerator {
|
|
|
197
199
|
)
|
|
198
200
|
}
|
|
199
201
|
|
|
200
|
-
_generateEntityFieldSetter(
|
|
202
|
+
_generateEntityFieldSetter(_entityDef: any, fieldDef: immutable.Map<any, any>) {
|
|
201
203
|
let name = fieldDef.getIn(['name', 'value'])
|
|
202
204
|
let gqlType = fieldDef.get('type')
|
|
203
205
|
let fieldValueType = this._valueTypeFromGraphQl(gqlType)
|
|
@@ -237,7 +239,7 @@ Suggestion: add an '!' to the member type of the List, change from '[${baseType}
|
|
|
237
239
|
)
|
|
238
240
|
}
|
|
239
241
|
|
|
240
|
-
_resolveFieldType(gqlType) {
|
|
242
|
+
_resolveFieldType(gqlType: immutable.Map<any, any>) {
|
|
241
243
|
let typeName = gqlType.getIn(['name', 'value'])
|
|
242
244
|
|
|
243
245
|
// If this is a reference to another type, the field has the type of
|
|
@@ -245,14 +247,14 @@ Suggestion: add an '!' to the member type of the List, change from '[${baseType}
|
|
|
245
247
|
const typeDef = this.schema.ast
|
|
246
248
|
.get('definitions')
|
|
247
249
|
.find(
|
|
248
|
-
def =>
|
|
250
|
+
(def: any) =>
|
|
249
251
|
(this._isEntityTypeDefinition(def) || this._isInterfaceDefinition(def)) &&
|
|
250
252
|
def.getIn(['name', 'value']) === typeName,
|
|
251
253
|
)
|
|
252
254
|
if (typeDef) {
|
|
253
255
|
return IdField.fromTypeDef(typeDef).typeName()
|
|
254
256
|
} else {
|
|
255
|
-
return typeName
|
|
257
|
+
return typeName as string
|
|
256
258
|
}
|
|
257
259
|
}
|
|
258
260
|
|
|
@@ -260,9 +262,9 @@ Suggestion: add an '!' to the member type of the List, change from '[${baseType}
|
|
|
260
262
|
* types, that's the type from the subgraph schema. For references to
|
|
261
263
|
* other entity types, this is the same as the type of the id of the
|
|
262
264
|
* referred type, i.e., `string` or `Bytes`*/
|
|
263
|
-
_valueTypeFromGraphQl(gqlType) {
|
|
265
|
+
_valueTypeFromGraphQl(gqlType: immutable.Map<any, any>): any {
|
|
264
266
|
if (gqlType.get('kind') === 'NonNullType') {
|
|
265
|
-
return this._valueTypeFromGraphQl(gqlType.get('type')
|
|
267
|
+
return this._valueTypeFromGraphQl(gqlType.get('type'))
|
|
266
268
|
} else if (gqlType.get('kind') === 'ListType') {
|
|
267
269
|
return '[' + this._valueTypeFromGraphQl(gqlType.get('type')) + ']'
|
|
268
270
|
} else {
|
|
@@ -272,7 +274,7 @@ Suggestion: add an '!' to the member type of the List, change from '[${baseType}
|
|
|
272
274
|
|
|
273
275
|
/** Determine the base type of `gqlType` by removing any non-null
|
|
274
276
|
* constraints and using the type of elements of lists */
|
|
275
|
-
_baseType(gqlType) {
|
|
277
|
+
_baseType(gqlType: immutable.Map<any, any>): any {
|
|
276
278
|
if (gqlType.get('kind') === 'NonNullType') {
|
|
277
279
|
return this._baseType(gqlType.get('type'))
|
|
278
280
|
} else if (gqlType.get('kind') === 'ListType') {
|
|
@@ -282,7 +284,7 @@ Suggestion: add an '!' to the member type of the List, change from '[${baseType}
|
|
|
282
284
|
}
|
|
283
285
|
}
|
|
284
286
|
|
|
285
|
-
_typeFromGraphQl(gqlType, nullable = true) {
|
|
287
|
+
_typeFromGraphQl(gqlType: immutable.Map<any, any>, nullable = true): any {
|
|
286
288
|
if (gqlType.get('kind') === 'NonNullType') {
|
|
287
289
|
return this._typeFromGraphQl(gqlType.get('type'), false)
|
|
288
290
|
} else if (gqlType.get('kind') === 'ListType') {
|
|
@@ -291,7 +293,7 @@ Suggestion: add an '!' to the member type of the List, change from '[${baseType}
|
|
|
291
293
|
} else {
|
|
292
294
|
// NamedType
|
|
293
295
|
let type = tsCodegen.namedType(
|
|
294
|
-
typesCodegen.ascTypeForValue(this._resolveFieldType(gqlType)),
|
|
296
|
+
typesCodegen.ascTypeForValue(this._resolveFieldType(gqlType)) as any,
|
|
295
297
|
)
|
|
296
298
|
// In AssemblyScript, primitives cannot be nullable.
|
|
297
299
|
return nullable && !type.isPrimitive() ? tsCodegen.nullableType(type) : type
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
class Param {
|
|
2
|
-
constructor(public name: string, public type:
|
|
2
|
+
constructor(public name: string, public type: NamedType | NullableType) {
|
|
3
3
|
this.name = name
|
|
4
4
|
this.type = type
|
|
5
5
|
}
|
|
@@ -12,8 +12,8 @@ class Param {
|
|
|
12
12
|
class Method {
|
|
13
13
|
constructor(
|
|
14
14
|
public name: string,
|
|
15
|
-
public params:
|
|
16
|
-
public returnType:
|
|
15
|
+
public params: Param[],
|
|
16
|
+
public returnType: NamedType | undefined,
|
|
17
17
|
public body: string,
|
|
18
18
|
) {
|
|
19
19
|
this.name = name
|
|
@@ -35,8 +35,8 @@ class Method {
|
|
|
35
35
|
class StaticMethod {
|
|
36
36
|
constructor(
|
|
37
37
|
public name: string,
|
|
38
|
-
public params:
|
|
39
|
-
public returnType:
|
|
38
|
+
public params: Param[],
|
|
39
|
+
public returnType: NamedType | NullableType,
|
|
40
40
|
public body: string,
|
|
41
41
|
) {
|
|
42
42
|
this.name = name
|
|
@@ -142,7 +142,7 @@ class NamedType {
|
|
|
142
142
|
class ArrayType {
|
|
143
143
|
public name: string
|
|
144
144
|
|
|
145
|
-
constructor(public inner:
|
|
145
|
+
constructor(public inner: NamedType) {
|
|
146
146
|
this.inner = inner
|
|
147
147
|
this.name = `Array<${inner.toString()}>`
|
|
148
148
|
}
|
|
@@ -153,7 +153,7 @@ class ArrayType {
|
|
|
153
153
|
}
|
|
154
154
|
|
|
155
155
|
class NullableType {
|
|
156
|
-
constructor(public inner:
|
|
156
|
+
constructor(public inner: NamedType | ArrayType) {
|
|
157
157
|
this.inner = inner
|
|
158
158
|
}
|
|
159
159
|
|
|
@@ -176,15 +176,23 @@ class ModuleImports {
|
|
|
176
176
|
}
|
|
177
177
|
|
|
178
178
|
const namedType = (name: string) => new NamedType(name)
|
|
179
|
-
const arrayType = (name:
|
|
180
|
-
const param = (name: string, type:
|
|
181
|
-
const method = (
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
179
|
+
const arrayType = (name: NamedType) => new ArrayType(name)
|
|
180
|
+
const param = (name: string, type: NamedType) => new Param(name, type)
|
|
181
|
+
const method = (
|
|
182
|
+
name: string,
|
|
183
|
+
params: Param[],
|
|
184
|
+
returnType: NamedType | undefined,
|
|
185
|
+
body: string,
|
|
186
|
+
) => new Method(name, params, returnType, body)
|
|
187
|
+
const staticMethod = (
|
|
188
|
+
name: string,
|
|
189
|
+
params: Param[],
|
|
190
|
+
returnType: NamedType | NullableType,
|
|
191
|
+
body: string,
|
|
192
|
+
) => new StaticMethod(name, params, returnType, body)
|
|
185
193
|
const klass = (name: string, options: ClassOptions) => new Class(name, options)
|
|
186
194
|
const klassMember = (name: string, type: string) => new ClassMember(name, type)
|
|
187
|
-
const nullableType = (type:
|
|
195
|
+
const nullableType = (type: NamedType | ArrayType) => new NullableType(type)
|
|
188
196
|
const moduleImports = (nameOrNames: string | string[], module: string) =>
|
|
189
197
|
new ModuleImports(nameOrNames, module)
|
|
190
198
|
|
|
@@ -6,7 +6,7 @@ describe('Codegen utilities', () => {
|
|
|
6
6
|
disambiguateNames({
|
|
7
7
|
values: ['a', 'b', 'c'],
|
|
8
8
|
getName: x => x,
|
|
9
|
-
setName: (
|
|
9
|
+
setName: (_x, name) => name,
|
|
10
10
|
}),
|
|
11
11
|
).toEqual(['a', 'b', 'c'])
|
|
12
12
|
|
|
@@ -14,7 +14,7 @@ describe('Codegen utilities', () => {
|
|
|
14
14
|
disambiguateNames({
|
|
15
15
|
values: ['a', 'a', 'a'],
|
|
16
16
|
getName: x => x,
|
|
17
|
-
setName: (
|
|
17
|
+
setName: (_x, name) => name,
|
|
18
18
|
}),
|
|
19
19
|
).toEqual(['a', 'a1', 'a2'])
|
|
20
20
|
|
|
@@ -1,4 +1,12 @@
|
|
|
1
|
-
|
|
1
|
+
export function disambiguateNames<T>({
|
|
2
|
+
values,
|
|
3
|
+
getName,
|
|
4
|
+
setName,
|
|
5
|
+
}: {
|
|
6
|
+
values: T[]
|
|
7
|
+
getName: (value: T, index: number) => string
|
|
8
|
+
setName: (value: T, name: string) => void
|
|
9
|
+
}) {
|
|
2
10
|
let collisionCounter = new Map()
|
|
3
11
|
return values.map((value, index) => {
|
|
4
12
|
let name = getName(value, index)
|
|
@@ -13,41 +21,35 @@ const disambiguateNames = ({ values, getName, setName }) => {
|
|
|
13
21
|
})
|
|
14
22
|
}
|
|
15
23
|
|
|
16
|
-
|
|
24
|
+
export function isTupleType(t: string) {
|
|
17
25
|
return t === 'tuple'
|
|
18
26
|
}
|
|
19
27
|
|
|
20
|
-
|
|
28
|
+
export function containsTupleType(t: string) {
|
|
21
29
|
return isTupleType(t) || isTupleArrayType(t) || isTupleMatrixType(t)
|
|
22
30
|
}
|
|
23
31
|
|
|
24
|
-
|
|
32
|
+
export function isTupleArrayType(t: string) {
|
|
25
33
|
return t.match(/^tuple\[([0-9]+)?\]$/)
|
|
26
34
|
}
|
|
27
35
|
|
|
28
|
-
|
|
36
|
+
export function isTupleMatrixType(t: string) {
|
|
29
37
|
return t.match(/^tuple\[([0-9]+)?\]\[([0-9]+)?\]$/)
|
|
30
38
|
}
|
|
31
39
|
|
|
32
|
-
const unrollTuple = ({
|
|
33
|
-
|
|
40
|
+
export const unrollTuple = ({
|
|
41
|
+
path,
|
|
42
|
+
value,
|
|
43
|
+
}: {
|
|
44
|
+
path: string[]
|
|
45
|
+
index: number // TODO: index is unused, do we really need it?
|
|
46
|
+
value: any
|
|
47
|
+
}) =>
|
|
48
|
+
value.components.reduce((acc: any[], component: any, index: number) => {
|
|
34
49
|
let name = component.name || `value${index}`
|
|
35
50
|
return acc.concat(
|
|
36
51
|
component.type === 'tuple'
|
|
37
|
-
? unrollTuple({
|
|
38
|
-
path: [...path, name],
|
|
39
|
-
index,
|
|
40
|
-
value: component,
|
|
41
|
-
})
|
|
52
|
+
? unrollTuple({ path: [...path, name], index, value: component })
|
|
42
53
|
: [{ path: [...path, name], type: component.type }],
|
|
43
54
|
)
|
|
44
55
|
}, [])
|
|
45
|
-
|
|
46
|
-
export {
|
|
47
|
-
containsTupleType,
|
|
48
|
-
disambiguateNames,
|
|
49
|
-
isTupleType,
|
|
50
|
-
isTupleArrayType,
|
|
51
|
-
isTupleMatrixType,
|
|
52
|
-
unrollTuple,
|
|
53
|
-
}
|
package/src/scaffold/index.ts
CHANGED
|
@@ -98,7 +98,7 @@ dataSources:
|
|
|
98
98
|
|
|
99
99
|
generateSchema() {
|
|
100
100
|
const hasEvents = this.protocol.hasEvents()
|
|
101
|
-
const events = hasEvents ? abiEvents(this.abi).toJS() : []
|
|
101
|
+
const events = hasEvents ? abiEvents(this.abi!).toJS() : []
|
|
102
102
|
|
|
103
103
|
return prettier.format(
|
|
104
104
|
hasEvents && this.indexEvents
|
|
@@ -130,7 +130,7 @@ dataSources:
|
|
|
130
130
|
|
|
131
131
|
generateMapping() {
|
|
132
132
|
const hasEvents = this.protocol.hasEvents()
|
|
133
|
-
const events = hasEvents ? abiEvents(this.abi).toJS() : []
|
|
133
|
+
const events = hasEvents ? abiEvents(this.abi!).toJS() : []
|
|
134
134
|
const protocolMapping = this.protocol.getMappingScaffold()
|
|
135
135
|
|
|
136
136
|
return prettier.format(
|
|
@@ -156,7 +156,7 @@ dataSources:
|
|
|
156
156
|
|
|
157
157
|
generateTests() {
|
|
158
158
|
const hasEvents = this.protocol.hasEvents()
|
|
159
|
-
const events = hasEvents ? abiEvents(this.abi).toJS() : []
|
|
159
|
+
const events = hasEvents ? abiEvents(this.abi!).toJS() : []
|
|
160
160
|
|
|
161
161
|
return events.length > 0
|
|
162
162
|
? generateTestsFiles(this.contractName, events, this.indexEvents)
|
package/src/scaffold/schema.ts
CHANGED
|
@@ -1,13 +1,18 @@
|
|
|
1
|
+
import immutable from 'immutable'
|
|
1
2
|
import { ascTypeForProtocol, valueTypeForAsc } from '../codegen/types'
|
|
2
3
|
import * as util from '../codegen/util'
|
|
3
4
|
import Protocol from '../protocols'
|
|
4
5
|
|
|
5
|
-
export
|
|
6
|
-
util.disambiguateNames({
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
6
|
+
export function abiEvents(abi: { data: immutable.Map<any, any> }) {
|
|
7
|
+
return (util.disambiguateNames({
|
|
8
|
+
// @ts-expect-error improve typings of disambiguateNames to handle iterables
|
|
9
|
+
values: abi.data.filter(item => item.get('type') === 'event'),
|
|
10
|
+
// @ts-expect-error improve typings of disambiguateNames to handle iterables
|
|
11
|
+
getName: event => event.get('name'),
|
|
12
|
+
// @ts-expect-error improve typings of disambiguateNames to handle iterables
|
|
13
|
+
setName: (event, name) => event.set('_alias', name),
|
|
14
|
+
}) as unknown) as immutable.List<any>
|
|
15
|
+
}
|
|
11
16
|
|
|
12
17
|
export const protocolTypeToGraphQL = (protocol: string, name: string) => {
|
|
13
18
|
let ascType = ascTypeForProtocol(protocol, name)
|
package/src/schema.ts
CHANGED