Skip to content

Commit

Permalink
Fix GraphQL schema creation when using indexed fields. (OrchardCMS#17360
Browse files Browse the repository at this point in the history
)
  • Loading branch information
gvkries authored Jan 15, 2025
1 parent 1c8ae25 commit 1814599
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,26 +99,26 @@ public virtual void AddScalarFilterFields(Type graphType, string fieldName, stri

private void AddEqualityFilters(Type graphType, string fieldName, string description)
{
AddFilterFields(CreateGraphType(graphType), EqualityOperators, fieldName, description);
AddFilterFields(graphType, EqualityOperators, fieldName, description);
}

private void AddStringFilters(Type graphType, string fieldName, string description)
{
AddFilterFields(CreateGraphType(graphType), StringComparisonOperators, fieldName, description);
AddFilterFields(graphType, StringComparisonOperators, fieldName, description);
}

private void AddNonStringFilters(Type graphType, string fieldName, string description)
{
AddFilterFields(CreateGraphType(graphType), NonStringValueComparisonOperators, fieldName, description);
AddFilterFields(graphType, NonStringValueComparisonOperators, fieldName, description);
}

private void AddMultiValueFilters(Type graphType, string fieldName, string description)
{
AddFilterFields(CreateGraphType(graphType), MultiValueComparisonOperators, fieldName, description);
AddFilterFields(graphType, MultiValueComparisonOperators, fieldName, description);
}

private void AddFilterFields(
IGraphType resolvedType,
Type graphType,
IDictionary<string, Func<IStringLocalizer, string, string>> filters,
string fieldName,
string description)
Expand All @@ -129,45 +129,8 @@ private void AddFilterFields(
{
Name = fieldName + filter.Key,
Description = filter.Value(S, description),
ResolvedType = resolvedType,
Type = graphType,
});
}
}

private readonly Dictionary<Type, IGraphType> graphTypes = new();

private IGraphType CreateGraphType(Type type)
{
if (type.IsGenericType)
{
var genericDef = type.GetGenericTypeDefinition();
if (genericDef == typeof(ListGraphType<>))
{
var innerType = type.GetGenericArguments()[0];

return new ListGraphType(CreateGraphType(innerType));
}

if (genericDef == typeof(NonNullGraphType<>))
{
var innerType = type.GetGenericArguments()[0];

return new NonNullGraphType(CreateGraphType(innerType));
}
}

if (typeof(ScalarGraphType).IsAssignableFrom(type))
{
if (!graphTypes.TryGetValue(type, out var graphType))
{
graphType = (IGraphType)Activator.CreateInstance(type);

graphTypes[type] = graphType;
}

return graphType;
}

throw new InvalidOperationException($"{type.Name} is not a valid {nameof(ScalarGraphType)}.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ protected void BuildInternal(ISchema schema, ContentTypeDefinition contentTypeDe
Name = partFieldName,
Description = S["Represents a {0}.", part.PartDefinition.Name],
Type = typeof(DynamicPartWhereInputGraphType),
ResolvedType = new DynamicPartWhereInputGraphType(part, serviceProvider.GetRequiredService<IStringLocalizer<DynamicPartWhereInputGraphType>>())
ResolvedType = new DynamicPartWhereInputGraphType(part, schema, contentFieldProviders, serviceProvider.GetRequiredService<IStringLocalizer<DynamicPartWhereInputGraphType>>())
};

inputGraphType.AddField(field);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using GraphQL.Types;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;
using OrchardCore.Apis.GraphQL.Queries;
using OrchardCore.ContentManagement.Metadata.Models;
Expand All @@ -8,41 +7,27 @@ namespace OrchardCore.ContentManagement.GraphQL.Queries.Types;

public sealed class DynamicPartWhereInputGraphType : WhereInputObjectGraphType<ContentPart>
{
private ContentTypePartDefinition _part;

public DynamicPartWhereInputGraphType(
ContentTypePartDefinition part,
ISchema schema,
IEnumerable<IContentFieldProvider> contentFieldProviders,
IStringLocalizer<DynamicPartWhereInputGraphType> stringLocalizer)
: base(stringLocalizer)
{
Name = $"{part.Name}WhereInput";
_part = part;
}

public override void Initialize(ISchema schema)
{
if (schema is IServiceProvider serviceProvider)
foreach (var field in part.PartDefinition.Fields)
{
var contentFieldProviders = serviceProvider.GetServices<IContentFieldProvider>().ToList();

foreach (var field in _part.PartDefinition.Fields)
foreach (var fieldProvider in contentFieldProviders)
{
foreach (var fieldProvider in contentFieldProviders)
{
var fieldType = fieldProvider.GetField(schema, field, _part.Name);
var fieldType = fieldProvider.GetField(schema, field, part.Name);

if (fieldType != null)
{
AddScalarFilterFields(fieldType.Type, fieldType.Name, fieldType.Description);
break;
}
if (fieldType != null)
{
AddScalarFilterFields(fieldType.Type, fieldType.Name, fieldType.Description);
break;
}
}
}

// Part is not required here anymore, do not keep it alive.
_part = null;

base.Initialize(schema);
}
}

0 comments on commit 1814599

Please sign in to comment.