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

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
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
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New

Other popular topics Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31586 112
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
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