Absinthe: Nested `non_null` not caught by schema validator

Created on 6 Mar 2020  路  4Comments  路  Source: absinthe-graphql/absinthe

Environment

  • Elixir version (elixir -v): 1.9.4
  • Absinthe version (mix deps | grep absinthe): 1.5.0-rc.3
  • Client Framework and version (Relay, Apollo, etc): Apollo

Expected behavior

The schema validator should catch nested non_null field definitions at compile time.

Actual behavior

It doesn't actually do the above.

Relevant Schema/Middleware Code

Sample field definition that highlights this unexpected behaviour.

field(:permissions, non_null(list_of(non_null(non_null(:string)))))

I can create a PR for this provided someone can give me pointers on where to start.

Most helpful comment

All 4 comments

If I understand you have a list of structures, which you want to validate.

It's not gonna work as expected; field(:permissions, non_null(list_of(non_null(:string)))) works fine because your list contains string who cannot be null.

If you try (non_null(non_null(:string)) expecting to validate an object, it will fail. Because it doesn't know which field it should check against if your object has multiples fields. ie:

foo {
    bar: "x", 
    baz: "y"
}

If you want to check your structure fields one better approach. First, create a type which contains the fields you want to validate:

... module 

input_object :foo_input do 
    field :bar, non_null(:string)
    field :baz, :string
end
...

After it you can create your field as a composition off your type + check as:

field(:permission, non_null(list_of(non_null(:foo_input)))

It will not allow:

  • null as value
  • a list containing null values
  • neither values you tagged as non_null in your input_object (the only bar in this case).

I think you misunderstood the issue. All I'm saying here is

field(:permissions, non_null(list_of(non_null(non_null(:string)))))

is invalid, and should be caught at compile time. Unfortunately that's not the case.

Wow, clearly I did, so ignore my response.

Was this page helpful?
0 / 5 - 0 ratings