AssemblyScript Migration Guide
Up until now, subgraphs have been using one of the first versions of AssemblyScript (v0.6). Finally we've added support for the newest one available (v0.19.10)! 🎉
That will enable subgraph developers to use newer features of the AS language and standard library.
This guide is applicable for anyone using graph-cli
/graph-ts
below version 0.22.0
. If you're already at a higher than (or equal) version to that, you've already been using version 0.19.10
of AssemblyScript 🙂
Note: As of
0.24.0
,graph-node
can support both versions, depending on theapiVersion
specified in the subgraph manifest.
Features
New functionality
TypedArray
s can now be built fromArrayBuffer
s by using the newwrap
static method (v0.8.1)New standard library functions:
String#toUpperCase
,String#toLowerCase
,String#localeCompare
andTypedArray#set
(v0.9.0)Added support for x instanceof GenericClass (v0.9.2)
Added
StaticArray<T>
, a more efficient array variant (v0.9.3)Added
Array<T>#flat
(v0.10.0)Implemented
radix
argument onNumber#toString
(v0.10.1)Added support for separators in floating point literals (v0.13.7)
Added support for first class functions (v0.14.0)
Add builtins:
i32/i64/f32/f64.add/sub/mul
(v0.14.13)Implement
Array/TypedArray/String#at
(v0.18.2)Added support for template literal strings (v0.18.17)
Add
encodeURI(Component)
anddecodeURI(Component)
(v0.18.27)Add
toString
,toDateString
andtoTimeString
toDate
(v0.18.29)Add
toUTCString
forDate
(v0.18.30)Add
nonnull/NonNullable
builtin type (v0.19.2)
Optimizations
Math
functions such asexp
,exp2
,log
,log2
andpow
have been replaced by faster variants (v0.9.0)Slightly optimize
Math.mod
(v0.17.1)Cache more field accesses in std Map and Set (v0.17.8)
Optimize for powers of two in
ipow32/64
(v0.18.2)
Other
The type of an array literal can now be inferred from its contents (v0.9.0)
Updated stdlib to Unicode 13.0.0 (v0.10.0)
How to upgrade?
Change your mappings
apiVersion
insubgraph.yaml
to0.0.6
:
Update the
graph-cli
you're using to thelatest
version by running:
Do the same for
graph-ts
, but instead of installing globally, save it in your main dependencies:
Follow the rest of the guide to fix the language breaking changes.
Run
codegen
anddeploy
again.
Breaking changes
Nullability
On the older version of AssemblyScript, you could create code like this:
However on the newer version, because the value is nullable, it requires you to check, like this:
Or force it like this:
If you are unsure which to choose, we recommend always using the safe version. If the value doesn't exist you might want to just do an early if statement with a return in you subgraph handler.
Variable Shadowing
Before you could do variable shadowing and code like this would work:
However now this isn't possible anymore, and the compiler returns this error:
You'll need to rename your duplicate variables if you had variable shadowing.
Null Comparisons
By doing the upgrade on your subgraph, sometimes you might get errors like these:
To solve you can simply change the if
statement to something like this:
The same applies if you're doing != instead of ==.
Casting
The common way to do casting before was to just use the as
keyword, like this:
However this only works in two scenarios:
Primitive casting (between types such as
u8
,i32
,bool
; eg:let b: isize = 10; b as usize
);Upcasting on class inheritance (subclass → superclass)
Examples:
There are two scenarios where you may want to cast, but using as
/<T>var
isn't safe:
Downcasting on class inheritance (superclass → subclass)
Between two types that share a superclass
For those cases, you can use the changetype<T>
function:
If you just want to remove nullability, you can keep using the as
operator (or <T>variable
), but make sure you know that value can't be null, otherwise it will break.
For the nullability case we recommend taking a look at the nullability check feature, it will make your code cleaner 🙂
Also we've added a few more static methods in some types to ease casting, they are:
Bytes.fromByteArray
Bytes.fromUint8Array
BigInt.fromByteArray
ByteArray.fromBigInt
Nullability check with property access
To use the nullability check feature you can use either if
statements or the ternary operator (?
and :
) like this:
However that only works when you're doing the if
/ ternary on a variable, not on a property access, like this:
Which outputs this error:
To fix this issue, you can create a variable for that property access so that the compiler can do the nullability check magic:
Operator overloading with property access
If you try to sum (for example) a nullable type (from a property access) with a non nullable one, the AssemblyScript compiler instead of giving a compile time error warning that one of the values is nullable, it just compiles silently, giving chance for the code to break at runtime.
We've opened a issue on the AssemblyScript compiler for this, but for now if you do these kind of operations in your subgraph mappings, you should change them to do a null check before it.
Value initialization
If you have any code like this:
It will compile but break at runtime, that happens because the value hasn't been initialized, so make sure your subgraph has initialized their values, like this:
Also if you have nullable properties in a GraphQL entity, like this:
And you have code similar to this:
You'll need to make sure to initialize the total.amount
value, because if you try to access like in the last line for the sum, it will crash. So you either initialize it first:
Or you can just change your GraphQL schema to not use a nullable type for this property, then we'll initialize it as zero on the codegen
step 😉
Class property initialization
If you export any classes with properties that are other classes (declared by you or by the standard library) like this:
The compiler will error because you either need to add an initializer for the properties that are classes, or add the !
operator:
Array initialization
The Array
class still accepts a number to initialize the length of the list, however you should take care because operations like .push
will actually increase the size instead of adding to the beginning, for example:
Depending on the types you're using, eg nullable ones, and how you're accessing them, you might encounter a runtime error like this one:
To actually push at the beginning you should either, initialize the Array
with size zero, like this:
Or you should mutate it via index:
GraphQL schema
This is not a direct AssemblyScript change, but you may have to update your schema.graphql
file.
Now you no longer can define fields in your types that are Non-Nullable Lists. If you have a schema like this:
You'll have to add an !
to the member of the List type, like this:
This changed because of nullability differences between AssemblyScript versions, and it's related to the src/generated/schema.ts
file (default path, you might have changed this).
Other
Aligned
Map#set
andSet#add
with the spec, returningthis
(v0.9.2)Arrays no longer inherit from ArrayBufferView, but are now distinct (v0.10.0)
Classes initialized from object literals can no longer define a constructor (v0.10.0)
The result of a
**
binary operation is now the common denominator integer if both operands are integers. Previously, the result was a float as if callingMath/f.pow
(v0.11.0)Coerce
NaN
tofalse
when casting tobool
(v0.14.9)When shifting a small integer value of type
i8
/u8
ori16
/u16
, only the 3 respectively 4 least significant bits of the RHS value affect the result, analogous to the result of ani32.shl
only being affected by the 5 least significant bits of the RHS value. Example:someI8 << 8
previously produced the value0
, but now producessomeI8
due to masking the RHS as8 & 7 = 0
(3 bits) (v0.17.0)Bug fix of relational string comparisons when sizes differ (v0.17.8)
Last updated