6

I'm working on a 10.2 solution with JSS & NextJS using the GraphQL (edge) endpoint. I already noticed this endpoint is sometimes quite different compared to others and now I would like to extend it. We are also using SXA and I wanted to use a treelist with tokens in the datasource so I created a custom field for that - which is pretty easy to do.

Our problem now is that the data from that field is fetched as string and not as a Multilist field (which would give us an item array). And as GraphQL is used to fetch all data that is an issue...

My first attempt was adding my field in the config (identical as the Treelist) to /sitecore/api/GraphQL/defaults/content/fieldTypeMappings/standardTypeMapping in the config as this is mentioned in the edgeContent schema provider. That doesn't work.

I did find 2 ways I can extend the schema:

  • Apparently all fields are added to a list and attached to a GraphType in Sitecore.Services.GraphQL.EdgeSchema.TemplateGeneration.FieldMapping.DefaultFieldTypeFactoryStore. I can create my own version of this factory and inject instead - this works but somehow I don't think this is the correct way to extend this as the class doesn't seem to be made to be extended.
  • I was able (as a test) to extend existing GraphType's (eg the ImageFieldGraphType) with extra properties by creating a SchemaExtender. This feels like the more official way to extend things as it is also mentioned in the SC documentation (although not for 10.2). But.. this way I managed to extend existing types, but I don't know how to tell the schema that my "Tokened Treelist" is actually just a MultilistGraphType with such an extender.

So, my question is: how can I use my custom field in the GraphQL schema as Multilist? Can this be done with a SchemaExtender, and if so - how? Should I overwrite the FieldTypeFactoryStore?

1
  • Did you find a solution for your question? I am in similar situation I added a custom Image field and data from that field is fetched as string. Thank you. Mar 1, 2023 at 4:53

1 Answer 1

3

We had a similar challenge some time ago and we contacted Sitecore Support.

Sitecore support summary:

The Edge schema provider doesn't actually use the "fieldTypeMappings" configuration node. The field type mapping is done using a separate "IFieldTypeFactoryStore" service that is registered in "Sitecore.Services.GraphQL.EdgeContent.config". The mapping is hardcoded and doesn't depend on the configuration. The reason behind this is that the Edge preview endpoint should mirror the schema and behavior of Sitecore Experience Edge for XM. If any changes are done to the preview endpoint, it will behave differently from the actual Delivery Experience Edge endpoint which will make the development and testing unreliable.

If you don't plan to use Sitecore Experience Edge for XM, please consider creating a new GraphQL endpoint and using it instead of "edge": https://doc.sitecore.com/xp/en/developers/hd/200/sitecore-headless-development/start-using-sitecore-graphql-api.html This new endpoint can use the default "systemContent" which will allow you to use custom field mapping.

It means that there is no "right and clean" way to achieve what you ask for. The right way will be usage GraphQL endpoint that is based on "content", which is more extendable.

As we don't plan to use Sitecore Experience Edge for XM, we implemented our own Sitecore.Services.GraphQL.EdgeSchema.TemplateGeneration.FieldMapping.IFieldTypeFactoryStore and added FieldMapping there. This approach works, but doesn't look great.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.