I'm using HotChocolate 13. Here is my model FArticle
(the field ArRef
is my id):
public partial class FArticle
{
public string ArRef { get; set; } = null!;
public string? SomeValue { 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.SomeValue)
.HasMaxLength(69)
.IsUnicode(false)
.HasColumnName("SomeValue");
entity.Property(e => e.ArRef)
.HasMaxLength(19)
.IsUnicode(false)
.HasColumnName("AR_Ref");
});
}
}
My Query
:
public class Query
{
[UseFArticleMapping]
[UseOffsetPaging(MaxPageSize = 100, IncludeTotalCount = true, DefaultPageSize = 20)]
[UseProjection]
[UseFiltering]
[UseSorting]
public IEnumerable<FArticle> GetFArticles([Service] DbContext context) =>
context.FArticles;
}
As you can see I added my own middleware UseFArticleMapping
.
Now when I launch the query:
{
fArticles(
skip: 0
take: 1
) {
items {
someValue
}
}
}
I can access my list of FArticle
with my middleware UseFArticleMapping
:
public class UseFArticleMappingAttribute : ObjectFieldDescriptorAttribute
{
public UseFArticleMappingAttribute([CallerLineNumber] int order = 0)
{
Order = order;
}
protected override void OnConfigure(
IDescriptorContext context,
IObjectFieldDescriptor descriptor,
MemberInfo member
)
{
descriptor.Use(next => async middlewareContext =>
{
await next(middlewareContext);
object? result = middlewareContext.Result;
// https://stackoverflow.com/a/68459545/6824121
if (result is CollectionSegment<FArticle> fArticles)
{
foreach (var fArticle in fArticles.Items)
{
fArticle.ArRef // fArticle.ArRef is null
}
}
});
}
}
But as I didn't specify the arRef
in the query (only someValue
) fArticle.ArRef
returns null
.
How can I tell HotChocolate to load ArRef
without adding arRef
in my query so I can access it in my middleware ?
You need to use IsProjected
public partial class FArticle
{
[IsProjected(true)]
public string ArRef { get; set; } = null!;
}