Fork me on GitHub

I’m trying to add “Any” type for random object outputs using this description:

:Any {:description "Anything"
      :parse identity
      :serialize identity}
Unfortunately, when resolved value of this type is a list, query fails with error "Field resolver returned a collection of values, expected only a single value". Is there any way around this other than doing something like wrapping the result list in some dummy object like {:result […]}?

Lennart Buit14:09:09

the spec says that any json-serializable value is indeed valid for a custom scalar like this. I guess lacinia does not like that. I can understand why tho, I guess it is not that common to have an arbitrary object type in GraphQL.


It is such a common mistake to return a list when you mean a single object and vice-versa that Lacinia has some guard rails for that and its separate from the rudimentary type system. Turning those guard rails off would be disruptive at would probably involve some special key on the scalar type (something like :validate-values false ) to indicate that the normal value vs. list checks can be skipped.


Or perhaps we don't need the single vs. list checks and should trust the dev's code? Or perhaps we should have a development mode with extra checks vs. a production mode that optimizes for speed ?


But for your case, I think you are onto something like a wrapper type, so the scalar's serialize could take most Integer, String, etc. values as-is, but for a sequence you might define an AnyList record that the serialize could de-reference as a special case.

Lennart Buit16:09:16

Can’t you warn when what you return isn’t supposed to be list like unless it’s type is a custom scalar. You know if it is even worth fixing.