comigor / artemis

Build dart types from GraphQL schemas and queries
MIT License
495 stars 119 forks source link

Add copyWith methods generation to objects #109

Open vasilich6107 opened 4 years ago

vasilich6107 commented 4 years ago

As far as dart passes objects by reference it would be great to have a 'copyWith' extension for the data classes with something like this

https://github.com/numen31337/copy_with_extension

docs https://www.oleksandrkirichenko.com/blog/dart-extensions/

vasilich6107 commented 4 years ago

I just realized that SomeCalss.fromJson(someClass.toJson()) could be some kind of a trick to make a copy)

comigor commented 4 years ago

But you'd have to serialize and deserialize it again. copyWith would be more performatic.

vasilich6107 commented 4 years ago

@comigor what do you think about this https://pub.dev/packages/freezed ?

comigor commented 4 years ago

I've personally never used it, but it seems nice. I liked the future-proof for non-nullables and late variables!

FickleLife commented 4 years ago

+1. My use case is a search/filter with several parameters being passed to a query. I have three different fields updating the variable at different times (textfield, filters, sorting) and as options are selected in any of these I want to update the global variable that is located in the service with these parameters. I can't use the Artemis generated type from the graphql query itself as it's immutable. In the end I created another type that has a copyWith method which is largely identical to the Artemis generated type.

Happy to be corrected if there is a better way to do this.

joelpinto commented 4 years ago

@comigor what do you think about this https://pub.dev/packages/freezed ?

I strongly recommend this

ramyak-mehra commented 3 years ago

is there an update on this issue? I am using fragments that create mixins thus fromJson and toJson methods are not available.

is there any way I can add let's say https://pub.dev/packages/freezed manually ? @vasilich6107

vasilich6107 commented 3 years ago

No progress for now

ramyak-mehra commented 3 years ago

If made this extension method, if anyone looking to do something similar.

extension NewInstance on ClassMixin {
  ClassMixin jsonCopyWith() {
    return Class.fromJson(
        (this as Class).toJson());
  }
}
fpabl0 commented 3 years ago

@vasilich6107 freezed would be a great addition and it would simplify a little bit the needed dependencies: json_annotation and equatable wouldn't be needed because freezed_annotation already exports json_annotation and already has equality check :). Also it has a nice toString() method implementation :)

JulianBissekkou commented 3 years ago

It would be nice to have this implemented on the generated fragment mixins as well.

salihagic commented 3 years ago

Is this still in consideration? It would be nice to have copyWith method in the generated classes.