From 1814599ecfce9f41d7eca459cea8887f160c0dff Mon Sep 17 00:00:00 2001 From: Georg von Kries Date: Wed, 15 Jan 2025 20:57:43 +0100 Subject: [PATCH] Fix GraphQL schema creation when using indexed fields. (#17360) --- .../Queries/WhereInputObjectGraphType.cs | 49 +++---------------- .../Types/DynamicContentTypeBuilder.cs | 2 +- .../Types/DynamicPartWhereInputGraphType.cs | 33 ++++--------- 3 files changed, 16 insertions(+), 68 deletions(-) diff --git a/src/OrchardCore/OrchardCore.Apis.GraphQL.Abstractions/Queries/WhereInputObjectGraphType.cs b/src/OrchardCore/OrchardCore.Apis.GraphQL.Abstractions/Queries/WhereInputObjectGraphType.cs index de9259a5187..b92fefeb770 100644 --- a/src/OrchardCore/OrchardCore.Apis.GraphQL.Abstractions/Queries/WhereInputObjectGraphType.cs +++ b/src/OrchardCore/OrchardCore.Apis.GraphQL.Abstractions/Queries/WhereInputObjectGraphType.cs @@ -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> filters, string fieldName, string description) @@ -129,45 +129,8 @@ private void AddFilterFields( { Name = fieldName + filter.Key, Description = filter.Value(S, description), - ResolvedType = resolvedType, + Type = graphType, }); } } - - private readonly Dictionary 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)}."); - } } diff --git a/src/OrchardCore/OrchardCore.ContentManagement.GraphQL/Queries/Types/DynamicContentTypeBuilder.cs b/src/OrchardCore/OrchardCore.ContentManagement.GraphQL/Queries/Types/DynamicContentTypeBuilder.cs index 59e4ceb3092..cf17f7572c5 100644 --- a/src/OrchardCore/OrchardCore.ContentManagement.GraphQL/Queries/Types/DynamicContentTypeBuilder.cs +++ b/src/OrchardCore/OrchardCore.ContentManagement.GraphQL/Queries/Types/DynamicContentTypeBuilder.cs @@ -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>()) + ResolvedType = new DynamicPartWhereInputGraphType(part, schema, contentFieldProviders, serviceProvider.GetRequiredService>()) }; inputGraphType.AddField(field); diff --git a/src/OrchardCore/OrchardCore.ContentManagement.GraphQL/Queries/Types/DynamicPartWhereInputGraphType.cs b/src/OrchardCore/OrchardCore.ContentManagement.GraphQL/Queries/Types/DynamicPartWhereInputGraphType.cs index efe321f6c21..d164fecec00 100644 --- a/src/OrchardCore/OrchardCore.ContentManagement.GraphQL/Queries/Types/DynamicPartWhereInputGraphType.cs +++ b/src/OrchardCore/OrchardCore.ContentManagement.GraphQL/Queries/Types/DynamicPartWhereInputGraphType.cs @@ -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; @@ -8,41 +7,27 @@ namespace OrchardCore.ContentManagement.GraphQL.Queries.Types; public sealed class DynamicPartWhereInputGraphType : WhereInputObjectGraphType { - private ContentTypePartDefinition _part; - public DynamicPartWhereInputGraphType( ContentTypePartDefinition part, + ISchema schema, + IEnumerable contentFieldProviders, IStringLocalizer 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().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); } }