Skip to content

Commit

Permalink
Merge pull request #6 from autoguru-au/feature/support-hc12.6
Browse files Browse the repository at this point in the history
feat: Better support for HC v12.6.0
  • Loading branch information
benmccallum authored Jan 28, 2022
2 parents 14f891b + 7fa67be commit f856b03
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 28 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ dotnet add package AutoGuru.HotChocolate.PolymorphicIds

Configure it on your schema (`ISchemaBuilder`) or executor (`IRequestExecutorBuilder`):
```c#
.AddGlobalObjectIdentification() // Required since Hot Chocolate v12.6.0+
.AddPolymorphicIds(new PolymorphicIdsOptions
{
HandleGuidIds = false, // true by default
Expand Down Expand Up @@ -107,7 +108,8 @@ We strive to match Hot Chocolate's supported .NET target frameworks, though this

| HotChocolate | Polymorphic IDs | Our docs |
| ------------ | --------------- | -----------|
| v12.0.0 | v2 | right here |
| v12.6.0* | v3 | right here |
| v12.0.0 | v2 | [/v2/main](https://github.com/autoguru-au/hotchocolate-polymorphic-ids/tree/v2/main) branch |
| v11.1.0 | v1 | [/v1/main](https://github.com/autoguru-au/hotchocolate-polymorphic-ids/tree/v1/main) branch |

\* Denotes unexpected binary incompatibility / breaking change in Hot Chocolate
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
For more details look at the `Errors` property.

1. Global ID support isn't enabled but is required for AutoGuru.HotChocolate.PolymorphicIds. Please ensure that AddGlobalObjectIdentification is called during startup.
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@ input LolInput {
directive @defer("If this argument label has a value other than null, it will be passed on to the result of this defer directive. This label is intended to give client applications a way to identify to which fragment a deferred result belongs to." label: String "Deferred when true." if: Boolean) on FRAGMENT_SPREAD | INLINE_FRAGMENT

"The `@stream` directive may be provided for a field of `List` type so that the backend can leverage technology such as asynchronous iterators to provide a partial list in the initial response, and additional list items in subsequent responses. `@include` and `@skip` take precedence over `@stream`."
directive @stream("If this argument label has a value other than null, it will be passed on to the result of this stream directive. This label is intended to give client applications a way to identify to which fragment a streamed result belongs to." label: String "The initial elements that shall be send down to the consumer." initialCount: Int! "Streamed when true." if: Boolean) on FIELD
directive @stream("If this argument label has a value other than null, it will be passed on to the result of this stream directive. This label is intended to give client applications a way to identify to which fragment a streamed result belongs to." label: String "The initial elements that shall be send down to the consumer." initialCount: Int! = 0 "Streamed when true." if: Boolean) on FIELD
76 changes: 53 additions & 23 deletions src/AutoGuru.HotChocolate.PolymorphicIds.Tests/IdAttributeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using HotChocolate.Types;
using HotChocolate.Types.Relay;
using Microsoft.Extensions.DependencyInjection;
using Shouldly;
using VerifyTests;
using VerifyXunit;
using Xunit;
Expand Down Expand Up @@ -58,6 +59,21 @@ query foo (
nullableGuidIdList(id: [$guidId $null $guidId])
}";

[Fact]
public async Task PolyId_SchemaBuildError_If_GlobalIdentification_Isnt_Enabled()
{
// arrange

// act
var schemaBuilder = SchemaBuilder.New()
.AddQueryType<Query>()
.AddPolymorphicIds();

// assert
var ex = Should.Throw<SchemaException>(() => schemaBuilder.Create());
await Verifier.Verify(ex.Message);
}

[Theory]
[InlineData(false)]
[InlineData(true)]
Expand All @@ -74,7 +90,7 @@ public async Task PolyId_On_Arguments(bool isEnabled)
.AddQueryType<Query>()
.AddType<FooPayload>()
.AddType<Bar>()
.AddGlobalObjectIdentification()
.AddGlobalObjectIdentification(registerNodeInterface: false)
.AddPolymorphicIds(isEnabled
? default
: new PolymorphicIdsOptions
Expand Down Expand Up @@ -116,6 +132,7 @@ public async Task PolyId_On_Arguments_Invalid_Id()
await SchemaBuilder.New()
.AddQueryType<Query>()
.AddType<FooPayload>()
.AddGlobalObjectIdentification(registerNodeInterface: false)
.AddPolymorphicIds()
.Create()
.MakeExecutable(_executorOptions)
Expand Down Expand Up @@ -145,6 +162,7 @@ public async Task PolyId_On_Objects(bool isEnabled)
await SchemaBuilder.New()
.AddQueryType<Query>()
.AddType<FooPayload>()
.AddGlobalObjectIdentification(registerNodeInterface: false)
.AddPolymorphicIds(isEnabled
? default
: new PolymorphicIdsOptions
Expand Down Expand Up @@ -200,6 +218,7 @@ public async Task PolyId_On_Objects_Invalid_Id()
await SchemaBuilder.New()
.AddQueryType<Query>()
.AddType<FooPayload>()
.AddGlobalObjectIdentification(registerNodeInterface: false)
.AddPolymorphicIds()
.Create()
.MakeExecutable(_executorOptions)
Expand Down Expand Up @@ -239,6 +258,7 @@ public async Task PolyId_On_Objects_Invalid_Id_FluentStyle()
.AddQueryType<Query>()
.AddType<FooPayload>()
.AddType<LolInputType>()
.AddGlobalObjectIdentification(registerNodeInterface: false)
.AddPolymorphicIds()
.Create();

Expand Down Expand Up @@ -270,16 +290,18 @@ public async Task Id_On_Arguments()
{
// arrange
var idSerializer = new IdSerializer();
var intId = idSerializer.Serialize("Query", 1);
var longId = idSerializer.Serialize("Query", long.MaxValue);
var stringId = idSerializer.Serialize("Query", "abc");
var guidId = idSerializer.Serialize("Query", new Guid("26a2dc8f-4dab-408c-88c6-523a0a89a2b5"));
var intId = idSerializer.Serialize("Some", 1);
var longId = idSerializer.Serialize("Some", long.MaxValue);
var stringId = idSerializer.Serialize("Some", "abc");
var guidId = idSerializer.Serialize("Some", new Guid("26a2dc8f-4dab-408c-88c6-523a0a89a2b5"));

// act
var result =
await SchemaBuilder.New()
.AddQueryType<Query>()
.AddType<FooPayload>()
.AddType<Bar>()
.AddGlobalObjectIdentification(registerNodeInterface: false)
.Create()
.MakeExecutable()
.ExecuteAsync(
Expand Down Expand Up @@ -308,6 +330,7 @@ public async Task Id_On_Objects()
await SchemaBuilder.New()
.AddQueryType<Query>()
.AddType<FooPayload>()
.AddGlobalObjectIdentification(registerNodeInterface: false)
.Create()
.MakeExecutable()
.ExecuteAsync(
Expand Down Expand Up @@ -357,6 +380,7 @@ public async Task Id_On_Objects_Given_Nulls()
await SchemaBuilder.New()
.AddQueryType<Query>()
.AddType<FooPayload>()
.AddGlobalObjectIdentification(registerNodeInterface: false)
.Create()
.MakeExecutable()
.ExecuteAsync(
Expand Down Expand Up @@ -405,6 +429,7 @@ public async Task Id_On_Objects_InvalidType()
await SchemaBuilder.New()
.AddQueryType<Query>()
.AddType<FooPayload>()
.AddGlobalObjectIdentification(registerNodeInterface: false)
.Create()
.MakeExecutable()
.ExecuteAsync(
Expand Down Expand Up @@ -436,25 +461,29 @@ public async Task Id_On_Objects_InvalidId()
var someId = "abc";

// act
var result =
await SchemaBuilder.New()
.AddQueryType<Query>()
.AddType<FooPayload>()
.Create()
.MakeExecutable()
.ExecuteAsync(
QueryRequestBuilder.New()
.SetQuery(
@"query foo ($someId: ID!) {
foo(input: { someId: $someId someIds: [$someId] }) {
someId
... on FooPayload {
someIds
}
var schema = SchemaBuilder.New()
.AddQueryType<Query>()
.AddType<FooPayload>()
.AddGlobalObjectIdentification(registerNodeInterface: false)
.Create();

var executableSchema = schema
.MakeExecutable();

var result = await executableSchema
.ExecuteAsync(
QueryRequestBuilder.New()
.SetQuery(
@"query foo ($someId: ID!) {
foo(input: { someId: $someId someIds: [$someId] }) {
someId
... on FooPayload {
someIds
}
}")
.SetVariableValue("someId", someId)
.Create());
}
}")
.SetVariableValue("someId", someId)
.Create());

// assert
await Verifier.Verify(new
Expand All @@ -474,6 +503,7 @@ public async Task Schema()
SchemaBuilder.New()
.AddQueryType<Query>()
.AddType<FooPayload>()
.AddGlobalObjectIdentification(registerNodeInterface: false)
.AddPolymorphicIds()
.Create()
.ToString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static ISchemaBuilder AddPolymorphicIds(
}

options ??= new PolymorphicIdsOptions();

return builder
.SetContextData(typeof(PolymorphicIdsOptions).FullName!, options)
.TryAddTypeInterceptor<PolymorphicIdsTypeInterceptor>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ internal sealed class PolymorphicIdsTypeInterceptor : TypeInterceptor
{
private const string StandardGlobalIdFormatterName = "GlobalIdInputValueFormatter";

// /src/HotChocolate/Core/src/Types/Types/WellKnownContextData.cs
private const string GlobalIdSupportEnabledContextDataKey = "HotChocolate.Relay.GlobalId";

private PolymorphicIdsOptions? _options;
private PolymorphicIdsOptions Options =>
_options ?? throw new Exception("Options weren't set up");
Expand All @@ -25,6 +28,19 @@ public override void OnBeforeCompleteType(
DefinitionBase? definition,
IDictionary<string, object?> contextData)
{
var globalContextData = completionContext.ContextData;
if (!globalContextData.ContainsKey(GlobalIdSupportEnabledContextDataKey))
{
var error = SchemaErrorBuilder.New()
.SetMessage(
"Global ID support isn't enabled but is required for " +
"AutoGuru.HotChocolate.PolymorphicIds. Please ensure that " +
$"{nameof(RelaySchemaBuilderExtensions.AddGlobalObjectIdentification)} " +
"is called during startup.")
.Build();
throw new SchemaException(error);
}

if (completionContext.ContextData.TryGetValue(
typeof(PolymorphicIdsOptions).FullName!,
out var o) &&
Expand Down
4 changes: 2 additions & 2 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<Product>Polymorphic IDs.</Product>
<Description>Polymorphic Relay IDs for HotChocolate</Description>
<PackageTags>GraphQL HotChocolate Relay</PackageTags>
<Version>2.1.0</Version>
<Version>3.0.0</Version>

<PackageProjectUrl>https://github.com/autoguru-au/hotchocolate-polymorphic-ids</PackageProjectUrl>
<RepositoryUrl>https://github.com/autoguru-au/hotchocolate-polymorphic-ids</RepositoryUrl>
Expand All @@ -17,7 +17,7 @@
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>

<HotChocolateVersion>12.0.0</HotChocolateVersion>
<HotChocolateVersion>12.6.0</HotChocolateVersion>

<TargetFrameworks>net5.0; netcoreapp3.1; netstandard2.0</TargetFrameworks>
<TargetFrameworks Condition="$(MSBuildProjectName.EndsWith('Tests'))">net5.0; netcoreapp3.1</TargetFrameworks>
Expand Down

0 comments on commit f856b03

Please sign in to comment.