prismake / typegql

Create GraphQL schema with TypeScript classes.
https://prismake.github.io/typegql/
MIT License
423 stars 21 forks source link

Feature request: an alias for @Field({isNullable: false}) #34

Closed capaj closed 6 years ago

capaj commented 6 years ago

this is written often and is quite verbose to write. Compared to just @Field() it's much lengthier. If there was an alias such as @FieldNN or maybe @NonNullField it would definitely save me some reading/writing.

From the implementation perspective it's trivial and I think the saved characters are worth having such alias.

pie6k commented 6 years ago

Right. Implementation is trivial:

interface NonNullFieldOptions extends FieldOptions {
  isNullable: never;
}

export function NonNullField(options?: NonNullFieldOptions) {
  return Field({...options, isNullable: false});
}

But I'm kinda opposing to that. I believe having more configurations is always making project harder to maintain.

What do you think about making fields NonNull by default?

capaj commented 6 years ago

@pie6k I use nullable fields more often than non nullable in our project, so I wouldn't make all fields NonNull by default.

I agree that more configuration is harder to maintain. To illustrate-this is certainly less configuration:

@ObjectType()
export class OrganisationFeatures extends FeaturesBase {
  @NonNullField() campaigns: boolean
  @NonNullField() custom_email_sender_address: boolean
  @NonNullField() OKTA_SAML: boolean
  @NonNullField() search_analytics: boolean
  @NonNullField() SCORM: boolean
  @NonNullField() custom_font: boolean
}
// than this:

@ObjectType()
export class OrganisationFeatures extends FeaturesBase {
  @Field({ isNullable: false })
  campaigns: boolean
  @Field({ isNullable: false })
  custom_email_sender_address: boolean
  @Field({ isNullable: false })
  OKTA_SAML: boolean
  @Field({ isNullable: false })
  search_analytics: boolean
  @Field({ isNullable: false })
  SCORM: boolean
  @Field({ isNullable: false })
  custom_font: boolean
}

// You may notice that prettier now puts all fields on single line with the decorator.

aliasing a parameter set is not hard to maintain IMHO. It's 3 extra lines + 3 lines of typings and this small change can save hundreds or more of extra characters and lines on a bigger project. Which of these you'd rather read?

pie6k commented 6 years ago

I was thinking a bit about this and I've decided to not do this.

It'll require the same thing for input fields aka NonNullInputField and propably the same for interface fields soon. Could be the same with other ideas that might come soon.

Also, NonNullField is still a field.

Perhaps we can return to this topic when there will be more supporting it voices or other reasons for it. For now I'll close it.

Also as an aliast - it's very easy to create it yourself in your project.