Extending Edge GraphQl Schema To Support Custom Fields In XM Cloud
Hello everyone! In this blog, we'll explore how to extend the GraphQL schema to support custom fields. In XM Cloud, when using the preview endpoint and running a search GQL query, all item versions are returned rather than just the latest ones. This can be challenging for authors who need to verify results in the page builder, as it relies on the preview endpoint for executing GQL queries.
In this blog, we will add _latestversion to supported field types in GQL schema. We will extend Sitecore.Services.GraphQL.EdgeSchema.Services.SearchService. Below is the code for your references.
namespace Foundation.Services
{
using System;
using System.Collections;
using System.Linq.Expressions;
using System.Reflection;
using Sitecore;
using Sitecore.Services.GraphQL.Content.GraphTypes.ContentSearch;
using Sitecore.Services.GraphQL.EdgeSchema.GraphTypes.Search.Models;
using Sitecore.Services.GraphQL.EdgeSchema.Services;
using Sitecore.Services.GraphQL.EdgeSchema.Services.Search;
using Sitecore.Sites;
public class SearchService : Sitecore.Services.GraphQL.EdgeSchema.Services.SearchService
{
private string LatestVersionFilteringFeatureFieldName = "_latestversion";
public SearchService(IOperationHandlerFactory operationHandlerFactory, IPaginationService paginationService) : this(operationHandlerFactory)
{
}
public SearchService(IOperationHandlerFactory operationHandlerFactory) : base(operationHandlerFactory)
{
var baseServiceType = typeof(Sitecore.Services.GraphQL.EdgeSchema.Services.SearchService);
var fieldInfo = baseServiceType.GetField("_supportedFieldsNew", BindingFlags.NonPublic | BindingFlags.Instance);
var supportedFieldsList = (IList)fieldInfo.GetValue(this);
var supportedFieldType = Type.GetType("Sitecore.Services.GraphQL.EdgeSchema.Services.SupportedField, Sitecore.Services.GraphQL.EdgeSchema");
object fieldInstance = Activator.CreateInstance(
supportedFieldType,
BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public,
binder: null,
args: new object[] { LatestVersionFilteringFeatureFieldName, ItemSearchOperator.EQ },
culture: null);
supportedFieldsList.Add(fieldInstance);
}
protected override Expression<Func<ContentSearchResult, bool>> ResolveFieldPredicate(string fieldName, string fieldValue, ItemSearchOperator? searchOperator)
{
if (fieldName == LatestVersionFilteringFeatureFieldName)
{
if (searchOperator == null)
{
searchOperator = ItemSearchOperator.EQ;
}
if (!FilterLatestVersionsOnly(fieldValue, searchOperator)) return null;
}
return base.ResolveFieldPredicate(fieldName, fieldValue, searchOperator);
}
private bool FilterLatestVersionsOnly(string fieldValue, ItemSearchOperator? searchOperator)
{
var isPreview = LiveModeSwitcher.CurrentValue != LiveModeState.Enabled && Context.Site != null && Context.Site.DisplayMode != DisplayMode.Edit;
return isPreview && searchOperator == ItemSearchOperator.EQ && fieldValue.Equals("true", StringComparison.InvariantCultureIgnoreCase);
}
}
}
Now it's time to add the patch. Check below code.
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"
xmlns:set="http://www.sitecore.net/xmlconfig/set/"
xmlns:queueiaritems="http://www.sitecore.net/xmlconfig/queueiaritems"
xmlns:latestversionfix="http://www.sitecore.net/xmlconfig/latestversionfix/">
<sitecore>
<services>
<register latestversionfix:require="on" serviceType="Sitecore.Services.GraphQL.EdgeSchema.Services.ISearchService, Sitecore.Services.GraphQL.EdgeSchema">
<patch:attribute name="implementationType" value="Foundation.Services.SearchService, Foundation.Services"/>
</register>
</services>
</sitecore>
</configuration>
Now let's add an entry of latestversionfix in our XDT transformation file.
<add xdt:Transform="InsertIfMissing" xdt:Locator="Match(key)" key="latestversionfix:define" value="on" />
Now add Transforms command to XM Cloud Build Json File.
transforms": [
{
"xdtPath": "/xdts/common/web.config.xdt",
"targetPath": "/web.config"
}
]
Thanks for reading and keep learning.
You can check my other blogs too if interested. Blog Website
References:
- https://doc.sitecore.com/sai/en/developers/sitecoreai/the-sitecoreai-build-configuration.html#xdt-transformations
Comments
Post a Comment