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

Collapsed fields collision in graphql #13927

Merged
Merged
Show file tree
Hide file tree
Changes from 12 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
Expand Up @@ -86,7 +86,7 @@ public class ContentFieldsProvider : IContentFieldProvider
}
};

public FieldType GetField(ContentPartFieldDefinition field)
public FieldType GetField(ContentPartFieldDefinition field, string namedPartTechnicalName, string customFieldName)
{
if (!_contentFieldTypeMappings.TryGetValue(field.FieldDefinition.Name, out var value))
{
Expand All @@ -96,13 +96,13 @@ public FieldType GetField(ContentPartFieldDefinition field)
var fieldDescriptor = value;
return new FieldType
{
Name = field.Name,
Name = customFieldName ?? field.Name,
Description = fieldDescriptor.Description,
Type = fieldDescriptor.FieldType,
Resolver = new FuncFieldResolver<ContentElement, object>(context =>
{
// Check if part has been collapsed by trying to get the parent part.
var contentPart = context.Source.Get(typeof(ContentPart), field.PartDefinition.Name);
var contentPart = context.Source.Get(typeof(ContentPart), namedPartTechnicalName);

// Part is not collapsed, access field directly.
contentPart ??= context.Source;
Expand All @@ -116,6 +116,11 @@ public FieldType GetField(ContentPartFieldDefinition field)
};
}

public bool HasField(ContentPartFieldDefinition field)
{
return _contentFieldTypeMappings.ContainsKey(field.FieldDefinition.Name);
}
lampersky marked this conversation as resolved.
Show resolved Hide resolved

private sealed class FieldTypeDescriptor
{
public string Description { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,23 @@ public ObjectGraphTypeFieldProvider(IHttpContextAccessor httpContextAccessor)
_httpContextAccessor = httpContextAccessor;
}

public FieldType GetField(ContentPartFieldDefinition field)
public FieldType GetField(ContentPartFieldDefinition field, string namedPartTechnicalName, string customFieldName)
{
var serviceProvider = _httpContextAccessor.HttpContext.RequestServices;
var queryGraphType = _partObjectGraphTypes.GetOrAdd(field.FieldDefinition.Name,
partName => serviceProvider.GetService<IEnumerable<IObjectGraphType>>()?
.FirstOrDefault(x => x.GetType().BaseType.GetGenericArguments().First().Name == partName)
);
var queryGraphType = GetObjectGraphType(field);

if (queryGraphType != null)
{
return new FieldType
{
Name = field.Name,
Name = customFieldName ?? field.Name,
Description = field.FieldDefinition.Name,
Type = queryGraphType.GetType(),
Resolver = new FuncFieldResolver<ContentElement, ContentElement>(context =>
{
var typeToResolve = context.FieldDefinition.ResolvedType.GetType().BaseType.GetGenericArguments().First();

// Check if part has been collapsed by trying to get the parent part.
var contentPart = context.Source.Get(typeof(ContentPart), field.PartDefinition.Name);
var contentPart = context.Source.Get(typeof(ContentPart), namedPartTechnicalName);

// Part is not collapsed, access field directly.
contentPart ??= context.Source;
Expand All @@ -54,5 +50,20 @@ public FieldType GetField(ContentPartFieldDefinition field)

return null;
}

private IObjectGraphType GetObjectGraphType(ContentPartFieldDefinition field)
{
var serviceProvider = _httpContextAccessor.HttpContext.RequestServices;

return _partObjectGraphTypes.GetOrAdd(field.FieldDefinition.Name,
partName => serviceProvider.GetService<IEnumerable<IObjectGraphType>>()?
.FirstOrDefault(x => x.GetType().BaseType.GetGenericArguments().First().Name == partName)
);
}

public bool HasField(ContentPartFieldDefinition field)
{
return GetObjectGraphType(field) != null;
}
hishamco marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
@@ -1,35 +1,44 @@
using System.Threading.Tasks;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Options;
using OrchardCore.ContentManagement.GraphQL.Options;
using OrchardCore.ContentManagement.GraphQL.Settings;
using OrchardCore.ContentManagement.Metadata.Models;
using OrchardCore.ContentTypes.Editors;
using OrchardCore.ContentTypes.GraphQL.ViewModels;
using OrchardCore.DisplayManagement.ModelBinding;
using OrchardCore.DisplayManagement.Views;

namespace OrchardCore.ContentTypes.GraphQL.Drivers
{
public class GraphQLContentTypePartSettingsDriver : ContentTypePartDefinitionDisplayDriver
{
private readonly GraphQLContentOptions _contentOptions;
private readonly IStringLocalizer S;

public GraphQLContentTypePartSettingsDriver(IOptions<GraphQLContentOptions> optionsAccessor)
public GraphQLContentTypePartSettingsDriver(IOptions<GraphQLContentOptions> optionsAccessor, IStringLocalizer<GraphQLContentTypePartSettingsDriver> stringLocalizer)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see it used.

{
_contentOptions = optionsAccessor.Value;
S = stringLocalizer;
}

public override IDisplayResult Edit(ContentTypePartDefinition contentTypePartDefinition)
public override IDisplayResult Edit(ContentTypePartDefinition contentTypePartDefinition, IUpdateModel updater)
{
if (contentTypePartDefinition.ContentTypeDefinition.Name == contentTypePartDefinition.PartDefinition.Name)
{
return null;
}

return Initialize<GraphQLContentTypePartSettingsViewModel>("GraphQLContentTypePartSettings_Edit", model =>
return Initialize<GraphQLContentTypePartSettingsViewModel>("GraphQLContentTypePartSettings_Edit", async model =>
{
model.Definition = contentTypePartDefinition;
model.Options = _contentOptions;
model.Settings = contentTypePartDefinition.GetSettings<GraphQLContentTypePartSettings>();

if (!updater.ModelState.IsValid)
{
await updater.TryUpdateModelAsync(model, Prefix, x => x.Settings);
}
}).Location("Content");
}

Expand All @@ -42,11 +51,11 @@ public override async Task<IDisplayResult> UpdateAsync(ContentTypePartDefinition

var model = new GraphQLContentTypePartSettingsViewModel();

await context.Updater.TryUpdateModelAsync(model, Prefix);
await context.Updater.TryUpdateModelAsync(model, Prefix, m => m.Settings);

context.Builder.WithSettings(model.Settings);

return Edit(contentTypePartDefinition);
return Edit(contentTypePartDefinition, context.Updater);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,52 @@
var hidden = hiddenByDefault ? true : settings.Hidden;
}

<div class="mb-3">
<div class="form-check">
<input type="checkbox" class="form-check-input" asp-for="Settings.Collapse" checked="@collapsed" asp-is-disabled="@collapsedByDefault" />
<label class="form-check-label" asp-for="Settings.Collapse">@T["Collapse"]</label>
@if (collapsedByDefault)
{
<span class="hint">@T["NB. Setting is collapsed by default, and cannot be overwritten."]</span>
}
else
{
<span class="hint">@T["Check to collapse fields in the GraphQL schema."]</span>
}
</div>
<div class="form-check">
<input type="checkbox" class="form-check-input" asp-for="Settings.Hidden" checked="@hidden" asp-is-disabled="@hiddenByDefault" />
<label class="form-check-label" asp-for="Settings.Hidden">@T["Hide"]</label>
@if (hiddenByDefault)
{
<span class="hint">@T["Setting is hidden by default, and cannot be overwritten."]</span>
}
else
{
<span class="hint">@T["Check to hide part from the GraphQL schema."]</span>
}
</div>

<div class="form-check">
<input type="checkbox" class="form-check-input" asp-for="Settings.Collapse" checked="@collapsed" asp-is-disabled="@collapsedByDefault" />
<label class="form-check-label" asp-for="Settings.Collapse">@T["Collapse"]</label>
@if (collapsedByDefault)
{
<span class="hint">@T["NB. Setting is collapsed by default, and cannot be overwritten."]</span>
}
else
{
<span class="hint">@T["Check to collapse fields in the GraphQL schema."]</span>
}
</div>

<div class="form-check mb-3 mt-3">
<input type="checkbox" class="form-check-input" asp-for="Settings.PreventFieldNameCollision" checked="@settings.PreventFieldNameCollision" />
<label class="form-check-label" asp-for="Settings.PreventFieldNameCollision">@T["Prevent Field Name Collision"]</label>
<span class="hint">@T["Check to prevent fields' names collisions in the GraphQL schema when they are collapsed."]</span>
</div>

<div class="form-check mb-3">
<input type="checkbox" class="form-check-input" asp-for="Settings.Hidden" checked="@hidden" asp-is-disabled="@hiddenByDefault" />
<label class="form-check-label" asp-for="Settings.Hidden">@T["Hide"]</label>
@if (hiddenByDefault)
{
<span class="hint">@T["Setting is hidden by default, and cannot be overwritten."]</span>
}
else
{
<span class="hint">@T["Check to hide part from the GraphQL schema."]</span>
}
</div>

<script at="Foot">
(function () {
const collapseCheckbox = document.getElementById("@(Html.IdFor(x => x.Settings.Collapse))");
const preventFieldNameCollisionCheckbox = document.getElementById("@(Html.IdFor(x => x.Settings.PreventFieldNameCollision))");
const setFieldVisibility = (checked) => {
preventFieldNameCollisionCheckbox.parentElement.style.display = checked ? 'block' : 'none';
if (!checked) {
preventFieldNameCollisionCheckbox.checked = false;
}
};
collapseCheckbox.addEventListener('change', (e) => {
setFieldVisibility(e.target.checked);
});
setFieldVisibility(@(collapsed.ToString().ToLower()));
lampersky marked this conversation as resolved.
Show resolved Hide resolved
})();
</script>
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using GraphQL;
using GraphQL.Types;
using OrchardCore.ContentManagement.GraphQL.Settings;
using OrchardCore.ContentManagement.Metadata.Models;
Expand Down Expand Up @@ -221,5 +222,17 @@ public bool IsHiddenByDefault(ContentTypePartDefinition definition)

return false;
}

internal string GetFieldName(ContentTypePartDefinition definition, string partName, string fieldName)
{
var settings = definition.GetSettings<GraphQLContentTypePartSettings>();

if (settings.PreventFieldNameCollision)
{
return $"{partName.ToFieldName()}{fieldName.ToPascalCase()}";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No separators?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sebastienros yes, no separator, partName.ToFieldName() is producing camelCase, fieldName.ToPascalCase() is producing PascalCase when concatenated you will get some user friendly fields

example:
partName: MyCustomPart
fieldName: MyCustomField
will produce: myCustomMyCustomField graphQL field

image

lampersky marked this conversation as resolved.
Show resolved Hide resolved
}

return fieldName;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void Build(FieldType contentQuery, ContentTypeDefinition contentTypeDefin
continue;
}

if (!(part.PartDefinition.Fields.Any(field => contentFieldProviders.Any(fieldProvider => fieldProvider.GetField(field) != null))))
if (!(part.PartDefinition.Fields.Any(field => contentFieldProviders.Any(fieldProvider => fieldProvider.HasField(field)))))
{
continue;
}
Expand All @@ -64,7 +64,9 @@ public void Build(FieldType contentQuery, ContentTypeDefinition contentTypeDefin
{
foreach (var fieldProvider in contentFieldProviders)
{
var fieldType = fieldProvider.GetField(field);
var customFieldName = _contentOptions.GetFieldName(part, part.Name, field.Name);

var fieldType = fieldProvider.GetField(field, part.Name, customFieldName);

if (fieldType != null)
{
Expand All @@ -90,7 +92,7 @@ public void Build(FieldType contentQuery, ContentTypeDefinition contentTypeDefin
{
foreach (var fieldProvider in contentFieldProviders)
{
var contentFieldType = fieldProvider.GetField(field);
var contentFieldType = fieldProvider.GetField(field, part.Name);

if (contentFieldType != null && !contentItemType.HasField(contentFieldType.Name))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public DynamicPartGraphType(IHttpContextAccessor httpContextAccessor, ContentTyp
{
foreach (var fieldProvider in contentFieldProviders)
{
var fieldType = fieldProvider.GetField(field);
var fieldType = fieldProvider.GetField(field, part.Name);
if (fieldType != null)
{
AddField(fieldType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ namespace OrchardCore.ContentManagement.GraphQL.Queries.Types
{
public interface IContentFieldProvider
{
FieldType GetField(ContentPartFieldDefinition field);
FieldType GetField(ContentPartFieldDefinition field, string namedPartTechnicalName, string customFieldName = null);

bool HasField(ContentPartFieldDefinition field);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ public class GraphQLContentTypePartSettings
public bool Collapse { get; set; }

public bool Hidden { get; set; }

public bool PreventFieldNameCollision { get; set; }
}
}
Loading