Insights

Understanding Graph Types in Sitecore

A brief description of the graph types used by Sitecore to render the graph query data in experience edge

Introduction to Sitecore Graph Types

Why is it important to know what Graph Types are? One, when trying to create a new schema or extending an existing schema of a query, it is important to know which Graph Type to use in the code. And Second, It is always good to learn something new.

From a code stand point, Graph Types are nothing but C# classes which emit some fields and properties required within the graph queries. We will start with the base class of the Graph Type which is inherited by intermediate classes and finally end with the derived class which inherits the intermediate class. There are also some stand alone classes which can be directly used such as FieldType.

Base Class

GraphType

The GraphType itself is an abstract C# class which is meant to be inherited. This class provides certain fields and properties such as Name, Description, HasMetadata(), CollectionTypes() etc. This Name and Description field is used to define the name and description of the schema. Refer image below

Screenshot of a software development documentation page showing functions for querying items in a content tree.

This class also inherits from certain interfaces to extend their properties.

Fields Properties
public string Name { get; set; } public TType GetMetadata(string key, TType defaultValue = default(TType))
public string Description { get; set; } public bool HasMetadata(string key)
public string DeprecationReason { get; set; } public virtual string CollectTypes(TypeCollectionContext context)
protected bool Equals(IGraphType other)

Intermediate Class

Intermediate classes are those who inherit from the GraphType class. Currently will see 2 intermediate classes.

  • Scalar Type
  • Complex Graph Type

Scalar Type

It is an abstract C# class which inherits the GraphType class. The properties of this class are Serialize(), ParseValue() and ParseLiteral() which are abstract as well. From the names of the properties its functionalities can also be assumed. As it inherits from GraphType class it also extends the properties such as Name, Description etc.

Properties

  • public abstract object Serialize(object value);
  • public abstract object ParseValue(object value);
  • public abstract object ParseLiteral(IValue value);

Complex Graph Type

The ComplexGraphType is an abstract class which inherits from GraphType class. It also extends the properties of GraphType class such as Name, Description etc. It requires a TSourceType object to be passed along, which directly relates to any Sitecore object such as Item (Sitecore.Data.Items.Item), Field (Sitecore.Data.Fields.Field), User (Sitecore.Security.Accounts.User) etc. This class omits properties related to fields of a TSourceType object, such as HasField(), GetField(), AddField() etc.

Properties

  • public bool HasField(string name)
  • public FieldType GetField(string name)
  • public virtual FieldType AddField(FieldType fieldType)
  • public FieldType Field(Type type, string name, string description = null, QueryArguments arguments = null, Func, object> resolve = null, string deprecationReason = null)
  • public FieldType Field(string name, string description = null, QueryArguments arguments = null, Func, object> resolve = null, string deprecationReason = null) where TGraphType : IGraphType

Derived Class

Derived classes are classes which inherit from scalar or complex graph types (Not restricted to these two). These classes inherit all the properties of GraphType class and scalar/complex graph type class. These classes override the Serialize(object value), ParseValue(object value), ParseLiteral(IValue value) properties to render specific values.

IntGraphType (Derived from Scalar Class)

This class derives from Scalar class and uses its properties to define functionalities render an Integer value.

StringGraphType (Derived from Scalar Class)

This class derives from Scalar class and uses its properties to define functionalities render a String value.

BooleanGraphType (Derived from Scalar Class)

This class derives from Scalar class and uses its properties to define functionalities render a Boolean value.

ItemGraphType (Derived from ObjectGraphTypeField Class)

This class derives from ObjectGraphTypeField which internally inherits from ComplexGraphType class. This class is used to resolve the fields of Sitecore Item (Sitecore.Data.Items.Item). It has a parameterized constructor which takes 3 arguments. The GraphQLItemResolver object which can be obtained using the GetService call (hence not necessary to pass), the name of the item and templateid of the item. And as a result it provides you with fields specific to item such as id, name, path, children, fields, template etc.

Optimizing Schema Management with Sitecore Graph Types

Effective use of Sitecore Graph Types is essential for developers tasked with schema creation or extension. These types, ranging from base to derived classes, enhance development by offering a structured framework for data interactions. Understanding the roles of scalar, complex, and derived graph types helps in achieving more efficient application management. This guide highlights the critical aspects of Sitecore Graph Types, setting the stage for developers to further improve their projects.



Meet Arsalaan Pathan

Sitecore Developer

⚽🎮🛣️

Arsalaan is not just a web developer; he's a certified expert in Sitecore and Content Hub development, bringing a unique blend of technical prowess and creative vision to every project. Since diving into the world of web development in 2016, he has continually honed his skills, initially on .NET platforms before transitioning seamlessly to Sitecore in 2020. With a passion for crafting dynamic and engaging digital experiences, Arsalaan is driven by the challenge of pushing boundaries and delivering innovative solutions that captivate audiences and drive results.

In his free time, he often savors the thrill of spontaneous road trips, exploring new destinations and soaking in the sights along the way. Alternatively, you'll find him on the soccer field, eagerly chasing after the ball, his passion for the game evident in the beads of sweat glistening on his brow. Whether traversing open roads or dominating the field, he embraces each moment with gusto and determination.

Connect with Arsalaan