graphql-platform icon indicating copy to clipboard operation
graphql-platform copied to clipboard

NotMapped Attribute is ignored when used on IList

Open Lenny4 opened this issue 1 year ago • 2 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Product

Hot Chocolate

Describe the bug

I have this model:

using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
using MyProject.Class.Dto;

namespace SageApi.Model;

public partial class FArticle
{
    public string ArRef { get; set; } = null!;

    [NotMapped]
    public int? Prices { get; set; }

    public static void ConfigureModelBuilder(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<FArticle>(entity =>
        {
            entity.HasIndex(e => e.ArRef, "UKA_F_ARTICLE_AR_Ref").IsUnique();
            entity.Property(e => e.ArRef)
                .HasMaxLength(19)
                .IsUnicode(false)
                .HasColumnName("AR_Ref");
        });
    }
}

My Query:

using SageApi.Model.Sage;

namespace SageApi.Services.GraphQl;
public class Query
{
    [UseOffsetPaging(MaxPageSize = 100, IncludeTotalCount = true, DefaultPageSize = 20)]
    [UseProjection]
    [UseFiltering]
    [UseSorting]
    public IEnumerable<FArticle> GetFArticles([Service] SageDbContext context) =>
        context.FArticles;
}

When I launch my query:

{
    fArticles(
        skip: 0
        take: 1
    ) {
        items {
            arRef
            prices
        }
    }
}

It works fine prices has the value null.

Now I create a Dto:

namespace SageApi.Class.Dto;

public class PriceDto
{
    public short? SomeValue { get; set; }
}

and I change

    [NotMapped]
    public int? Prices { get; set; }

to

    [NotMapped]
    public IList<PriceDto>? Prices { get; set; }

Now when I launch the query

{
    fArticles(
        skip: 0
        take: 1
    ) {
        items {
            arRef
            prices{
                someValue
            }
        }
    }
}

I got error:

The LINQ expression 'p1 => new PriceDto{ AcCategorie = p1.AcCategorie }
' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.

Why does HotChocolate try to get SomeValue in the database when prop typed as IList while I clearly specify the [NotMapped] attribute ?

Steps to reproduce

As described above

Relevant log output

No response

Additional Context?

No response

Version

13.0.0

Lenny4 avatar Jan 19 '24 01:01 Lenny4

Can you also add the code of your updated LINQ query?

huysentruitw avatar Feb 19 '24 19:02 huysentruitw

Hello @huysentruitw ,

Can you also add the code of your updated LINQ query?

I didn't write any LINQ query. The LINQ query is generated by the middlewares UseOffsetPaging UseProjection UseFiltering UseSorting

Here is the SageDbContext:

using Microsoft.EntityFrameworkCore;

namespace SageApi.Model.Sage;

public partial class SageDbContext : DbContext
{
    public SageDbContext()
    {
    }

    public SageDbContext(DbContextOptions<SageDbContext> options)
        : base(options)
    {
    }

    public virtual DbSet<FArticle> FArticles { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        FArticle.ConfigureModelBuilder(modelBuilder);
    }

    partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}

Lenny4 avatar Feb 19 '24 20:02 Lenny4