-
Notifications
You must be signed in to change notification settings - Fork 115
Joystream types - refactorization and upgrade #1124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
Added some scripts that allow automatically creating api.createType
And we will:
The idea is based on https://polkadot.js.org/api/examples/promise/90_typegen, but since the tool works with auto-generated interfaces instead of Codec types that we use, I temporary created a modified script. Using
or use: It would be nice if Required setupIn order to use the augmented type definitions other projects need to add: To
|
|
One last note: After current refactorization it's possible to use Also For all structs we have field getters and helper methods, like: Unless we previosly had getters that modify the original value (ie. |
| getField<FieldKey extends keyof FieldTypes>(key: FieldKey): InstanceType<FieldTypes[FieldKey]> { | ||
| return this.get(key as string) as InstanceType<FieldTypes[FieldKey]> | ||
| } | ||
| getString<FieldKey extends keyof FieldTypes>(key: FieldKey): string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest getFieldAsString as a better name
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not worth spending more time on this however now, there is more important work.
| export class CuratorApplicationIdSet extends Vec.with(CuratorApplicationId) {} | ||
|
|
||
| export const contentWorkingGroupTypes = { | ||
| ChannelId: 'u64', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems unnecessary to specify ChannelId: 'u64' and for the remaining Ids, but this is an artifact from previous version. I can't remember why I was forced to use this style however.
|
Looks good I tried the creating/updating augment-types.ts steps successfully. It would be better to have a single script that generates both polkadot and the custom types and imports that can be run at anytime without having to think about when is the right time to do it, and avoid having to manually edit the augment-types.ts. (if possible) |
|
Merging, noting that dependent packages are broken and will be fixed in coming up PRs |
Implements #1118. I created this issue recentlly, so it contains a good summary of the changes (copy-pasting it here). Another, earlier issue explaining some of the reasoning behind the approach I took can also be found here: #1036
This PR includes:
StructandEnumtypes to use ourJoyEnum/JoyStructwrappers. The reasonsing behind this is that:Registry), so it made sense to use this as an opportunity to perform further normalization.Struct.withand use one shared TypeScript interface that provides type-safe accessors for allStructsTuple.with(),Vec.with()etc. whenever possible (avoid having to specify constructors on our side for same reasons as mentioned above)@polkadot/typesRegistryin order to work@joystream/typesjust export theRegistryTypesthat we can then pass as argument when creating the api (since this is now the recommended way of initializing it) instead of registering them "gloablly" withregisterJoystreamTypesChanges required to implement in dependet projects:
registerJoystreamTypesother project should now initialize the api with:new, ie.Registry is available in the api instance (
api.registry) or any type instance (ie.memberProfile.registry).This is however not a recommended approach of creating types (see: https://polkadot.js.org/api/start/types.create.html), so I'm considering adding the right augmentation for
api.createTypeso that we can use it instead (in a relatively type-safe way). The problem withapi.createTypes()is that it probably won't allow us to force the right schema for stucts like we do currently (ie. previosly some of the struct constructors required a type-safe object with correct Codec values for all struct fields, because thevalueargument type was set to a corresponding interface). I can think of a workaround using JoyStruct to allow for something like ie.Membership.create(/* typesafe object */)which wouldn't require passing the registry (we could possibly intialize it in@joystream/typesitself), but this bypasses the recommended way again, so I'm not sure if that's the right approach..OptionText.empty()orBlockAndTime.newEmpty(), but I haven't seen any usages of those outside very few cases within Pioneer. Since those would now need the registry in order to work I think I can just replace those usages with a more normalized way of creating types (ie.api.createType, or some new helpers inJoyStruct/JoyEnumas mentioned above)This is still WIP, but it should be possible to use the library in the current form (although it's not obvious yet how we should approach the creation of types after the upgrade, ie. whether to use
api.createType, which will require some additional work, or some other, temporary approach, so I wouldn't advise refactoring other projects yet).The changes are based on #1071 since it introduced some changes in
@joystream/typesbefore. The commit that contains the actual refactorization (most related to this PR) is: c7c531d.I dind't want to make too much changes just to pass the CI, so I just updated the resolutions in root
package.jsonand temporary disabledstorage-nodepostinstall build, which allows the types checks to pass, but everything else will probably fail (until upgraded)