sezaru

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:

  1. 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.

  2. 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

zachdaniel

Creator of Ash

I think its a great idea :smiley: Probably my suggestion would be to have a special type of query

queries do
  validate :validate_create_user, :create
end

Most Liked

sezaru

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:

  1. I wasn’t able to create it as a query api, only as a mutation one.
  2. I couldn’t figure out how to remove the result field in the validation api since that is not needed at all.
sezaru

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

zachdaniel

Creator of Ash

Awesome! I will review next week as its a hefty PR :heart:

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54260 488
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40165 209
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44265 214
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement