Skip to content

Commit beb8bb3

Browse files
authored
Added global ID formatter conditionally (#8684)
1 parent 2c9abe9 commit beb8bb3

File tree

2 files changed

+62
-3
lines changed

2 files changed

+62
-3
lines changed

src/HotChocolate/Data/src/Data/Filters/Extensions/RelayIdFilterFieldExtensions.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using HotChocolate.Configuration;
22
using HotChocolate.Language;
33
using HotChocolate.Types;
4+
using HotChocolate.Types.Relay;
45
using HotChocolate.Utilities;
56

67
namespace HotChocolate.Data.Filters;
@@ -31,9 +32,12 @@ public static IFilterOperationFieldDescriptor ID(
3132
.Extend()
3233
.OnBeforeCompletion((c, d) =>
3334
{
34-
var returnType = d.Member is null ? typeof(string) : d.Member.GetReturnType();
35-
var returnTypeInfo = c.DescriptorContext.TypeInspector.CreateTypeInfo(returnType);
36-
d.Formatters.Push(CreateSerializer(c, returnTypeInfo.NamedType));
35+
if (c.Features.Get<NodeSchemaFeature>()?.IsEnabled == true)
36+
{
37+
var returnType = d.Member is null ? typeof(string) : d.Member.GetReturnType();
38+
var returnTypeInfo = c.DescriptorContext.TypeInspector.CreateTypeInfo(returnType);
39+
d.Formatters.Push(CreateSerializer(c, returnTypeInfo.NamedType));
40+
}
3741
});
3842

3943
return descriptor;

src/HotChocolate/Data/test/Data.Filters.Tests/FilterInputTypeTest.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,53 @@ public void FilterInputType_Should_InfereType_When_ItIsAInterface()
377377
schema.MatchSnapshot();
378378
}
379379

380+
[Fact]
381+
public void FilterInputType_WithGlobalObjectIdentification_AppliesGlobalIdFormatter()
382+
{
383+
// arrange
384+
var builder = SchemaBuilder.New()
385+
.ModifyOptions(x => x.StrictValidation = false)
386+
.AddGlobalObjectIdentification()
387+
.AddFiltering()
388+
.AddQueryType(
389+
d => d
390+
.Field("books")
391+
.UseFiltering<BookFilterInput>()
392+
.Resolve(new List<Book>()));
393+
394+
// act
395+
var schema = builder.Create();
396+
var idFilterField = ((BookFilterInput)schema.Types[nameof(BookFilterInput)]).Fields["id"];
397+
398+
// assert
399+
Assert.True(
400+
((IdOperationFilterInputType)idFilterField.Type).Fields.All(
401+
f => f.Formatter is FilterGlobalIdInputValueFormatter));
402+
}
403+
404+
[Fact]
405+
public void FilterInputType_WithoutGlobalObjectIdentification_DoesNotApplyGlobalIdFormatter()
406+
{
407+
// arrange
408+
var builder = SchemaBuilder.New()
409+
.ModifyOptions(x => x.StrictValidation = false)
410+
.AddFiltering()
411+
.AddQueryType(
412+
d => d
413+
.Field("books")
414+
.UseFiltering<BookFilterInput>()
415+
.Resolve(new List<Book>()));
416+
417+
// act
418+
var schema = builder.Create();
419+
var idFilterField = ((BookFilterInput)schema.Types[nameof(BookFilterInput)]).Fields["id"];
420+
421+
// assert
422+
Assert.True(
423+
((IdOperationFilterInputType)idFilterField.Type).Fields.All(
424+
f => f.Formatter is not FilterGlobalIdInputValueFormatter));
425+
}
426+
380427
[Fact]
381428
public async Task Execute_CoerceWhereArgument_MatchesSnapshot()
382429
{
@@ -608,4 +655,12 @@ public class FilterWithStruct
608655
}
609656

610657
public record struct ExampleValueType(string Foo, string Bar);
658+
659+
private class BookFilterInput : FilterInputType<Book>
660+
{
661+
protected override void Configure(IFilterInputTypeDescriptor<Book> descriptor)
662+
{
663+
descriptor.Field(b => b.Id).Type<IdOperationFilterInputType>();
664+
}
665+
}
611666
}

0 commit comments

Comments
 (0)