Skip to content

Mutable Objects

Aaron Aichlmayr edited this page Apr 11, 2019 · 3 revisions

Due to graphql limitations you need to create two objects to be able to mutate. To reduce the maintenance you can use a base class to contain the fields. This base class will need a type argument. The recommended class definition is public class ClassNameBase<T>: SqlFieldResolver<T> where T : SqlFieldResolver<T>, new(). Inside that class add the metadata fields (Table, DefaultOrder, PrimaryProperty) and any sql fields which are both queryable and mutatable.

Next add your mutable class. The recommended class definition is public class ClassNameMutable: ClassNameBase<ClassNameMutable>, IInputObjectGraphType. This class will need no properties unless you want to add properties which are mutable but not queryable. You will also need to add the GraphQLInputObject attribute

Next add your query class. The recommended class definition is public class ClassName: ClassNameBase<ClassName>, IObjectGraphType, IMutationResolver<ClassName, ClassNameMutable>. Inside that class you will need to add any fields which are queryable but not mutatable, this includes any foreign object references. You will also need a Mutate method. You can use the following template:

public ClassName Mutate(ResolveFieldContext context, ClassNameMutable input)
{
	return SqlFieldMutator.Mutate<ClassName, ClassNameMutable>(context, input);
}

This will allow you to edit create and edit objects using the object's name when doing a mutation. If you need fields skipped when doing an insert (ex. Auto Increment Ids) set the SkipOnInsert property of the SqlGraphQLField attribute to true for those fields

Optional: To enable Delete Add the IDeleteResolver<TIdType> interface to your query class. TIdType should be replaced with the type of your identifier. Inside that class you will also need to add a Delete method. You can use the following template:

public void Delete(ResolveFieldContext context, TIdType id)
{
	SqlFieldMutator.Delete<ClassNameMutable, TIdType>(context, id);
}
Clone this wiki locally