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

Fixed ID Attribute #7092

Merged
merged 4 commits into from
May 6, 2024
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 @@ -37,19 +37,7 @@ public override void OnBeforeRegisterDependencies(

discoveryContext.RegisterDependencies(extensionDefinition);

foreach (var field in def.Fields)
{
if (field is FilterFieldDefinition filterField &&
filterField.Member is { } member &&
(member.GetCustomAttribute(typeof(IDAttribute)) is { } ||
member.GetCustomAttribute(typeof(IDAttribute<>)) is { }))
{
filterField.Type = discoveryContext.TypeInspector.GetTypeRef(
typeof(IdOperationFilterInputType),
TypeContext.Input,
discoveryContext.Scope);
}
}
ApplyIdAttributesToFields(discoveryContext, def);
}

public override void OnBeforeCompleteName(
Expand Down Expand Up @@ -150,4 +138,48 @@ filterFieldDefinition.Type is { } filterFieldType &&
}
}
}

private static void ApplyIdAttributesToFields(
ITypeDiscoveryContext discoveryContext,
FilterInputTypeDefinition def)
{
foreach (var field in def.Fields)
{
if (field.HasIdAttribute())
{
field.Type = discoveryContext.TypeInspector.GetTypeRef(
typeof(IdOperationFilterInputType),
TypeContext.Input,
discoveryContext.Scope);
}
}
}
}

file static class Extensions
{
public static bool HasIdAttribute(this InputFieldDefinition? definiton)
{
if (definiton is not FilterFieldDefinition { Member: { } member })
{
return false;
}

var attributes = member.GetCustomAttributesData();
foreach (var attribute in attributes)
{
if (attribute.AttributeType == typeof(IDAttribute))
{
return true;
}

if (attribute.AttributeType.IsGenericType &&
attribute.AttributeType.GetGenericTypeDefinition() == typeof(IDAttribute<>))
{
return true;
}
}

return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
schema {
query: Query
}

type Query {
test: String
}

input FooIdGenericFilterInput {
and: [FooIdGenericFilterInput!]
or: [FooIdGenericFilterInput!]
bar: IdOperationFilterInput
}

input IdOperationFilterInput {
eq: ID
neq: ID
in: [ID]
nin: [ID]
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace HotChocolate.OpenApi.Tests;
[Collection("Open api integration tests")]
public class IntegrationTests
{
private readonly string _petStoreYaml = FileResource.Open("PetStore.yaml");

[Theory]
[InlineData("findAnyPets", "query { findPets { name } }")]
[InlineData("findSinglePet", "query { findPetById(id: 1) { name } }")]
Expand Down Expand Up @@ -40,12 +42,11 @@ public async Task QueryPets_Returns_Results(string caseName, string query)
});

await openApiServer.Host.StartAsync();
var apiDocument = FileResource.Open("PetStore.yaml");

var schema = await new ServiceCollection()
.AddSingleton(httpClientFactoryMock.Object)
.AddGraphQL()
.AddOpenApi("PetStore", apiDocument)
.AddOpenApi("PetStore", _petStoreYaml)
.BuildRequestExecutorAsync();

// Act
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public GetJsonResult(global::System.Text.Json.JsonElement json)
{
unchecked
{
var hash = 5;
int hash = 5;
hash ^= 397 * Json.GetHashCode();
return hash;
}
Expand Down Expand Up @@ -401,7 +401,7 @@ public partial class AnyScalarDefaultSerializationClientEntityIdFactory : global
{Indented = false};
public global::StrawberryShake.EntityId Parse(global::System.Text.Json.JsonElement obj)
{
var __typename = obj.GetProperty("__typename").GetString()!;
global::System.String __typename = obj.GetProperty("__typename").GetString()!;
return __typename switch
{
_ => throw new global::System.NotSupportedException()};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public GetFooResult(global::System.Collections.Generic.IReadOnlyList<global::Str
{
unchecked
{
var hash = 5;
int hash = 5;
if (Foo != null)
{
foreach (var Foo_elm in Foo)
Expand Down Expand Up @@ -231,7 +231,7 @@ public GetFoo_Foo_Baz(global::System.String? id)
{
unchecked
{
var hash = 5;
int hash = 5;
if (Id != null)
{
hash ^= 397 * Id.GetHashCode();
Expand Down Expand Up @@ -297,7 +297,7 @@ public GetFoo_Foo_Quox(global::System.String? foo)
{
unchecked
{
var hash = 5;
int hash = 5;
if (Foo != null)
{
hash ^= 397 * Foo.GetHashCode();
Expand Down Expand Up @@ -363,7 +363,7 @@ public GetFoo_Foo_Baz2(global::System.String? id)
{
unchecked
{
var hash = 5;
int hash = 5;
if (Id != null)
{
hash ^= 397 * Id.GetHashCode();
Expand Down Expand Up @@ -429,7 +429,7 @@ public GetFoo_Foo_Quox2(global::System.String? foo)
{
unchecked
{
var hash = 5;
int hash = 5;
if (Foo != null)
{
hash ^= 397 * Foo.GetHashCode();
Expand Down Expand Up @@ -726,7 +726,7 @@ public GetFooResult Create(global::StrawberryShake.IOperationResultDataInfo data
}

var bars = new global::System.Collections.Generic.List<global::StrawberryShake.CodeGeneration.CSharp.Integration.EntityIdOrData.IGetFoo_Foo?>();
foreach (var child in list)
foreach (global::StrawberryShake.EntityIdOrData? child in list)
{
bars.Add(MapIGetFoo_Foo(child, snapshot));
}
Expand Down Expand Up @@ -1044,7 +1044,7 @@ public partial class EntityIdOrDataClientEntityIdFactory : global::StrawberrySha
{Indented = false};
public global::StrawberryShake.EntityId Parse(global::System.Text.Json.JsonElement obj)
{
var __typename = obj.GetProperty("__typename").GetString()!;
global::System.String __typename = obj.GetProperty("__typename").GetString()!;
return __typename switch
{
"Baz" => ParseBazEntityId(obj, __typename),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ public GetHeroResult(global::StrawberryShake.CodeGeneration.CSharp.Integration.M
{
unchecked
{
var hash = 5;
int hash = 5;
if (Hero != null)
{
hash ^= 397 * Hero.GetHashCode();
Expand Down Expand Up @@ -325,7 +325,7 @@ public GetHero_Hero_Droid(global::System.String name, global::StrawberryShake.Co
{
unchecked
{
var hash = 5;
int hash = 5;
hash ^= 397 * Name.GetHashCode();
if (Friends != null)
{
Expand Down Expand Up @@ -395,7 +395,7 @@ public GetHero_Hero_Human(global::System.String name, global::StrawberryShake.Co
{
unchecked
{
var hash = 5;
int hash = 5;
hash ^= 397 * Name.GetHashCode();
if (Friends != null)
{
Expand Down Expand Up @@ -468,7 +468,7 @@ public GetHero_Hero_Friends_FriendsConnection(global::System.Collections.Generic
{
unchecked
{
var hash = 5;
int hash = 5;
if (Nodes != null)
{
foreach (var Nodes_elm in Nodes)
Expand Down Expand Up @@ -540,7 +540,7 @@ public GetHero_Hero_Friends_Nodes_Droid(global::System.String name)
{
unchecked
{
var hash = 5;
int hash = 5;
hash ^= 397 * Name.GetHashCode();
return hash;
}
Expand Down Expand Up @@ -602,7 +602,7 @@ public GetHero_Hero_Friends_Nodes_Human(global::System.String name)
{
unchecked
{
var hash = 5;
int hash = 5;
hash ^= 397 * Name.GetHashCode();
return hash;
}
Expand Down Expand Up @@ -733,7 +733,7 @@ public OnReviewSubResult(global::StrawberryShake.CodeGeneration.CSharp.Integrati
{
unchecked
{
var hash = 5;
int hash = 5;
hash ^= 397 * OnReview.GetHashCode();
return hash;
}
Expand Down Expand Up @@ -804,7 +804,7 @@ public OnReviewSub_OnReview_Review(global::System.String __typename, global::Sys
{
unchecked
{
var hash = 5;
int hash = 5;
hash ^= 397 * __typename.GetHashCode();
hash ^= 397 * Stars.GetHashCode();
if (Commentary != null)
Expand Down Expand Up @@ -899,7 +899,7 @@ public CreateReviewMutResult(global::StrawberryShake.CodeGeneration.CSharp.Integ
{
unchecked
{
var hash = 5;
int hash = 5;
hash ^= 397 * CreateReview.GetHashCode();
return hash;
}
Expand Down Expand Up @@ -964,7 +964,7 @@ public CreateReviewMut_CreateReview_Review(global::System.Int32 stars, global::S
{
unchecked
{
var hash = 5;
int hash = 5;
hash ^= 397 * Stars.GetHashCode();
if (Commentary != null)
{
Expand Down Expand Up @@ -1105,7 +1105,7 @@ public partial class ReviewInput : global::StrawberryShake.CodeGeneration.CSharp
{
unchecked
{
var hash = 5;
int hash = 5;
hash ^= 397 * Stars.GetHashCode();
if (Commentary != null)
{
Expand Down Expand Up @@ -2315,7 +2315,7 @@ public GetHero_Hero_Droid Map(global::StrawberryShake.CodeGeneration.CSharp.Inte
}

var characters = new global::System.Collections.Generic.List<global::StrawberryShake.CodeGeneration.CSharp.Integration.MultiProfile.IGetHero_Hero_Friends_Nodes?>();
foreach (var child in list)
foreach (global::StrawberryShake.EntityId? child in list)
{
characters.Add(MapIGetHero_Hero_Friends_Nodes(child, snapshot));
}
Expand Down Expand Up @@ -2396,7 +2396,7 @@ public GetHero_Hero_Human Map(global::StrawberryShake.CodeGeneration.CSharp.Inte
}

var characters = new global::System.Collections.Generic.List<global::StrawberryShake.CodeGeneration.CSharp.Integration.MultiProfile.IGetHero_Hero_Friends_Nodes?>();
foreach (var child in list)
foreach (global::StrawberryShake.EntityId? child in list)
{
characters.Add(MapIGetHero_Hero_Friends_Nodes(child, snapshot));
}
Expand Down Expand Up @@ -2475,7 +2475,7 @@ public partial class MultiProfileClientEntityIdFactory : global::StrawberryShake
{Indented = false};
public global::StrawberryShake.EntityId Parse(global::System.Text.Json.JsonElement obj)
{
var __typename = obj.GetProperty("__typename").GetString()!;
global::System.String __typename = obj.GetProperty("__typename").GetString()!;
return __typename switch
{
"Droid" => ParseDroidEntityId(obj, __typename),
Expand Down
Loading
Loading