Skip to content
Open
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
22 changes: 18 additions & 4 deletions src/Simplic.OxS.Server/Extensions/GraphQLExtension.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
using HotChocolate.Execution.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Simplic.OxS.Server.GraphQL;
using Simplic.OxS.Server.Middleware;

namespace Simplic.OxS.Server.Extensions
{
public static class GraphQLExtension
{
/// <summary>
/// Enable the use of GraphQL within the simplic eco system
/// Enable the use of GraphQL within the simplic eco system.
/// </summary>
/// <param name="services"></param>
/// <returns></returns>
public static IServiceCollection UseGraphQL<TQuery>(this IServiceCollection services, Action<IRequestExecutorBuilder> builder = null) where TQuery : class
/// <param name="services">DI service collection.</param>
/// <param name="builder">Optional builder hook for service-specific extensions.</param>
/// <param name="tolerateMissingFieldValues">
/// When <c>true</c> (default), every output field on user-defined object types is
/// made nullable in the generated schema. This prevents <c>NonNull</c> spec
/// violations when a resolver returns <c>null</c> for a property that wasn't
/// stored on legacy documents. Set to <c>false</c> for strict, spec-conformant
/// non-null behavior (clients then must handle the propagated <c>null</c>).
/// </param>
public static IServiceCollection UseGraphQL<TQuery>(
this IServiceCollection services,
Action<IRequestExecutorBuilder> builder = null,
bool tolerateMissingFieldValues = true) where TQuery : class
Comment on lines +24 to +25

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Verify nullable context + detect similar non-nullable delegate defaults to null.
fd -e csproj --exec rg -n "<Nullable>.*</Nullable>" {}
rg -nP --type=cs '\bAction<[^>]+>\s+\w+\s*=\s*null\b'

Repository: simplic/simplic-oxs

Length of output: 1195


🏁 Script executed:

cat -n src/Simplic.OxS.Server/Extensions/GraphQLExtension.cs | head -50

Repository: simplic/simplic-oxs

Length of output: 2607


Mark the optional builder callback as nullable.

Line 24 assigns null to a non-nullable delegate type. Use Action<IRequestExecutorBuilder>? to align with the enabled nullable reference type context.

Suggested fix
-            Action<IRequestExecutorBuilder> builder = null,
+            Action<IRequestExecutorBuilder>? builder = null,
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
Action<IRequestExecutorBuilder> builder = null,
bool tolerateMissingFieldValues = true) where TQuery : class
Action<IRequestExecutorBuilder>? builder = null,
bool tolerateMissingFieldValues = true) where TQuery : class
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/Simplic.OxS.Server/Extensions/GraphQLExtension.cs` around lines 24 - 25,
The method parameter 'builder' in GraphQLExtension.cs is being assigned null but
is declared non-nullable; change its type to a nullable delegate by updating the
parameter declaration from Action<IRequestExecutorBuilder> builder = null to
Action<IRequestExecutorBuilder>? builder = null (i.e., make the 'builder'
parameter nullable) so it aligns with nullable reference types and the method
signature (where TQuery : class).

{
var req = services.AddGraphQLServer().ModifyOptions(o =>
{
Expand All @@ -22,6 +33,9 @@ public static IServiceCollection UseGraphQL<TQuery>(this IServiceCollection serv
.AddAuthorization()
.AddQueryType<TQuery>();

if (tolerateMissingFieldValues)
req.TryAddTypeInterceptor<MakeFieldsNullableTypeInterceptor>();

// Set TimeSpan representation to d.hh:mm:ss
req.AddType(new TimeSpanType(TimeSpanFormat.DotNet));

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using HotChocolate.Configuration;
using HotChocolate.Types.Descriptors;
using HotChocolate.Types.Descriptors.Configurations;

Check failure on line 3 in src/Simplic.OxS.Server/GraphQL/MakeFieldsNullableTypeInterceptor.cs

View check run for this annotation

Azure Pipelines / simplic.simplic-oxs

src/Simplic.OxS.Server/GraphQL/MakeFieldsNullableTypeInterceptor.cs#L3

src/Simplic.OxS.Server/GraphQL/MakeFieldsNullableTypeInterceptor.cs(3,38): Error CS0234: The type or namespace name 'Configurations' does not exist in the namespace 'HotChocolate.Types.Descriptors' (are you missing an assembly reference?)

namespace Simplic.OxS.Server.GraphQL
{
/// <summary>
/// HotChocolate type interceptor that rewrites every output field of every
/// user-defined object type so its outermost type becomes nullable.
/// <para>
/// Purpose: tolerate items that are missing values for fields the schema
/// would otherwise mark as <c>NonNull</c>. Without this interceptor, a
/// resolver returning <c>null</c> for a <c>!</c>-field causes the parent
/// selection-set to be replaced by <c>null</c> (per GraphQL spec).
/// </para>
/// <para>
/// HotChocolate built-in types (introspection, paging connections, etc.)
/// are skipped because clients depend on their non-null guarantees.
/// </para>
/// </summary>
internal sealed class MakeFieldsNullableTypeInterceptor : TypeInterceptor
{
public override void OnBeforeCompleteName(
ITypeCompletionContext completionContext,
TypeSystemConfiguration configuration)

Check failure on line 25 in src/Simplic.OxS.Server/GraphQL/MakeFieldsNullableTypeInterceptor.cs

View check run for this annotation

Azure Pipelines / simplic.simplic-oxs

src/Simplic.OxS.Server/GraphQL/MakeFieldsNullableTypeInterceptor.cs#L25

src/Simplic.OxS.Server/GraphQL/MakeFieldsNullableTypeInterceptor.cs(25,13): Error CS0246: The type or namespace name 'TypeSystemConfiguration' could not be found (are you missing a using directive or an assembly reference?)
{
if (completionContext.IsIntrospectionType)
return;

if (configuration is not ObjectTypeConfiguration objectConfig)
return;

// Skip HotChocolate's own types (Connection, Edge, PageInfo, ...).
var runtimeType = objectConfig.RuntimeType;
if (runtimeType?.Namespace is { } ns &&
ns.StartsWith("HotChocolate", System.StringComparison.Ordinal))
{
Comment on lines +35 to +37

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Tighten the namespace guard to avoid skipping user types.

Line 36 uses StartsWith("HotChocolate", ...), which also matches namespaces like HotChocolateExtensions and can unintentionally bypass this interceptor for user-defined runtime types.

Suggested fix
-            if (runtimeType?.Namespace is { } ns &&
-                ns.StartsWith("HotChocolate", System.StringComparison.Ordinal))
+            if (runtimeType?.Namespace is { } ns &&
+                (ns.Equals("HotChocolate", System.StringComparison.Ordinal) ||
+                 ns.StartsWith("HotChocolate.", System.StringComparison.Ordinal)))
             {
                 return;
             }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (runtimeType?.Namespace is { } ns &&
ns.StartsWith("HotChocolate", System.StringComparison.Ordinal))
{
if (runtimeType?.Namespace is { } ns &&
(ns.Equals("HotChocolate", System.StringComparison.Ordinal) ||
ns.StartsWith("HotChocolate.", System.StringComparison.Ordinal)))
{
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/Simplic.OxS.Server/GraphQL/MakeFieldsNullableTypeInterceptor.cs` around
lines 35 - 37, The namespace check in MakeFieldsNullableTypeInterceptor is too
broad: the runtimeType?.Namespace StartsWith("HotChocolate", ...) will also
match names like "HotChocolateExtensions" and skip user types; change the guard
to only skip the exact HotChocolate root or its subnamespaces by checking either
Namespace.Equals("HotChocolate", StringComparison.Ordinal) ||
Namespace.StartsWith("HotChocolate.", StringComparison.Ordinal) (or the
equivalent) so runtimeType.Namespace only bypasses the interceptor for
"HotChocolate" and "HotChocolate.*".

return;
}

foreach (var field in objectConfig.Fields)
{
if (field.IsIntrospectionField)
continue;

if (field.Type is not ExtendedTypeReference extRef)
continue;

var nullableType = completionContext.TypeInspector
.ChangeNullability(extRef.Type, true);

field.Type = extRef.WithType(nullableType);
}
}
}
}
Loading