Skip to content
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

Add option to initialize nullables, instead of setting them to null #62

Closed
elibosley opened this issue Dec 12, 2022 · 4 comments · Fixed by #69
Closed

Add option to initialize nullables, instead of setting them to null #62

elibosley opened this issue Dec 12, 2022 · 4 comments · Fixed by #69
Labels
enhancement New feature or request

Comments

@elibosley
Copy link

It would be awesome if you could merge this fork: https://github.com/immuta/graphql-codegen-factories (not my code) in order to allow nullable values to be treated as non-null when generating mocks. This would greatly improve the usefulness of this library for me.

Thanks a ton, awesome work on this!

@zhouzi
Copy link
Owner

zhouzi commented Dec 14, 2022

Hey! I'm happy to bring in any improvement 👍

I just had a look at the fork but it seems to implement a few different things so I am not sure to identify the changes you are interested in.

Would you like for all nullable fields to be treated as non-nullable from the schema? Should it also be reflected in the types or only in the factories' return value?

So given the following schema:

type Post {
  id: ID
  title: String
  excerpt: String
}

It should generate:

interface Post {
  id: string;
  title: string;
  excerpt: string;
}

function createPostMock(props: Partial<Post>): Post {
  return {
    id: "",
    title: "",
    excerpt: "",
    ...props,
  };
}

Or the fields should remain nullable in the type but filled with default values in the factory:

interface Post {
  id: string | null;
  title: string | null;
  excerpt: string | null;
}

function createPostMock(props: Partial<Post>): Post {
  return { 
    id: "",
    title: "",
    excerpt: "",
    ...props,
  };
}

@elibosley
Copy link
Author

Hey! The first thing you discussed would be amazing - so basically add a flag to treat nullables as non-nullable if you request - allowing generation of non-nullable data. I know this is a problem with my schema but it would really help me for testing and I think would be a great QOL improvement!

@zhouzi zhouzi added the enhancement New feature or request label Dec 23, 2022
@zhouzi
Copy link
Owner

zhouzi commented Dec 23, 2022

Thanks for the suggestion, I can definitely see how that would be useful. It should be straightforward to implement, I'll look into it 👍

@zhouzi zhouzi changed the title Nullables always treated as null Add option to initialize nullables, instead of setting them to null Dec 31, 2022
@zhouzi
Copy link
Owner

zhouzi commented Jan 20, 2023

@elibosley I just released v1.2.0 with this feature. See the documentation for an example of how you can configure it: https://gabinaureche.com/graphql-codegen-factories/configuration/schema#configmaybevaluedefault The gist is to set the maybeValueDefault configuration option to "{defaultValue}".

Let me know if you have any feedback 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants