Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix GraphQL Explorer #15319

Merged
merged 6 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,42 +1,37 @@
using GraphQL.Types;
using Microsoft.Extensions.DependencyInjection;

namespace OrchardCore.Apis
namespace OrchardCore.Apis;

public static class ServiceCollectionExtensions
{
public static class ServiceCollectionExtensions
/// <summary>
/// Registers a type describing input arguments.
/// </summary>
/// <typeparam name="TObject"></typeparam>
/// <typeparam name="TObjectType"></typeparam>
/// <param name="services"></param>
public static void AddInputObjectGraphType<TObject, TObjectType>(this IServiceCollection services)
where TObject : class
where TObjectType : InputObjectGraphType<TObject>
{
/// <summary>
/// Registers a type describing input arguments.
/// </summary>
/// <typeparam name="TObject"></typeparam>
/// <typeparam name="TObjectType"></typeparam>
/// <param name="services"></param>
public static void AddInputObjectGraphType<TObject, TObjectType>(this IServiceCollection services)
where TObject : class
where TObjectType : InputObjectGraphType<TObject>
{
// Instances are registered as singletons as their constructor holds the logic to configure the type
// and doesn't need to run every time
services.AddSingleton<TObjectType>();
services.AddSingleton<InputObjectGraphType<TObject>, TObjectType>(s => s.GetRequiredService<TObjectType>());
services.AddSingleton<IInputObjectGraphType, TObjectType>(s => s.GetRequiredService<TObjectType>());
}
services.AddScoped<TObjectType>();
services.AddTransient<InputObjectGraphType<TObject>, TObjectType>(s => s.GetRequiredService<TObjectType>());
services.AddTransient<IInputObjectGraphType, TObjectType>(s => s.GetRequiredService<TObjectType>());
}

/// <summary>
/// Registers a type describing output arguments.
/// </summary>
/// <typeparam name="TInput"></typeparam>
/// <typeparam name="TInputType"></typeparam>
/// <param name="services"></param>
public static void AddObjectGraphType<TInput, TInputType>(this IServiceCollection services)
where TInput : class
where TInputType : ObjectGraphType<TInput>
{
// Instances are registered as singletons as their constructor holds the logic to configure the type
// and doesn't need to run every time
services.AddSingleton<TInputType>();
services.AddSingleton<ObjectGraphType<TInput>, TInputType>(s => s.GetRequiredService<TInputType>());
services.AddSingleton<IObjectGraphType, TInputType>(s => s.GetRequiredService<TInputType>());
}
/// <summary>
/// Registers a type describing output arguments.
/// </summary>
/// <typeparam name="TInput"></typeparam>
/// <typeparam name="TInputType"></typeparam>
/// <param name="services"></param>
public static void AddObjectGraphType<TInput, TInputType>(this IServiceCollection services)
where TInput : class
where TInputType : ObjectGraphType<TInput>
{
services.AddScoped<TInputType>();
services.AddTransient<ObjectGraphType<TInput>, TInputType>(s => s.GetRequiredService<TInputType>());
services.AddTransient<IObjectGraphType, TInputType>(s => s.GetRequiredService<TInputType>());
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using GraphQL;
using GraphQL.Resolvers;
Expand All @@ -15,12 +14,13 @@ namespace OrchardCore.ContentManagement.GraphQL.Queries.Types
{
public class TypedContentTypeBuilder : IContentTypeBuilder
{
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly GraphQLContentOptions _contentOptions;
private static readonly ConcurrentDictionary<string, Type> _partTypes = new();
private static readonly ConcurrentDictionary<string, IObjectGraphType> _partObjectGraphTypes = new();
private static readonly ConcurrentDictionary<string, IInputObjectGraphType> _partInputObjectGraphTypes = new();

private readonly IHttpContextAccessor _httpContextAccessor;
private readonly GraphQLContentOptions _contentOptions;

public TypedContentTypeBuilder(IHttpContextAccessor httpContextAccessor,
IOptions<GraphQLContentOptions> contentOptionsAccessor)
{
Expand All @@ -38,8 +38,8 @@ public void Build(FieldType contentQuery, ContentTypeDefinition contentTypeDefin
return;
}

IEnumerable<IObjectGraphType> queryObjectGraphTypes = null;
IEnumerable<IInputObjectGraphType> queryInputGraphTypes = null;
var queryObjectGraphTypes = serviceProvider.GetServices<IObjectGraphType>();
var queryInputGraphTypes = serviceProvider.GetServices<IInputObjectGraphType>();

foreach (var part in contentTypeDefinition.Parts)
{
Expand All @@ -57,13 +57,13 @@ public void Build(FieldType contentQuery, ContentTypeDefinition contentTypeDefin
}

var partType = _partTypes.GetOrAdd(part.PartDefinition.Name, key => typeActivator.GetTypeActivator(key).Type);
var queryGraphType = _partObjectGraphTypes.GetOrAdd(part.PartDefinition.Name,
partName =>
{
queryObjectGraphTypes ??= serviceProvider.GetService<IEnumerable<IObjectGraphType>>();
return queryObjectGraphTypes?.FirstOrDefault(x => x.GetType().BaseType.GetGenericArguments().First().Name == partName);
}
);
var queryGraphType = _partObjectGraphTypes
.GetOrAdd(part.PartDefinition.Name,
partName =>
{
return queryObjectGraphTypes.FirstOrDefault(x => x.GetType().BaseType.GetGenericArguments().First().Name == partName);
}
);

var collapsePart = _contentOptions.ShouldCollapse(part);

Expand All @@ -73,7 +73,10 @@ public void Build(FieldType contentQuery, ContentTypeDefinition contentTypeDefin
{
foreach (var field in queryGraphType.Fields)
{
if (_contentOptions.ShouldSkip(queryGraphType.GetType(), field.Name)) continue;
if (_contentOptions.ShouldSkip(queryGraphType.GetType(), field.Name))
{
continue;
}

var rolledUpField = new FieldType
{
Expand Down Expand Up @@ -123,8 +126,7 @@ public void Build(FieldType contentQuery, ContentTypeDefinition contentTypeDefin

var inputGraphTypeResolved = _partInputObjectGraphTypes.GetOrAdd(part.PartDefinition.Name, partName =>
{
queryInputGraphTypes ??= serviceProvider.GetService<IEnumerable<IInputObjectGraphType>>();
return queryInputGraphTypes?.FirstOrDefault(x => x.GetType().BaseType.GetGenericArguments().FirstOrDefault()?.Name == part.PartDefinition.Name);
return queryInputGraphTypes.FirstOrDefault(x => x.GetType().BaseType.GetGenericArguments().FirstOrDefault()?.Name == part.PartDefinition.Name);
});

if (inputGraphTypeResolved != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static IServiceCollection AddContentGraphQL(this IServiceCollection servi
{
services.AddSingleton<ISchemaBuilder, ContentItemQuery>();
services.AddSingleton<ISchemaBuilder, ContentTypeQuery>();
services.AddSingleton<ContentItemInterface>();
services.AddTransient<ContentItemInterface>();

services.AddTransient<ContentItemType>();

Expand Down
Loading