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

Make some standard fields non nullable #105

Closed
ahey opened this issue Jan 7, 2024 · 8 comments
Closed

Make some standard fields non nullable #105

ahey opened this issue Jan 7, 2024 · 8 comments
Labels
enhancement New feature or request

Comments

@ahey
Copy link
Contributor

ahey commented Jan 7, 2024

Generating a graphql layer for my front-end in typescript, I noticed some fields are unnecessarily nullable. To achieve a more pleasant typescript experience, I propose the following fields be made non null:

  • errors as returned by a mutation. In case of no errors, an empty list will be returned. Furthermore, each item in the error list can also be made not null, since it either exists in the list or not, and we don't want nulls in the list.
  • error.fields

In addition to these fields, I would also like to see the the _result fields be made non nullable, as they will always contain the result and errors fields, except in the case of root_level_errors? being enabled, in which case i suppose the _result field should be nullable.

See here for an example of the changes (not ready for merge, just for discussion)

This would be a breaking change to the existing api. We could keep the existing behaviour as default, and add a config setting to enable the new behaviour. The existing behaviour should be considered deprecated and we could warn to use the flag, and default to use it in v1.0.0.

What do you think?

@ahey ahey added the enhancement New feature or request label Jan 7, 2024
@zachdaniel
Copy link
Contributor

IIRC result will be null if errors is present, and errors will be null if the result is present, right? However, items in the error list being non null can definitely be done.

@rbino
Copy link
Contributor

rbino commented Jan 30, 2024

I think (also from the diff) that @ahey was referring to the fact that the Result type as a whole is non-nullable (as you said you always either have a result or some errors), so

createFoo(input: CreateFooInput): CreateFooResult

should instead be

createFoo(input: CreateFooInput): CreateFooResult!

This (as mentioned above) is particularly useful when integrating with TypeScript where nullability is important for type generation.

Another doubt I have is: should the input also be non-nullable?

@zachdaniel
Copy link
Contributor

Ah, yes you are right, we can definitely make that return type non nullable. Going from nullable -> not nullable on a returned type isn't breaking, so I'd definitely accept a PR on that front. As for non-nullable input, that one is more tricky. Not all actions require some kind of input, and if all keys are not required, the input could also be not required. It would be a breaking change to require it, even if we only did it in cases where there was at least one required input inside of it, because nullable -> not nullable on an input type is theoretically breaking. Although in those scenarios the action would fail 100% of the time... 🤷 so maybe fine to just make both changes. overall result type not nullable, and input not nullable only if at least one member is not nullable.

rbino added a commit to rbino/ash_graphql that referenced this issue Jan 31, 2024
As discussed in ash-project#105 and ash-project#110, put this behind an opt-in configuration to avoid
breaking existing code.
The ID in update mutations is always non-null if non-null mutation arguments are
allowed, while input is non-null if it's allowed _and_ there is at least a
non-null field in the input.

Document the newly added config variable in the getting started guide.
rbino added a commit to rbino/ash_graphql that referenced this issue Jan 31, 2024
rbino added a commit to rbino/ash_graphql that referenced this issue Jan 31, 2024
zachdaniel pushed a commit that referenced this issue Jan 31, 2024
* improvement: make mutation arguments non-null

As discussed in #105 and #110, put this behind an opt-in configuration to avoid
breaking existing code.
The ID in update mutations is always non-null if non-null mutation arguments are
allowed, while input is non-null if it's allowed _and_ there is at least a
non-null field in the input.

Document the newly added config variable in the getting started guide.

* chore: enable non-null mutation arguments in tests
zachdaniel pushed a commit that referenced this issue Jan 31, 2024
@barnabasJ
Copy link
Contributor

barnabasJ commented Feb 9, 2024

I just updated and stumbled upon this because my test now failed with data being now nil instead of it being %{"<operationName>" => nil}; I was a bit confused at first because a couple of lines above I was still getting something like this. But it was a read_one query, maybe to be consistent, the return type should be non_nullable for those too? Same for get probably.

At least I think if there is no error there should always be a record returned, otherwise there will be a NotFound Error?

@zachdaniel
Copy link
Contributor

@barnabasJ does this solve the issue? #110

@barnabasJ
Copy link
Contributor

barnabasJ commented Feb 9, 2024

@barnabasJ does this solve the issue? #110

#110 introduced the change and #114 reverted it back to the old behaviour for me

@ahey
Copy link
Contributor Author

ahey commented Apr 22, 2024

Hey @rbino thanks for rolling with this one! Is it safe to say that this is complete? If so I will close the issue.

@ahey
Copy link
Contributor Author

ahey commented May 3, 2024

I found another area for improvement, which from my testing appears to be backwards compatible. I have made a PR with the changes in make mutation result errors list non-nullable. Can anyone forsee any issues with this?

My understanding is that moving from a nullable list of errors to a non-nullable list of errors is safe, since we always return an empty array in case of no errors.

@ahey ahey closed this as completed May 4, 2024
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

No branches or pull requests

4 participants