Skip to content

Commit 1814599

Browse files
authored
Fix GraphQL schema creation when using indexed fields. (OrchardCMS#17360)
1 parent 1c8ae25 commit 1814599

File tree

3 files changed

+16
-68
lines changed

3 files changed

+16
-68
lines changed

src/OrchardCore/OrchardCore.Apis.GraphQL.Abstractions/Queries/WhereInputObjectGraphType.cs

Lines changed: 6 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -99,26 +99,26 @@ public virtual void AddScalarFilterFields(Type graphType, string fieldName, stri
9999

100100
private void AddEqualityFilters(Type graphType, string fieldName, string description)
101101
{
102-
AddFilterFields(CreateGraphType(graphType), EqualityOperators, fieldName, description);
102+
AddFilterFields(graphType, EqualityOperators, fieldName, description);
103103
}
104104

105105
private void AddStringFilters(Type graphType, string fieldName, string description)
106106
{
107-
AddFilterFields(CreateGraphType(graphType), StringComparisonOperators, fieldName, description);
107+
AddFilterFields(graphType, StringComparisonOperators, fieldName, description);
108108
}
109109

110110
private void AddNonStringFilters(Type graphType, string fieldName, string description)
111111
{
112-
AddFilterFields(CreateGraphType(graphType), NonStringValueComparisonOperators, fieldName, description);
112+
AddFilterFields(graphType, NonStringValueComparisonOperators, fieldName, description);
113113
}
114114

115115
private void AddMultiValueFilters(Type graphType, string fieldName, string description)
116116
{
117-
AddFilterFields(CreateGraphType(graphType), MultiValueComparisonOperators, fieldName, description);
117+
AddFilterFields(graphType, MultiValueComparisonOperators, fieldName, description);
118118
}
119119

120120
private void AddFilterFields(
121-
IGraphType resolvedType,
121+
Type graphType,
122122
IDictionary<string, Func<IStringLocalizer, string, string>> filters,
123123
string fieldName,
124124
string description)
@@ -129,45 +129,8 @@ private void AddFilterFields(
129129
{
130130
Name = fieldName + filter.Key,
131131
Description = filter.Value(S, description),
132-
ResolvedType = resolvedType,
132+
Type = graphType,
133133
});
134134
}
135135
}
136-
137-
private readonly Dictionary<Type, IGraphType> graphTypes = new();
138-
139-
private IGraphType CreateGraphType(Type type)
140-
{
141-
if (type.IsGenericType)
142-
{
143-
var genericDef = type.GetGenericTypeDefinition();
144-
if (genericDef == typeof(ListGraphType<>))
145-
{
146-
var innerType = type.GetGenericArguments()[0];
147-
148-
return new ListGraphType(CreateGraphType(innerType));
149-
}
150-
151-
if (genericDef == typeof(NonNullGraphType<>))
152-
{
153-
var innerType = type.GetGenericArguments()[0];
154-
155-
return new NonNullGraphType(CreateGraphType(innerType));
156-
}
157-
}
158-
159-
if (typeof(ScalarGraphType).IsAssignableFrom(type))
160-
{
161-
if (!graphTypes.TryGetValue(type, out var graphType))
162-
{
163-
graphType = (IGraphType)Activator.CreateInstance(type);
164-
165-
graphTypes[type] = graphType;
166-
}
167-
168-
return graphType;
169-
}
170-
171-
throw new InvalidOperationException($"{type.Name} is not a valid {nameof(ScalarGraphType)}.");
172-
}
173136
}

src/OrchardCore/OrchardCore.ContentManagement.GraphQL/Queries/Types/DynamicContentTypeBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ protected void BuildInternal(ISchema schema, ContentTypeDefinition contentTypeDe
172172
Name = partFieldName,
173173
Description = S["Represents a {0}.", part.PartDefinition.Name],
174174
Type = typeof(DynamicPartWhereInputGraphType),
175-
ResolvedType = new DynamicPartWhereInputGraphType(part, serviceProvider.GetRequiredService<IStringLocalizer<DynamicPartWhereInputGraphType>>())
175+
ResolvedType = new DynamicPartWhereInputGraphType(part, schema, contentFieldProviders, serviceProvider.GetRequiredService<IStringLocalizer<DynamicPartWhereInputGraphType>>())
176176
};
177177

178178
inputGraphType.AddField(field);
Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using GraphQL.Types;
2-
using Microsoft.Extensions.DependencyInjection;
32
using Microsoft.Extensions.Localization;
43
using OrchardCore.Apis.GraphQL.Queries;
54
using OrchardCore.ContentManagement.Metadata.Models;
@@ -8,41 +7,27 @@ namespace OrchardCore.ContentManagement.GraphQL.Queries.Types;
87

98
public sealed class DynamicPartWhereInputGraphType : WhereInputObjectGraphType<ContentPart>
109
{
11-
private ContentTypePartDefinition _part;
12-
1310
public DynamicPartWhereInputGraphType(
1411
ContentTypePartDefinition part,
12+
ISchema schema,
13+
IEnumerable<IContentFieldProvider> contentFieldProviders,
1514
IStringLocalizer<DynamicPartWhereInputGraphType> stringLocalizer)
1615
: base(stringLocalizer)
1716
{
1817
Name = $"{part.Name}WhereInput";
19-
_part = part;
20-
}
2118

22-
public override void Initialize(ISchema schema)
23-
{
24-
if (schema is IServiceProvider serviceProvider)
19+
foreach (var field in part.PartDefinition.Fields)
2520
{
26-
var contentFieldProviders = serviceProvider.GetServices<IContentFieldProvider>().ToList();
27-
28-
foreach (var field in _part.PartDefinition.Fields)
21+
foreach (var fieldProvider in contentFieldProviders)
2922
{
30-
foreach (var fieldProvider in contentFieldProviders)
31-
{
32-
var fieldType = fieldProvider.GetField(schema, field, _part.Name);
23+
var fieldType = fieldProvider.GetField(schema, field, part.Name);
3324

34-
if (fieldType != null)
35-
{
36-
AddScalarFilterFields(fieldType.Type, fieldType.Name, fieldType.Description);
37-
break;
38-
}
25+
if (fieldType != null)
26+
{
27+
AddScalarFilterFields(fieldType.Type, fieldType.Name, fieldType.Description);
28+
break;
3929
}
4030
}
4131
}
42-
43-
// Part is not required here anymore, do not keep it alive.
44-
_part = null;
45-
46-
base.Initialize(schema);
4732
}
4833
}

0 commit comments

Comments
 (0)