Open szv opened 2 years ago
I just ran into this same error. Is there a workaround? @michaelstaib
I managed to solve this by preventing projections on inner fields of jsonb types:
public static class JsonBProjectionProviderDescriptorQueryableExtensions
{
public static IProjectionProviderDescriptor AddJsonBHandlers(
this IProjectionProviderDescriptor descriptor) =>
descriptor.RegisterFieldHandler<QueryableJsonBProjectionScalarHandler>();
}
public class QueryableJsonBProjectionScalarHandler
: QueryableProjectionScalarHandler
{
public override bool CanHandle(ISelection selection)
{
var isJsonType = selection.Field.Member?.GetCustomAttributes(true)?.Where(a => a is ColumnAttribute)?.Cast<ColumnAttribute>()?.Any(a => a.TypeName == "jsonb") ?? false;
return selection.Field.Member is not null &&
isJsonType;
}
}
public static class JsonBProjectionsRequestExecutorBuilderExtensions
{
public static IRequestExecutorBuilder AddJsonBProjections(
this IRequestExecutorBuilder builder)
{
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}
return builder.ConfigureSchema(x => x.AddJsonBProjections());
}
}
public static class JsonBProjectionsSchemaBuilderExtensions
{
public static ISchemaBuilder AddJsonBProjections(this ISchemaBuilder builder)
{
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}
return builder.AddConvention<IProjectionConvention>(
new ProjectionConventionExtension(
x => x.AddProviderExtension(
new ProjectionProviderExtension(y => y.AddJsonBHandlers()))));
}
}
It might be nice, for full postgres/ef core support, to have HotChocolate handle this automatically. I haven't contributed anything to HC before.. but I'll see what I can whip up.
@avisra if you want to contribute on this we would appreciate this... get something started and we can fill in the blanks that you have.
FYI this is also an issue when using ef core 7 json (specifically arrays inside the json column). The solution posted above (https://github.com/ChilliCream/graphql-platform/issues/4429#issuecomment-1175747490) works until you have it as part of an union and resolving the entity containing the json column twice, once with the array.
I'll try to create a minimal repro repo later on if it's needed.
Thank you @avisra !
I managed to solve this by preventing projections on inner fields of jsonb types:
public static class JsonBProjectionProviderDescriptorQueryableExtensions { public static IProjectionProviderDescriptor AddJsonBHandlers( this IProjectionProviderDescriptor descriptor) => descriptor.RegisterFieldHandler(); } public class QueryableJsonBProjectionScalarHandler : QueryableProjectionScalarHandler { public override bool CanHandle(ISelection selection) { var isJsonType = selection.Field.Member?.GetCustomAttributes(true)?.Where(a => a is ColumnAttribute)?.Cast<ColumnAttribute>()?.Any(a => a.TypeName == "jsonb") ?? false; return selection.Field.Member is not null && isJsonType; } } public static class JsonBProjectionsRequestExecutorBuilderExtensions { public static IRequestExecutorBuilder AddJsonBProjections( this IRequestExecutorBuilder builder) { if (builder is null) { throw new ArgumentNullException(nameof(builder)); } return builder.ConfigureSchema(x => x.AddJsonBProjections()); } } public static class JsonBProjectionsSchemaBuilderExtensions { public static ISchemaBuilder AddJsonBProjections(this ISchemaBuilder builder) { if (builder is null) { throw new ArgumentNullException(nameof(builder)); } return builder.AddConvention<IProjectionConvention>( new ProjectionConventionExtension( x => x.AddProviderExtension( new ProjectionProviderExtension(y => y.AddJsonBHandlers())))); } }`
Thank you so much it works like a charm!
Is there an existing issue for this?
Describe the bug
An array with the PostgreSQL-JSONB-Datatype does not get mapped correctly.
Further details are shown below...
Steps to reproduce
public class Tag { public string Name { get; set; } = string.Empty; }
public class CompanyDetails { public string Detail1 { get; set; } = string.Empty;
}
... I get the following output ...
... which is the expexted result. The corresponding mapped SQL-statement shows, that the mapping to the JSONB-DB-Type works as expected;
Now the problem
When running the following GraphQL-Query:
... I get the following error ...
When adding the
[UseProjection]
-Attribute to theTags
-Property of the Company-class, I get the following result:... although the content of the tags-column in the database is:
As you can see in the corresponding SQL-statement the tags-column does not get selected:
Do you have any ideas how to make this work or is it a bug of HC?
Kind regards, Sebastian
Relevant log output
No response
Additional Context?
I use
Product
Hot Chocolate
Version
12.2.1