-
-
Notifications
You must be signed in to change notification settings - Fork 14
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
Closed
V6/contrib #326
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
There are no files selected for viewing
124 changes: 63 additions & 61 deletions
124
examples/code-examples/Headless/SkybrudRedirectsExample/SkybrudRedirectsExampleQuery.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
// } | ||
//} |
122 changes: 62 additions & 60 deletions
122
examples/code-examples/Headless/UrlTrackerExample/TrackErrorStatusCodeMutation.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
// }); | ||
// } | ||
|
||
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; } | ||
//} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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