Skip to content

Fix @semanticNonNull on interface id fields #8305

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

Merged
merged 3 commits into from
May 23, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ public override void OnBeforeCompleteType(ITypeCompletionContext completionConte
continue;
}

if (field.Name == "id")
{
continue;
}

var levels = GetSemanticNonNullLevels(field.Type);

if (levels.Count < 1)
Expand Down
52 changes: 52 additions & 0 deletions src/HotChocolate/Core/test/Types.Tests/SemanticNonNullTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using HotChocolate.Execution;
using HotChocolate.Tests;
using HotChocolate.Types;
using HotChocolate.Types.Descriptors;
using HotChocolate.Types.Relay;
using Microsoft.Extensions.DependencyInjection;

Expand All @@ -25,6 +26,20 @@ public async Task Object_With_Id_Field()
.MatchSnapshotAsync();
}

[Fact]
public async Task Interface_With_Id_Field()
{
await new ServiceCollection()
.AddGraphQL()
.AddGlobalObjectIdentification()
.ModifyOptions(o => o.EnableSemanticNonNull = true)
.AddQueryType<QueryWithInteface>()
.AddType<InterfaceImplementingNode>()
.UseField(_ => _ => default)
.BuildSchemaAsync()
.MatchSnapshotAsync();
}

[Fact]
public async Task MutationConventions()
{
Expand Down Expand Up @@ -137,6 +152,43 @@ type Foo {
.MatchSnapshotAsync();
}

public class QueryWithInteface
{
public SomeObject GetSomeObject() => new();
}

[Node]
[ImplementsInterface<InterfaceImplementingNode>]
public record SomeObject
{
public int Id { get; set; }

public string Field { get; set; } = default!;

public static SomeObject? Get(int id) => new();
}

public class InterfaceImplementingNode : InterfaceType
{
protected override void Configure(IInterfaceTypeDescriptor descriptor)
{
descriptor.Implements<NodeType>();
descriptor
.Field("field")
.Type("String!");
}
}

[AttributeUsage(AttributeTargets.Class)]
public sealed class ImplementsInterfaceAttribute<T> : ObjectTypeDescriptorAttribute
where T : InterfaceType
{
protected override void OnConfigure(
IDescriptorContext context,
IObjectTypeDescriptor descriptor,
Type type) => descriptor.Implements<T>();
}

public class QueryType : ObjectType
{
protected override void Configure(IObjectTypeDescriptor descriptor)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
schema {
query: QueryWithInteface
}

interface InterfaceImplementingNode implements Node {
field: String @semanticNonNull
id: ID!
}

"The node interface is implemented by entities that have a global unique identifier."
interface Node {
id: ID!
}

type QueryWithInteface {
"Fetches an object given its ID."
node("ID of the object." id: ID!): Node
"Lookup nodes by a list of IDs."
nodes("The list of node IDs." ids: [ID!]!): [Node] @semanticNonNull
someObject: SomeObject @semanticNonNull
}

type SomeObject implements Node & InterfaceImplementingNode {
id: ID!
field: String @semanticNonNull
}

directive @semanticNonNull(levels: [Int!] = [ 0 ]) on FIELD_DEFINITION
Loading