Fork me on GitHub
#graphql
<
2024-02-23
>
defa09:02:48

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?

vlaaad10:02:16

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

vlaaad10:02:29

here is an example

defa10:02:03

Thanks for the swift reply, @U47G49KHQ. Yes, wrapping in a map was my workaround but it doesn't look all that pretty. I thought there might be an other approach? @U04VDKC4G any ideas or ways to teach lacinia a new trick?

favila11:02:12

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

favila11:02:13

Which is to say I think what you want would be difficult

favila11:02:25

Note your custom scalar can decode to a vector , but your resolvers must return something non-vector for the encoder

favila11:02:15

Which makes your scalar codecs not bijective, but maybe you really want vectors at least for input

defa08:02:03

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.