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

V6/contrib #326

Closed
wants to merge 3 commits into from
Closed
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
@@ -1,61 +1,63 @@
using HotChocolate;
using HotChocolate.Resolvers;
using HotChocolate.Types;
using Nikcio.UHeadless;
using Nikcio.UHeadless.ContentItems;
using Nikcio.UHeadless.Defaults.ContentItems;
using Skybrud.Umbraco.Redirects.Models;
using Skybrud.Umbraco.Redirects.Services;

namespace Code.Examples.Headless.SkybrudRedirectsExample;

[ExtendObjectType(typeof(HotChocolateQueryObject))]
public class SkybrudRedirectsExampleQuery : ContentByRouteQuery
{
[GraphQLName("skybrudRedirectsExampleQuery")]
public override Task<ContentItem?> ContentByRouteAsync(
IResolverContext resolverContext,
[GraphQLDescription("The route to fetch. Example '/da/frontpage/'.")] string route,
[GraphQLDescription("The base url for the request. Example: 'https://localhost:4000'. Default is the current domain")] string baseUrl = "",
[GraphQLDescription("The context of the request.")] QueryContext? inContext = null)
{
return base.ContentByRouteAsync(resolverContext, route, baseUrl, inContext);
}

protected override async Task<ContentItem?> CreateContentItemFromRouteAsync(IResolverContext resolverContext, string route, string baseUrl)
{
ArgumentNullException.ThrowIfNull(resolverContext);
ArgumentNullException.ThrowIfNull(baseUrl);

IRedirectsService redirectService = resolverContext.Service<IRedirectsService>();

var uri = new Uri($"{baseUrl.TrimEnd('/')}{route}");
IRedirect? redirect = redirectService.GetRedirectByUri(uri);

if (redirect != null)
{
IContentItemRepository<ContentItem> contentItemRepository = resolverContext.Service<IContentItemRepository<ContentItem>>();

string redirectUrl = redirect.Destination.FullUrl;

if (redirect.ForwardQueryString)
{
redirectUrl = redirectUrl.TrimEnd('/') + uri.Query;
}

return contentItemRepository.GetContentItem(new ContentItem.CreateCommand()
{
PublishedContent = null,
ResolverContext = resolverContext,
Redirect = new()
{
IsPermanent = redirect.IsPermanent,
RedirectUrl = redirectUrl,
},
StatusCode = redirect.IsPermanent ? StatusCodes.Status301MovedPermanently : StatusCodes.Status307TemporaryRedirect,
});
}

return await base.CreateContentItemFromRouteAsync(resolverContext, route, baseUrl).ConfigureAwait(false);
}
}
//using HotChocolate;
//using HotChocolate.Resolvers;
//using HotChocolate.Types;
//using Nikcio.UHeadless;
//using Nikcio.UHeadless.ContentItems;
//using Nikcio.UHeadless.Defaults.ContentItems;
//using Skybrud.Umbraco.Redirects.Models;
//using Skybrud.Umbraco.Redirects.Services;

//namespace Code.Examples.Headless.SkybrudRedirectsExample;

// TODO RE-IMPLEMENT

//[ExtendObjectType(typeof(HotChocolateQueryObject))]
//public class SkybrudRedirectsExampleQuery : ContentByRouteQuery
//{
// [GraphQLName("skybrudRedirectsExampleQuery")]
// public override Task<ContentItem?> ContentByRouteAsync(
// IResolverContext resolverContext,
// [GraphQLDescription("The route to fetch. Example '/da/frontpage/'.")] string route,
// [GraphQLDescription("The base url for the request. Example: 'https://localhost:4000'. Default is the current domain")] string baseUrl = "",
// [GraphQLDescription("The context of the request.")] QueryContext? inContext = null)
// {
// return base.ContentByRouteAsync(resolverContext, route, baseUrl, inContext);
// }

// protected override async Task<ContentItem?> CreateContentItemFromRouteAsync(IResolverContext resolverContext, string route, string baseUrl)
// {
// ArgumentNullException.ThrowIfNull(resolverContext);
// ArgumentNullException.ThrowIfNull(baseUrl);

// IRedirectsService redirectService = resolverContext.Service<IRedirectsService>();

// var uri = new Uri($"{baseUrl.TrimEnd('/')}{route}");
// IRedirect? redirect = redirectService.GetRedirectByUri(uri);

// if (redirect != null)
// {
// IContentItemRepository<ContentItem> contentItemRepository = resolverContext.Service<IContentItemRepository<ContentItem>>();

// string redirectUrl = redirect.Destination.FullUrl;

// if (redirect.ForwardQueryString)
// {
// redirectUrl = redirectUrl.TrimEnd('/') + uri.Query;
// }

// return contentItemRepository.GetContentItem(new ContentItem.CreateCommand()
// {
// PublishedContent = null,
// ResolverContext = resolverContext,
// Redirect = new()
// {
// IsPermanent = redirect.IsPermanent,
// RedirectUrl = redirectUrl,
// },
// StatusCode = redirect.IsPermanent ? StatusCodes.Status301MovedPermanently : StatusCodes.Status307TemporaryRedirect,
// });
// }

// return await base.CreateContentItemFromRouteAsync(resolverContext, route, baseUrl).ConfigureAwait(false);
// }
//}
Original file line number Diff line number Diff line change
@@ -1,72 +1,74 @@
using HotChocolate;
using HotChocolate.Resolvers;
using HotChocolate.Types;
using Nikcio.UHeadless;
using Nikcio.UHeadless.Defaults.Authorization;
using UrlTracker.Middleware.Background;
//using HotChocolate;
//using HotChocolate.Resolvers;
//using HotChocolate.Types;
//using Nikcio.UHeadless;
//using Nikcio.UHeadless.Defaults.Authorization;
//using UrlTracker.Middleware.Background;

namespace Code.Examples.Headless.UrlTrackerExample;
//namespace Code.Examples.Headless.UrlTrackerExample;

[ExtendObjectType(typeof(HotChocolateMutationObject))]
public class TrackErrorStatusCodeMutation : IGraphQLMutation
{
public const string PolicyName = "TrackErrorStatusCode";
// TODO RE-IMPLEMENT

public const string ClaimValue = "track.error.statuscode.mutation";
//[ExtendObjectType(typeof(HotChocolateMutationObject))]
//public class TrackErrorStatusCodeMutation : IGraphQLMutation
//{
// public const string PolicyName = "TrackErrorStatusCode";

[GraphQLIgnore]
public virtual void ApplyConfiguration(UHeadlessOptions options)
{
ArgumentNullException.ThrowIfNull(options);
// public const string ClaimValue = "track.error.statuscode.mutation";

options.UmbracoBuilder.Services.AddAuthorizationBuilder().AddPolicy(PolicyName, policy =>
{
if (options.DisableAuthorization)
{
policy.AddRequirements(new AlwaysAllowAuthoriaztionRequirement());
return;
}
// [GraphQLIgnore]
// public virtual void ApplyConfiguration(UHeadlessOptions options)
// {
// ArgumentNullException.ThrowIfNull(options);

policy.AddAuthenticationSchemes(DefaultAuthenticationSchemes.UHeadless);
// options.UmbracoBuilder.Services.AddAuthorizationBuilder().AddPolicy(PolicyName, policy =>
// {
// if (options.DisableAuthorization)
// {
// policy.AddRequirements(new AlwaysAllowAuthoriaztionRequirement());
// return;
// }

policy.RequireAuthenticatedUser();
// policy.AddAuthenticationSchemes(DefaultAuthenticationSchemes.UHeadless);

policy.RequireClaim(DefaultClaims.UHeadlessScope, ClaimValue);
});
}
// policy.RequireAuthenticatedUser();

public async Task<TrackErrorStatusCodeResponse> TrackErrorStatusCodeAsync(
IResolverContext resolverContext,
[GraphQLDescription("Status code of the client error.")] int statusCode,
[GraphQLDescription("The URL that generated the client error.")] string url,
[GraphQLDescription("The time and date at which the client error was generated")] DateTime timestamp,
[GraphQLDescription("The URL from which the current URL is requested")] string? referrer)
{
ArgumentNullException.ThrowIfNull(resolverContext);
// policy.RequireClaim(DefaultClaims.UHeadlessScope, ClaimValue);
Comment on lines -38 to -45

Choose a reason for hiding this comment

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

✅ No longer an issue: Excess Number of Function Arguments
TrackErrorStatusCodeAsync is no longer above the threshold for number of arguments

// });
// }

ILogger<TrackErrorStatusCodeMutation> logger = resolverContext.Service<ILogger<TrackErrorStatusCodeMutation>>();
switch (statusCode)
{
case StatusCodes.Status404NotFound:
IClientErrorProcessorQueue clientErrorProcessorQueue = resolverContext.Service<IClientErrorProcessorQueue>();
await clientErrorProcessorQueue.WriteAsync(new ClientErrorProcessorItem(url, timestamp, referrer)).ConfigureAwait(false);
break;
case StatusCodes.Status500InternalServerError:
logger.LogError("Internal server error occurred at {Timestamp} for URL {Url} with referrer {Referrer}", timestamp, url, referrer);
break;
default:
logger.LogWarning("Client error occurred at {Timestamp} for URL {Url} with referrer {Referrer} and status code {StatusCode}", timestamp, url, referrer, statusCode);
break;
}
// public async Task<TrackErrorStatusCodeResponse> TrackErrorStatusCodeAsync(
// IResolverContext resolverContext,
// [GraphQLDescription("Status code of the client error.")] int statusCode,
// [GraphQLDescription("The URL that generated the client error.")] string url,
// [GraphQLDescription("The time and date at which the client error was generated")] DateTime timestamp,
// [GraphQLDescription("The URL from which the current URL is requested")] string? referrer)
// {
// ArgumentNullException.ThrowIfNull(resolverContext);

// ILogger<TrackErrorStatusCodeMutation> logger = resolverContext.Service<ILogger<TrackErrorStatusCodeMutation>>();
// switch (statusCode)
// {
// case StatusCodes.Status404NotFound:
// IClientErrorProcessorQueue clientErrorProcessorQueue = resolverContext.Service<IClientErrorProcessorQueue>();
// await clientErrorProcessorQueue.WriteAsync(new ClientErrorProcessorItem(url, timestamp, referrer)).ConfigureAwait(false);
// break;
// case StatusCodes.Status500InternalServerError:
// logger.LogError("Internal server error occurred at {Timestamp} for URL {Url} with referrer {Referrer}", timestamp, url, referrer);
// break;
// default:
// logger.LogWarning("Client error occurred at {Timestamp} for URL {Url} with referrer {Referrer} and status code {StatusCode}", timestamp, url, referrer, statusCode);
// break;
// }

return new TrackErrorStatusCodeResponse
{
Success = true
};
}
}
// return new TrackErrorStatusCodeResponse
// {
// Success = true
// };
// }
//}

public sealed class TrackErrorStatusCodeResponse
{
public required bool Success { get; init; }
}
//public sealed class TrackErrorStatusCodeResponse
//{
// public required bool Success { get; init; }
//}
Loading
Loading