Absinthe Non Empty List type

I would like to specify a ‘list_of’ something which cannot be empty.

Any ideas?

Or should I have a go, rolling my own?

Doesn‘t Absinthe support something like non_null(list_of(...))?

1 Like

yep but [] is still valid as far as I can see…

Seems for bare GraphQL all possible declarations would accept an empty list.

1 Like

I am thinking about rolling my own scalar type perhaps… suspect there’s a simple solution, need some time to think about it though…

Hey @mmport80

When building a GraphQL API there’s an important distinction between the kind of validation and type guarantees offered by GraphQL and its implementations, and validations you may want to do within a resolver. The type system itself is extensible to some degree, in that you can (as you noted) create custom scalars. What GraphQL really lacks though are fully custom generic types. You can think of list_of or non_null as essentially generic types List<T> or NonNull<T> but these are essentially special cases, there isn’t a way to add NonEmptyList<T>.

This is a very long and verbose way to say: You’re best off just using some kind of validation in the resolver. The spec is still evolving, and there may be more capabilities type system wise down the line, but trying to alter how it works yourself isn’t going to be a fruitful endeavor.

5 Likes

Some co-workers recently wondered how to specify that one of a few fields should be present—essentially an xor between a couple of id fields.

My answer was that it simply isn’t something that GraphQL provides and using pattern matchin with a fall through clause was definitely powerful enough. I suppose that is the quandary with a partial type system like GraphQL, there isn’t any way to express complex rules and I doubt there ever will be.

3 Likes

Yeh. I like how GraphQL is stubbornly simple - however much I want it to be more expressive sometimes…

Thanks for this, saved me time and energy : )