@graphprotocol/graph-cli 0.26.0 → 0.26.1

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.
Files changed (24) hide show
  1. package/examples/basic-event-handlers/node_modules/@graphprotocol/graph-ts/chain/ethereum.ts +1 -1
  2. package/examples/basic-event-handlers/node_modules/@graphprotocol/graph-ts/chain/tendermint.ts +554 -0
  3. package/examples/basic-event-handlers/node_modules/@graphprotocol/graph-ts/common/collections.ts +52 -2
  4. package/examples/basic-event-handlers/node_modules/@graphprotocol/graph-ts/common/numbers.ts +11 -0
  5. package/examples/basic-event-handlers/node_modules/@graphprotocol/graph-ts/common/value.ts +47 -0
  6. package/examples/basic-event-handlers/node_modules/@graphprotocol/graph-ts/global/global.ts +150 -1
  7. package/examples/basic-event-handlers/node_modules/@graphprotocol/graph-ts/index.ts +2 -0
  8. package/examples/basic-event-handlers/node_modules/@graphprotocol/graph-ts/package.json +2 -1
  9. package/examples/basic-event-handlers/node_modules/@graphprotocol/graph-ts/test/test.js +2 -0
  10. package/examples/basic-event-handlers/node_modules/keccak/build/Makefile +1 -1
  11. package/examples/basic-event-handlers/node_modules/keccak/build/Release/.deps/Release/obj.target/keccak/src/addon.o.d.raw +16 -16
  12. package/examples/basic-event-handlers/node_modules/keccak/build/config.gypi +4 -4
  13. package/examples/basic-event-handlers/node_modules/keccak/build/keccak.target.mk +14 -14
  14. package/examples/basic-event-handlers/node_modules/websocket/build/Makefile +1 -1
  15. package/examples/basic-event-handlers/node_modules/websocket/build/Release/.deps/Release/obj.target/bufferutil/src/bufferutil.o.d.raw +16 -16
  16. package/examples/basic-event-handlers/node_modules/websocket/build/bufferutil.target.mk +14 -14
  17. package/examples/basic-event-handlers/node_modules/websocket/build/config.gypi +1 -1
  18. package/examples/basic-event-handlers/node_modules/websocket/build/validation.target.mk +14 -14
  19. package/package.json +1 -1
  20. package/src/codegen/schema.js +5 -46
  21. package/src/codegen/schema.test.js +0 -6
  22. package/src/codegen/types/conversions.js +19 -19
  23. package/src/codegen/types/index.js +0 -25
  24. package/src/codegen/types/index.test.js +0 -22
@@ -54,7 +54,7 @@ module.exports = class SchemaCodeGenerator {
54
54
  let klass = tsCodegen.klass(name, { export: true, extends: 'Entity' })
55
55
 
56
56
  // Generate and add a constructor
57
- klass.addMethod(this._generateConstructor(name, def.get('fields')))
57
+ klass.addMethod(this._generateConstructor(name))
58
58
 
59
59
  // Generate and add save() and getById() methods
60
60
  this._generateStoreMethods(name).forEach(method => klass.addMethod(method))
@@ -71,42 +71,7 @@ module.exports = class SchemaCodeGenerator {
71
71
  return klass
72
72
  }
73
73
 
74
- // Fields that are non-nullable get an empty/zero value set in the class constructor.
75
- _generateDefaultFieldValues(fields) {
76
- const indexOfIdField = fields.findIndex(field => field.getIn(['name', 'value']) === 'id')
77
- const fieldsWithoutId = fields.remove(indexOfIdField)
78
-
79
- const fieldsSetCalls = fieldsWithoutId
80
- .map(field => {
81
- const name = field.getIn(['name', 'value'])
82
- const type = this._typeFromGraphQl(field.get('type'), true, true)
83
-
84
- const isNullable = type instanceof tsCodegen.NullableType
85
-
86
- const directives = field.get('directives')
87
- const isDerivedFrom = directives.some(directive => directive.getIn(['name', 'value']) === 'derivedFrom')
88
-
89
- return { name, type, isNullable, isDerivedFrom }
90
- })
91
- // We only call the setter with the default value in the constructor for fields that are:
92
- // - Not nullable, so that AS doesn't break when subgraph developers try to access them before a `set`
93
- // - It doesn't matter if it's primitive or not
94
- // - Not tagged as `derivedFrom`, because they only exist in query time
95
- .filter(({
96
- isNullable,
97
- isDerivedFrom,
98
- }) => !isNullable && !isDerivedFrom)
99
- .map(({ name, type, isNullable }) => {
100
- const fieldTypeString = isNullable ? type.inner.toString() : type.toString()
101
-
102
- return `
103
- this.set('${name}', ${typesCodegen.initializedValueFromAsc(fieldTypeString)})`
104
- })
105
-
106
- return fieldsSetCalls.join('')
107
- }
108
-
109
- _generateConstructor(entityName, fields) {
74
+ _generateConstructor(entityName) {
110
75
  return tsCodegen.method(
111
76
  'constructor',
112
77
  [tsCodegen.param('id', tsCodegen.namedType('string'))],
@@ -114,7 +79,6 @@ module.exports = class SchemaCodeGenerator {
114
79
  `
115
80
  super()
116
81
  this.set('id', Value.fromString(id))
117
- ${this._generateDefaultFieldValues(fields)}
118
82
  `,
119
83
  )
120
84
  }
@@ -234,7 +198,7 @@ Suggestion: add an '!' to the member type of the List, change from '${fieldValue
234
198
  : gqlType.getIn(['name', 'value'])
235
199
  }
236
200
 
237
- _typeFromGraphQl(gqlType, nullable = true, nullablePrimitive = false) {
201
+ _typeFromGraphQl(gqlType, nullable = true) {
238
202
  if (gqlType.get('kind') === 'NonNullType') {
239
203
  return this._typeFromGraphQl(gqlType.get('type'), false)
240
204
  } else if (gqlType.get('kind') === 'ListType') {
@@ -245,13 +209,8 @@ Suggestion: add an '!' to the member type of the List, change from '${fieldValue
245
209
  let type = tsCodegen.namedType(
246
210
  typesCodegen.ascTypeForValue(gqlType.getIn(['name', 'value'])),
247
211
  )
248
-
249
- // Will not wrap primitives into NullableType by default.
250
- if (!nullablePrimitive && type.isPrimitive()) {
251
- return type
252
- }
253
-
254
- return nullable ? tsCodegen.nullableType(type) : type
212
+ // In AssemblyScript, primitives cannot be nullable.
213
+ return nullable && !type.isPrimitive() ? tsCodegen.nullableType(type) : type
255
214
  }
256
215
  }
257
216
  }
@@ -114,9 +114,6 @@ describe('Schema code generator', () => {
114
114
  body: `
115
115
  super()
116
116
  this.set('id', Value.fromString(id))
117
-
118
- this.set('name', Value.fromString(''))
119
- this.set('count', Value.fromI32(0))
120
117
  `,
121
118
  },
122
119
  {
@@ -279,9 +276,6 @@ describe('Schema code generator', () => {
279
276
  body: `
280
277
  super()
281
278
  this.set('id', Value.fromString(id))
282
-
283
- this.set('amount', Value.fromBigInt(BigInt.zero()))
284
- this.set('account', Value.fromString(''))
285
279
  `,
286
280
  },
287
281
  {
@@ -188,28 +188,28 @@ const VALUE_TO_ASSEMBLYSCRIPT = [
188
188
  const ASSEMBLYSCRIPT_TO_VALUE = [
189
189
  // Arrays
190
190
 
191
- ['Array<Address>', '[Bytes]', code => `Value.fromBytesArray(${code})`, 'new Array(0)'],
192
- ['Array<Bytes>', '[Bytes]', code => `Value.fromBytesArray(${code})`, 'new Array(0)'],
193
- ['Array<boolean>', '[Boolean]', code => `Value.fromBooleanArray(${code})`, 'new Array(0)'],
194
- ['Array<i32>', '[Int]', code => `Value.fromI32Array(${code})`, 'new Array(0)'],
195
- ['Array<BigInt>', '[BigInt]', code => `Value.fromBigIntArray(${code})`, 'new Array(0)'],
196
- ['Array<string>', '[String]', code => `Value.fromStringArray(${code})`, 'new Array(0)'],
197
- ['Array<string>', '[ID]', code => `Value.fromStringArray(${code})`, 'new Array(0)'],
198
- ['Array<BigDecimal>', '[BigDecimal]', code => `Value.fromBigDecimalArray(${code})`, 'new Array(0)'],
199
- ['Array<string>', /\[.*\]/, code => `Value.fromStringArray(${code})`, 'new Array(0)'],
200
- ['Array<string | null>', null, code => `Value.fromStringArray(${code})`, 'new Array(0)'],
191
+ ['Array<Address>', '[Bytes]', code => `Value.fromBytesArray(${code})`],
192
+ ['Array<Bytes>', '[Bytes]', code => `Value.fromBytesArray(${code})`],
193
+ ['Array<boolean>', '[Boolean]', code => `Value.fromBooleanArray(${code})`],
194
+ ['Array<i32>', '[Int]', code => `Value.fromI32Array(${code})`],
195
+ ['Array<BigInt>', '[BigInt]', code => `Value.fromBigIntArray(${code})`],
196
+ ['Array<string>', '[String]', code => `Value.fromStringArray(${code})`],
197
+ ['Array<string>', '[ID]', code => `Value.fromStringArray(${code})`],
198
+ ['Array<BigDecimal>', '[BigDecimal]', code => `Value.fromBigDecimalArray(${code})`],
199
+ ['Array<string>', /\[.*\]/, code => `Value.fromStringArray(${code})`],
200
+ ['Array<string | null>', null, code => `Value.fromStringArray(${code})`],
201
201
 
202
202
  // Scalar values
203
203
 
204
- ['Address', 'Bytes', code => `Value.fromBytes(${code})`, 'Address.zero()'],
205
- ['Bytes', 'Bytes', code => `Value.fromBytes(${code})`, 'Bytes.empty()'],
206
- ['boolean', 'Boolean', code => `Value.fromBoolean(${code})`, 'false'],
207
- ['i32', 'Int', code => `Value.fromI32(${code})`, '0'],
208
- ['BigInt', 'BigInt', code => `Value.fromBigInt(${code})`, 'BigInt.zero()'],
209
- ['string', 'String', code => `Value.fromString(${code})`, "''"],
210
- ['string', 'ID', code => `Value.fromString(${code})`, "''"],
211
- ['BigDecimal', 'BigDecimal', code => `Value.fromBigDecimal(${code})`, 'BigDecimal.zero()'],
212
- ['string', /.*/, code => `Value.fromString(${code})`, "''"],
204
+ ['Address', 'Bytes', code => `Value.fromBytes(${code})`],
205
+ ['Bytes', 'Bytes', code => `Value.fromBytes(${code})`],
206
+ ['boolean', 'Boolean', code => `Value.fromBoolean(${code})`],
207
+ ['i32', 'Int', code => `Value.fromI32(${code})`],
208
+ ['BigInt', 'BigInt', code => `Value.fromBigInt(${code})`],
209
+ ['string', 'String', code => `Value.fromString(${code})`],
210
+ ['string', 'ID', code => `Value.fromString(${code})`],
211
+ ['BigDecimal', 'BigDecimal', code => `Value.fromBigDecimal(${code})`],
212
+ ['string', /.*/, code => `Value.fromString(${code})`],
213
213
  ]
214
214
 
215
215
  /**
@@ -66,27 +66,6 @@ const findConversionToType = (fromTypeSystem, toTypeSystem, toType) => {
66
66
  return objectifyConversion(fromTypeSystem, toTypeSystem, conversion)
67
67
  }
68
68
 
69
- const findInitializationForType = (fromTypeSystem, toTypeSystem, ascType) => {
70
- const conversions = conversionsForTypeSystems(fromTypeSystem, toTypeSystem)
71
-
72
- const conversion = conversions.find(conversion =>
73
- typeof conversion.get(0) === 'string'
74
- ? conversion.get(0) === ascType
75
- : ascType.match(conversion.get(0)),
76
- )
77
-
78
- if (conversion === undefined) {
79
- throw new Error(
80
- `Conversion from '${fromTypeSystem}' to '${toTypeSystem}' for ` +
81
- `target type '${ascType}' is not supported`,
82
- )
83
- }
84
-
85
- const conversionObj = objectifyConversion(fromTypeSystem, toTypeSystem, conversion)
86
-
87
- return conversionObj.get('convert')(conversion.get(3))
88
- }
89
-
90
69
  // High-level type system API
91
70
 
92
71
  const ascTypeForProtocol = (protocol, protocolType) =>
@@ -120,9 +99,6 @@ const valueToAsc = (code, valueType) =>
120
99
  const valueFromAsc = (code, valueType) =>
121
100
  findConversionToType('AssemblyScript', 'Value', valueType).get('convert')(code)
122
101
 
123
- const initializedValueFromAsc = ascType =>
124
- findInitializationForType('AssemblyScript', 'Value', ascType)
125
-
126
102
  module.exports = {
127
103
  // protocol <-> AssemblyScript
128
104
  ascTypeForProtocol,
@@ -138,5 +114,4 @@ module.exports = {
138
114
  valueTypeForAsc,
139
115
  valueToAsc,
140
116
  valueFromAsc,
141
- initializedValueFromAsc,
142
117
  }
@@ -449,26 +449,4 @@ describe('AssemblyScript -> Value', () => {
449
449
  expect(codegen.valueFromAsc('x', '[String]')).toBe('Value.fromStringArray(x)')
450
450
  expect(codegen.valueTypeForAsc('Array<string>')).toBe('[String]')
451
451
  })
452
-
453
- describe('Initialization for zero/empty values', () => {
454
- test('Array<string>', () => {
455
- expect(codegen.initializedValueFromAsc('Array<string>')).toBe('Value.fromStringArray(new Array(0))')
456
- })
457
-
458
- test('Array<string | null>', () => {
459
- expect(codegen.initializedValueFromAsc('Array<string | null>')).toBe('Value.fromStringArray(new Array(0))')
460
- })
461
-
462
- test('i32', () => {
463
- expect(codegen.initializedValueFromAsc('i32')).toBe('Value.fromI32(0)')
464
- })
465
-
466
- test('string', () => {
467
- expect(codegen.initializedValueFromAsc('string')).toBe("Value.fromString('')")
468
- })
469
-
470
- test('BigInt', () => {
471
- expect(codegen.initializedValueFromAsc('BigInt')).toBe('Value.fromBigInt(BigInt.zero())')
472
- })
473
- })
474
452
  })