sezaru
Enabling form validation via AshGraphql
First, a disclaimer, I didn’t think too much about this idea so maybe there is already a better solution or even if this is possible giving GraphQL constraints.
The idea is to “replicate” the validate functionality of LV’s form validation but in the API’s generated by AshGraphql.
For example, right now, AFAIK, the only way to validate a create/update form using AshGraphql’s generated apis is to either submit the full form and get the errors, or to duplicate the validation in the frontend.
I was wondering if we could do something like adding a special field called validate_only or something like that that would allow the following:
-
Send the form with only a subset of the fields so the frontend can just send fields that the user filled and get validation for them only.
-
Only apply the changeset constraints and return instead of creating/updating the resource (basically replicating the functionality of a form validate).
Obs. Maybe (1) would not be possible with just the validate_only flag since the frontend graphql validation would block the user from sending fields that can’t be nullable? In that case, maybe another approach would be to add an option to the action in the graphql block that would generate another action only for validation where all fields can be nullable (or some other special type that indicates it is not filled yet), example:
graphql do
mutations do
create :create, :create, include_validation?: true
end
end
This would create a validateCreateResource api in graphql side besides the createResource api.
First Post!
zachdaniel
I think its a great idea
Probably my suggestion would be to have a special type of query
queries do
validate :validate_create_user, :create
end
Most Liked
sezaru
Hey @zachdaniel, I advanced a little bit in this task. I now have a validate action that will correctly give me the errors and will put all fields as nullable. For example:
This is the create action:
This is the validate for the create action:
This is the validation working:
I still need to clean up the code a little bit before I push a PR and there are some issues that i was not able to fix and I was wondering if you would be able to fix/improve them in my PR.
Basically, the main issues are:
- I wasn’t able to create it as a query api, only as a mutation one.
- I couldn’t figure out how to remove the
resultfield in the validation api since that is not needed at all.
sezaru
Hey @zachdaniel , quick question.seems like the update mutation in AshGraphql will create a query and use Ash.bulk_update to any update action. I’m not 100% sure on how to “convert” this into an Ash.Changeset.for_update call.
Any tips on that?
zachdaniel
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex













