graphql

defa 2024-02-23T09:48:48.679989Z

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?

defa 2024-02-25T08:09:03.399549Z

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.

vlaaad 2024-02-23T10:00:16.078369Z

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

vlaaad 2024-02-23T10:00:29.683599Z

here is an example

defa 2024-02-23T10:07:03.253959Z

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

favila 2024-02-23T11:00:12.062569Z

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

favila 2024-02-23T11:01:13.100189Z

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

favila 2024-02-23T11:02:25.515129Z

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

favila 2024-02-23T11:04:15.253679Z

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