In a previous post I wrote about extending the JSS Layout service to support serialization of field types that aren’t serialized OOTB, like the Multiroot treelist added by SXA. Catch up on that post here. That was great, but there is a little more to ensure those field types are also mapped and serialized by GraphQL queries.
Assuming you have a Multiroot Treelist field in a template like the below:
You could expect to write a GraphQL query like the following to retrieve the target items selected in the Multiroot Treelist.
Out of the box, the result of the “myMultiRootTreelist” field would be empty, as the type mapping is not recognised for that type and it falls back to a string. (ie. the pipe delimited list of IDs).
The solution is pretty straight forward, and quite reminiscent of the solution for enabling serialization of these types in the Layout Service. Sitecore exposes configuration to map field types to “Field Type Factories”. In this case the Multiroot treelist’s raw value will always be the same as any other Multilist, a pipe delimited list of IDs. So we can use the same approach used for the other Multilist fields. Patching in new mapping in the configuration is as easy as:
After applying this patch, running the same query as above will result in a response like:
This approach could also be applied to any other types that store multilist like raw values to reference a list of items. Also consider using one of the pre-configured mappings when creating any custom field types and intend using them in GraphQL.