Skip to content

Instantly share code, notes, and snippets.

@IvanLieckens
Created June 4, 2019 07:56
Show Gist options
  • Save IvanLieckens/3cf2d962e2e9b96fe23b49c6ed8628d1 to your computer and use it in GitHub Desktop.
Save IvanLieckens/3cf2d962e2e9b96fe23b49c6ed8628d1 to your computer and use it in GitHub Desktop.
Sitecore JSS GraphQL schema extension to output fields in JSS format
using GraphQL.Types;
using Newtonsoft.Json;
using Sitecore.Configuration;
using Sitecore.Data.Items;
using Sitecore.Data.Fields;
using Sitecore.Diagnostics;
using Sitecore.LayoutService.Serialization.FieldSerializers;
using Sitecore.LayoutService.Serialization.ItemSerializers;
using Sitecore.LayoutService.Serialization.Pipelines.GetFieldSerializer;
using Sitecore.Services.GraphQL.Content.GraphTypes;
using Sitecore.Services.GraphQL.GraphTypes;
using Sitecore.Services.GraphQL.Schemas;
using System.Globalization;
using System.IO;
using System.Text;
using System.Linq;
using System.Collections.Generic;
public class JssFieldsExtender : SchemaExtender
{
private const string OwnFieldsAttributeName = "ownFields";
public JssFieldsExtender(IGetFieldSerializerPipeline fieldSerializerPipeline)
{
ExtendTypes<ItemGraphType>(
type => type.Field<JsonGraphType>(
"jssFields",
"Gets the item fields in JSS format",
new QueryArguments(new QueryArgument<BooleanGraphType> { DefaultValue = false, Name = OwnFieldsAttributeName, Description = "Limits the fields to the fields on the template only or not" }),
context => Serialize(context, fieldSerializerPipeline),
null));
ExtendTypes<ItemInterfaceGraphType>(
type => type.Field<JsonGraphType>(
"jssFields",
"Gets the item fields in JSS format",
null,
null,
null));
}
protected virtual string Serialize(
ResolveFieldContext<Item> context,
IGetFieldSerializerPipeline getFieldSerializerPipeline)
{
Item item = context.Source;
bool ownFields = context.GetArgument(OwnFieldsAttributeName, false);
StringBuilder result = new StringBuilder();
IEnumerable<Field> fields;
if(ownFields)
{
fields = item.Template.OwnFields.Select(f => item.Fields[f.ID]);
}
else
{
item.Fields.ReadAll();
fields = item.Fields;
}
foreach (Field field in fields)
{
result.Append($"{SerializeField(field, getFieldSerializerPipeline)},");
}
// Strip last , if needed
if(result.Length > 0)
{
result = result.Remove(result.Length - 1, 1);
}
return $"{{{result}}}";
}
protected virtual string SerializeField(
Field field,
IGetFieldSerializerPipeline getFieldSerializerPipeline)
{
using (new SettingsSwitcher("Media.AlwaysIncludeServerUrl", bool.TrueString))
{
GetFieldSerializerPipelineArgs args = new GetFieldSerializerPipelineArgs()
{
Field = field,
ItemSerializer = new DefaultItemSerializer(getFieldSerializerPipeline)
};
IFieldSerializer result = getFieldSerializerPipeline.GetResult(args);
Assert.IsNotNull(result, "fieldSerializer != null");
StringWriter stringWriter = new StringWriter(new StringBuilder(256), CultureInfo.InvariantCulture);
using (JsonTextWriter writer = new JsonTextWriter(stringWriter))
{
writer.Formatting = Formatting.None;
result.EnableRenderedValues = Sitecore.Context.PageMode.IsExperienceEditorEditing;
result.Serialize(field, writer);
}
return stringWriter.ToString();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment