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

New nullable mode, disallow null for input field but allow undefined #1276

Open
MatthiasKunnen opened this issue May 11, 2022 · 2 comments
Open
Labels
Community 👨‍👧 Something initiated by a community Enhancement 🆕 New feature or request
Projects

Comments

@MatthiasKunnen
Copy link

MatthiasKunnen commented May 11, 2022

Is your feature request related to a problem? Please describe.
I'm trying to create a Mutation that allows partially patching an entity which has required fields. Example entity:

export class Entity {
    name: string
    description?: string | null
}

My current input type for this entity is

@InputType()
export class EntityUpdateInput {

    @Field(() => String, {nullable: true})
    name?: string | null

    @Field(() => String, {nullable: true})
    description?: string | null
}

Taking the description field as an example, mutations can either

  1. Pass a string -> it gets updated
  2. Pass null -> the field gets cleared
  3. Do no include the field in the argument -> the field is left alone

Now this all works marvelously for an optional field. However, for the name field it should not be allowed to pass null as the field is not allowed to be cleared.

Describe the solution you'd like
I would like the nullable property of the @Field decorator to feature an option optionalOnly which would result either a) an error being thrown on null or b) null properties being deleted. This would result in the following input type:

@InputType()
export class EntityUpdateInput {

    @Field(() => String, {nullable: 'optionalOnly'})
    name?: string

    @Field(() => String, {nullable: true})
    description?: string | null
}

I understand GraphQL cannot distinguish between nullability (null) and optionality (undefined) in its generated spec/documentation and that this has the potential of confusing the consumers of an API that differentiates between the two. However, I believe that this use case is a valid one.

Describe alternatives you've considered
I've looked into the possibility of using custom validators and class-validator to accomplish this but have not found a way so far as I'm not really trying to validate a value but rather, change it.

I had a look at the code of type-graphql but have not been able to estimate the time required or impact on the code base of this feature.

Additional context
Potentially related to #340 (transforming input fields).

@MichalLytek
Copy link
Owner

I understand GraphQL cannot distinguish between nullability (null) and optionality (undefined) in its generated spec/documentation and that this has the potential of confusing the consumers of an API that differentiates between the two.

Exactly. Without a proper layer for transforming input fields, all we can achieve is a false sense of type correctness. In runtime it will work the same.

@MatthiasKunnen
Copy link
Author

This feature request does indeed require the ability to transform input fields for option b or the ability to validate (throw errors) for option a.

@MichalLytek MichalLytek added Enhancement 🆕 New feature or request Community 👨‍👧 Something initiated by a community labels May 12, 2022
@MichalLytek MichalLytek added this to Backlog in Board via automation May 12, 2022
@MichalLytek MichalLytek added this to the Future release milestone May 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Community 👨‍👧 Something initiated by a community Enhancement 🆕 New feature or request
Projects
Board
  
Backlog
Development

No branches or pull requests

2 participants