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

Hot Chocolate 14 Migration #2348

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
17 changes: 15 additions & 2 deletions src/Core/Authorization/GraphQLAuthorizationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Diagnostics.CodeAnalysis;
using System.Security.Claims;
using HotChocolate.AspNetCore.Authorization;
using HotChocolate.Authorization;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

With Hot Chocolate we have abstracted the authorization hooks to allow users to more easily bring in new. providers without the need to rely on ASP.NET core APIs

using HotChocolate.Resolvers;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Primitives;
Expand All @@ -15,7 +16,7 @@ namespace Azure.DataApiBuilder.Core.Authorization;
/// The changes in this custom handler enable fetching the ClientRoleHeader value defined within requests (value of X-MS-API-ROLE) HTTP Header.
/// Then, using that value to check the header value against the authenticated ClientPrincipal roles.
/// </summary>
public class GraphQLAuthorizationHandler : HotChocolate.AspNetCore.Authorization.IAuthorizationHandler
public class GraphQLAuthorizationHandler : IAuthorizationHandler
{
/// <summary>
/// Authorize access to field based on contents of @authorize directive.
Expand All @@ -31,7 +32,10 @@ public class GraphQLAuthorizationHandler : HotChocolate.AspNetCore.Authorization
/// Returns a value indicating if the current session is authorized to
/// access the resolver data.
/// </returns>
public ValueTask<AuthorizeResult> AuthorizeAsync(IMiddlewareContext context, AuthorizeDirective directive)
public ValueTask<AuthorizeResult> AuthorizeAsync(
IMiddlewareContext context,
AuthorizeDirective directive,
CancellationToken cancellationToken = default)
{
if (!IsUserAuthenticated(context))
{
Expand All @@ -53,6 +57,15 @@ public ValueTask<AuthorizeResult> AuthorizeAsync(IMiddlewareContext context, Aut
return new ValueTask<AuthorizeResult>(AuthorizeResult.NotAllowed);
}

// TODO : check our implementation on this.
public ValueTask<AuthorizeResult> AuthorizeAsync(
AuthorizationContext context,
IReadOnlyList<AuthorizeDirective> directives,
CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}

/// <summary>
/// Get the value of the CLIENT_ROLE_HEADER HTTP Header from the HttpContext.
/// HttpContext will be present in IMiddlewareContext.ContextData
Expand Down
18 changes: 9 additions & 9 deletions src/Core/Services/ExecutionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public async ValueTask ExecuteMutateAsync(IMiddlewareContext context)
/// <returns>
/// Returns the runtime field value.
/// </returns>
public static object? ExecuteLeafField(IPureResolverContext context)
public static object? ExecuteLeafField(IResolverContext context)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The pure resolver context and the resolver context are now aligned.

{
// This means this field is a scalar, so we don't need to do
// anything for it.
Expand Down Expand Up @@ -190,7 +190,7 @@ fieldValue.ValueKind is not (JsonValueKind.Undefined or JsonValueKind.Null))
/// <returns>
/// Returns a new json object.
/// </returns>
public object? ExecuteObjectField(IPureResolverContext context)
public object? ExecuteObjectField(IResolverContext context)
{
string dataSourceName = GraphQLUtils.GetDataSourceNameFromGraphQLContext(context, _runtimeConfigProvider.GetConfig());
DataSource ds = _runtimeConfigProvider.GetConfig().GetDataSourceFromDataSourceName(dataSourceName);
Expand Down Expand Up @@ -226,7 +226,7 @@ fieldValue.ValueKind is not (JsonValueKind.Undefined or JsonValueKind.Null))
/// <returns>The resolved list, a JSON array, returned as type 'object?'.</returns>
/// <remarks>Return type is 'object?' instead of a 'List of JsonElements' because when this function returns JsonElement,
/// the HC12 engine doesn't know how to handle the JsonElement and results in requests failing at runtime.</remarks>
public object? ExecuteListField(IPureResolverContext context)
public object? ExecuteListField(IResolverContext context)
{
string dataSourceName = GraphQLUtils.GetDataSourceNameFromGraphQLContext(context, _runtimeConfigProvider.GetConfig());
DataSource ds = _runtimeConfigProvider.GetConfig().GetDataSourceFromDataSourceName(dataSourceName);
Expand Down Expand Up @@ -267,7 +267,7 @@ private static void SetContextResult(IMiddlewareContext context, JsonDocument? r
}

private static bool TryGetPropertyFromParent(
IPureResolverContext context,
IResolverContext context,
out JsonElement propertyValue)
{
JsonElement parent = context.Parent<JsonElement>();
Expand Down Expand Up @@ -444,7 +444,7 @@ public static InputObjectType InputObjectTypeFromIInputField(IInputField field)
/// CosmosDB does not utilize pagination metadata. So this function will return null
/// when executing GraphQl queries against CosmosDB.
/// </summary>
private static IMetadata? GetMetadata(IPureResolverContext context)
private static IMetadata? GetMetadata(IResolverContext context)
{
if (context.Selection.ResponseName == QueryBuilder.PAGINATION_FIELD_NAME && context.Path.Parent is not null)
{
Expand Down Expand Up @@ -493,7 +493,7 @@ public static InputObjectType InputObjectTypeFromIInputField(IInputField field)
/// </summary>
/// <param name="context">Pure resolver context</param>
/// <returns>Pagination metadata</returns>
private static IMetadata GetMetadataObjectField(IPureResolverContext context)
private static IMetadata GetMetadataObjectField(IResolverContext context)
{
// Depth Levels: / 0 / 1 / 2 / 3
// Example Path: /books/items/items[0]/publishers
Expand All @@ -515,7 +515,7 @@ private static IMetadata GetMetadataObjectField(IPureResolverContext context)
{
// This check handles when the current selection is a relationship field because in that case,
// there will be no context data entry.
// e.g. metadata for index 4 will not exist. only 3.
// e.g. metadata for index 4 will not exist. only 3.
// Depth: / 0 / 1 / 2 / 3 / 4
// Path: /books/items/items[0]/publishers/books
string objectParentName = GetMetadataKey(context.Path) + "::" + context.Path.Parent!.Depth;
Expand Down Expand Up @@ -562,7 +562,7 @@ private static string GetMetadataKey(IFieldSelection rootSelection)
/// context.Path -> /books depth(0)
/// context.Selection -> books { items {id, title}}
/// </summary>
private static void SetNewMetadata(IPureResolverContext context, IMetadata? metadata)
private static void SetNewMetadata(IResolverContext context, IMetadata? metadata)
{
string metadataKey = GetMetadataKey(context.Selection) + "::" + context.Path.Depth;
context.ContextData.Add(metadataKey, metadata);
Expand All @@ -574,7 +574,7 @@ private static void SetNewMetadata(IPureResolverContext context, IMetadata? meta
/// </summary>
/// <param name="context">Pure resolver context</param>
/// <param name="metadata">Pagination metadata</param>
private static void SetNewMetadataChildren(IPureResolverContext context, IMetadata? metadata)
private static void SetNewMetadataChildren(IResolverContext context, IMetadata? metadata)
{
// When context.Path is /entity/items the metadata key is "entity"
// The context key will use the depth of "items" so that the provided
Expand Down
8 changes: 4 additions & 4 deletions src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
<PackageVersion Include="CommandLineParser" Version="2.9.1" />
<PackageVersion Include="coverlet.msbuild" Version="6.0.2" />
<PackageVersion Include="coverlet.collector" Version="6.0.2" />
<PackageVersion Include="HotChocolate" Version="12.22.0" />
<PackageVersion Include="HotChocolate.AspNetCore" Version="12.22.0" />
<PackageVersion Include="HotChocolate.AspNetCore.Authorization" Version="12.22.0" />
<PackageVersion Include="HotChocolate.Types.NodaTime" Version="12.22.0" />
<PackageVersion Include="HotChocolate" Version="14.0.0-rc.0" />
<PackageVersion Include="HotChocolate.AspNetCore" Version="14.0.0-rc.0" />
<PackageVersion Include="HotChocolate.AspNetCore.Authorization" Version="14.0.0-rc.0" />
<PackageVersion Include="HotChocolate.Types.NodaTime" Version="14.0.0-rc.0" />
<PackageVersion Include="Humanizer" Version="2.14.1" />
<PackageVersion Include="Humanizer.Core" Version="2.14.1" />
<PackageVersion Include="DotNetEnv" Version="3.0.0" />
Expand Down
7 changes: 3 additions & 4 deletions src/Service.GraphQLBuilder/CustomScalars/SingleType.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using HotChocolate;
using HotChocolate.Language;
using HotChocolate.Types;

Expand All @@ -13,7 +12,7 @@ namespace Azure.DataApiBuilder.Service.GraphQLBuilder.CustomScalars
/// </summary>
public class SingleType : FloatTypeBase<float>
{
public static readonly NameString TypeName = new("Single");
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We removed NameString in Hot Chocolate 13 as this semantic string object just caused a lot of allocation.

public static readonly string TypeName = new("Single");
public static readonly string SingleDescription = "IEEE 754 32 bit float";

public SingleType()
Expand All @@ -27,7 +26,7 @@ public SingleType()
/// </summary>
public SingleType(float min, float max)
: this(
TypeName.Value,
TypeName,
SingleDescription,
min,
max,
Expand All @@ -39,7 +38,7 @@ public SingleType(float min, float max)
/// Initializes a new instance of the <see cref="SingleType"/> class.
/// </summary>
public SingleType(
NameString name,
string name,
string? description = null,
float min = float.MinValue,
float max = float.MaxValue,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,8 @@ public static string Target(FieldDefinitionNode field)
/// <returns>The name of the target object if the relationship is found, null otherwise.</returns>
public static string? GetTarget(IInputField infield)
{
Directive? directive = (Directive?)infield.Directives.FirstOrDefault(d => d.Name.Value == DirectiveName);
DirectiveNode? directiveNode = directive?.ToNode();
ArgumentNode? arg = directiveNode?.Arguments.First(a => a.Name.Value == "target");

return (string?)arg?.Value.Value;
Directive? directive = (Directive?)infield.Directives.FirstOrDefault(DirectiveName);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The directive collection has now optimized lookup methods. which reduces the code needed to get things from type system members.

return directive?.GetArgumentValue<string>("target");
}

/// <summary>
Expand Down
Loading