Closed jasonlaw closed 3 years ago
Noooo, you can't do that. First you don't need any expr at all, just:
MapEntity
and that's it, since all types match. But if there's an expression (only if you have prop name mismatch, or need conversion) - then it should be individual assignments to properties. NGraphQL actually looks inside this expr new expr with multiple assignments, and takes individual assignment expressions and uses them; simple x => x.ToCommunity() is not what it expects; all prop assignments are hidden inside the method.
I am not sure if I follow, but here is what I have changed to the code:
MapEntity<ICommunity>().To(x => new Community { Logo = x.Logo.ToModel() } );
Btw, in fact my entity and model are subclasses, above code was cleaned up to avoid any confusion, but it seems that the MapEntity doesn't support for the subclass.
Here is the new error I have got. ================= GraphQL Model Errors Detected ========================= Field 'Community.address' (module CommunityGraphQLModule) has no associated resolver or mapped entity field. Field 'Community.city' (module CommunityGraphQLModule) has no associated resolver or mapped entity field. Field 'Community.state' (module CommunityGraphQLModule) has no associated resolver or mapped entity field. Field 'Community.postcode' (module CommunityGraphQLModule) has no associated resolver or mapped entity field. Field 'Community.country' (module CommunityGraphQLModule) has no associated resolver or mapped entity field. ================= End GraphQL Model Errors ==============================
Entity with subclass
public interface ILocation
{
[Size(100)]
string Address { get; set; }
[Size(50)]
string City { get; set; }
[Size(50)]
string State { get; set; }
[Size(10)]
string Postcode { get; set; }
[Size(50)]
string Country { get; set; }
}
[Entity]
[Display("{Id} - {Name}")]
public interface ICommunity : ILocation
{
[PrimaryKey, Size(10)]
[AutoSequenceNumber()]
string Id { get; }
[Size(300)]
string Name { get; set; }
[Nullable, Unlimited]
string Description { get; set; }
[Nullable, CleanupOnDelete]
IFile Logo { get; set; }
bool UseLogoAsBackgroundImage { get; set; }
IList<IMemberInCommunity> RegisteredMembers { get; }
IList<IVendorInCommunity> RegisteredVendors { get; }
IList<IUser> Officers { get; }
//[Nullable]
//IBillingAccount BillingAccount { get; set; }
}
Model with subclass
public class Location
{
public string Address { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Postcode { get; set; }
public string Country { get; set; }
}
public class Community : Location
{
public string Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public UploadFile Logo { get; set; }
public bool UseLogoAsBackgroundImage { get; set; }
}
Did some debugging, could it be the Type.GetMembers doesn't return the base type's members?
NGraphQL.Server: ServerReflectionHelper.cs
public static IList<MemberInfo> GetFieldsPropsMethods(this Type type, bool withMethods) {
var mTypes = MemberTypes.Field | MemberTypes.Property;
if (withMethods)
mTypes |= MemberTypes.Method;
var members = type.GetMembers(BindingFlags.Public | BindingFlags.Instance)
.Where(m => !_specialMethods.Contains(m.Name))
.Where(m => (m.MemberType & mTypes) != 0)
.Where(m => !(m is MethodInfo mi && mi.IsSpecialName)) //filter out getters/setters
.ToList();
return members;
}
Looking at this, seems like inside Vita there are also problems with derived interface entities
Hmm...so far I don't have problem with Vita.
Btw, the problem seems only happens to Interface type, adding the fixed like below solve the problem.
public static IList<MemberInfo> GetFieldsProps(this Type type) {
if (type.IsInterface) {
// https://stackoverflow.com/questions/358835/getproperties-to-return-all-properties-for-an-interface-inheritance-hierarchy
return (new Type[] { type })
.Concat(type.GetInterfaces())
.SelectMany(i => i.GetFieldsPropsMethods(withMethods: false)).ToList();
}
return type.GetFieldsPropsMethods(withMethods: false);
}
yes, it is for derived interfaces only, fixing it. With Vita I found a case when derived interfaces fail too, outside GraphQL stuff. Fixing it.
pushed NGraphQL v1.1.1, should be fixed
Tested OK, thanks!
Hi @rivantsov ,
I am getting this error when starting up the GraphQL Server. Any idea?
================= GraphQL Model Errors Detected ========================= Invalid mapping expression for type VIQCore.Community.ICommunity->Community ================= End GraphQL Model Errors ==============================
Full error: