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
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
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
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.