Giter Club home page Giter Club logo

Comments (7)

budde377 avatar budde377 commented on August 26, 2024

Alright, how would you express these two cases with your approach:

  • copy with overwriting a nullable value with null?
  • copy with preserving a nullable value?

from graphql_codegen.

Hari-07 avatar Hari-07 commented on August 26, 2024

To be honest I'm not very sure how it is implemented but I have seen freezed managing to do what you mentioned despite its copyWith taking in values. I even tested it now and it seems to satisfy both the points you've mentioned.

I created a simple person class

@freezed
class Person with _$Person{
  const factory Person({
    required String? firstName,
    required String? lastName,
  }) = _Person;
}

and ran this test against it

test('Overwriting a nullable value with null', () {
  Person person = const Person(
    firstName: 'John',
    lastName: 'Doe',
  );

  Person person2 = person.copyWith(
    firstName: null,
  );

  expect(person.firstName, 'John');
  expect(person2.firstName, null);

  expect(person.lastName, 'Doe');
  expect(person2.lastName, 'Doe');
});

As far as I understood your question this satisfies it

  • The firstName property has been overwritten with a null
  • The secondName property is a nullable that's been preserved

Complete freezed file generated for the Person class: https://gist.github.com/Hari-07/11d5ce80eae042663a792877ba6b9d28

Additionally there's also a nice deepcopy on it that'd be quite useful too: https://github.com/rrousselGit/freezed#going-further-deep-copy

from graphql_codegen.

budde377 avatar budde377 commented on August 26, 2024

Thank you for sharing this! It's definitely a good source of improvement and I like the deep copy functionality.

from graphql_codegen.

Hari-07 avatar Hari-07 commented on August 26, 2024

Rather than reinvent the wheel, it might be easier for you to create freezed classes from the graphql file and then let it handle the utility methods on it. It'll add a dependency, but I think it's worth it

from graphql_codegen.

budde377 avatar budde377 commented on August 26, 2024

I think that's a trade off. Generally, I'd like to reduce the number of runtime dependencies and get more control over the generated output, moving to freezed is not in line with this.

Furthermore, we're in a unique position to implement this our selves in a more efficient way, because we have a very limited domain to cover. We have full control over the classes and can make anything happen.

Lastly, this work is not super complicated, I've already implemented a better copyWith and have the deep copy implementation pending.

from graphql_codegen.

Hari-07 avatar Hari-07 commented on August 26, 2024

That's awesome to hear! Looking forward to your updates

from graphql_codegen.

budde377 avatar budde377 commented on August 26, 2024

Released new copyWith and deep copy in 0.10.0-beta.2

from graphql_codegen.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.