In lacinia I want to define a custom type/scalar which is represented as a two-element vector/array (tupel):
:Something
{:fields
{:values {:type (list (non-null :Tupel))}
;...
}}
But when resolving values I try to return something like [[1 2] [3 4]] which results in a GraphQL error: "Field resolver returned a collection of values, expected only a single value.". Could that be fixed by some sort of custom field resolver?I’m just wrapping the tuple (vector) in a map and unwrap it in the scalar’s serializer. Just like @vlaad suggested. Works fine. Thanks for the explaining the internals of lacinia.
This rings a bell. I had this issue before. The workaround is to define a scalar type (which you already have, Tuple), make your resolver return a wrapper map with a single key, and add type serializer that accesses that key
here is an example
Custom type definition that has a serializer simply accessing a key: https://gitlab.com/arbetsformedlingen/taxonomy-dev/backend/jobtech-taxonomy-api/-/blob/main/src/clj/jobtech_taxonomy_api/db/graphql/changelog.clj?ref_type=heads#L265
And the wrapper that wraps a value, used in the resolver: https://gitlab.com/arbetsformedlingen/taxonomy-dev/backend/jobtech-taxonomy-api/-/blob/main/src/clj/jobtech_taxonomy_api/db/graphql/changelog.clj?ref_type=heads#L221
Lacinia execution involves a deep-merge step which takes multiple resolver results and combines them into a single value. This code is not aware of graphql types, so it cannot know that your vector is meant to be a tuple rather than a collection
Which is to say I think what you want would be difficult
Note your custom scalar can decode to a vector , but your resolvers must return something non-vector for the encoder
Which makes your scalar codecs not bijective, but maybe you really want vectors at least for input