Skip to content

Commit 1664d37

Browse files
authored
Fixed incorrect ResolverParameterKind for GetScopedState/SetLocalState (#7670)
1 parent 28e169d commit 1664d37

8 files changed

+1245
-2
lines changed

src/HotChocolate/Core/src/Types.Analyzers/Models/ResolverParameter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,13 @@ private static ResolverParameterKind GetParameterKind(
116116
{
117117
return parameter.IsSetState()
118118
? ResolverParameterKind.SetScopedState
119-
: ResolverParameterKind.GetGlobalState;
119+
: ResolverParameterKind.GetScopedState;
120120
}
121121

122122
if (parameter.IsLocalState(out key))
123123
{
124124
return parameter.IsSetState()
125-
? ResolverParameterKind.SetGlobalState
125+
? ResolverParameterKind.SetLocalState
126126
: ResolverParameterKind.GetLocalState;
127127
}
128128

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
namespace HotChocolate.Types;
2+
3+
public class ResolverTests
4+
{
5+
[Fact]
6+
public async Task GenerateSource_ResolverWithLocalStateArgument_MatchesSnapshot()
7+
{
8+
await TestHelper.GetGeneratedSourceSnapshot(
9+
"""
10+
using HotChocolate;
11+
using HotChocolate.Types;
12+
13+
namespace TestNamespace;
14+
15+
[ObjectType<Test>]
16+
internal static partial class TestType
17+
{
18+
public static int GetTest([LocalState("Test")] int test)
19+
{
20+
return test;
21+
}
22+
}
23+
24+
internal class Test;
25+
""").MatchMarkdownAsync();
26+
}
27+
28+
[Fact]
29+
public async Task GenerateSource_ResolverWithScopedStateArgument_MatchesSnapshot()
30+
{
31+
await TestHelper.GetGeneratedSourceSnapshot(
32+
"""
33+
using HotChocolate;
34+
using HotChocolate.Types;
35+
36+
namespace TestNamespace;
37+
38+
[ObjectType<Test>]
39+
internal static partial class TestType
40+
{
41+
public static int GetTest([ScopedState("Test")] int test)
42+
{
43+
return test;
44+
}
45+
}
46+
47+
internal class Test;
48+
""").MatchMarkdownAsync();
49+
}
50+
51+
[Fact]
52+
public async Task GenerateSource_ResolverWithGlobalStateArgument_MatchesSnapshot()
53+
{
54+
await TestHelper.GetGeneratedSourceSnapshot(
55+
"""
56+
using HotChocolate;
57+
using HotChocolate.Types;
58+
59+
namespace TestNamespace;
60+
61+
[ObjectType<Test>]
62+
internal static partial class TestType
63+
{
64+
public static int GetTest([GlobalState("Test")] int test)
65+
{
66+
return test;
67+
}
68+
}
69+
70+
internal class Test;
71+
""").MatchMarkdownAsync();
72+
}
73+
74+
[Fact]
75+
public async Task GenerateSource_ResolverWithLocalStateSetStateArgument_MatchesSnapshot()
76+
{
77+
await TestHelper.GetGeneratedSourceSnapshot(
78+
"""
79+
using HotChocolate;
80+
using HotChocolate.Types;
81+
82+
namespace TestNamespace;
83+
84+
[ObjectType<Test>]
85+
internal static partial class TestType
86+
{
87+
public static int GetTest([LocalState] SetState<int> test)
88+
{
89+
test(1);
90+
return 1;
91+
}
92+
}
93+
94+
internal class Test;
95+
""").MatchMarkdownAsync();
96+
}
97+
98+
[Fact]
99+
public async Task GenerateSource_ResolverWithScopedStateSetStateArgument_MatchesSnapshot()
100+
{
101+
await TestHelper.GetGeneratedSourceSnapshot(
102+
"""
103+
using HotChocolate;
104+
using HotChocolate.Types;
105+
106+
namespace TestNamespace;
107+
108+
[ObjectType<Test>]
109+
internal static partial class TestType
110+
{
111+
public static int GetTest([ScopedState] SetState<int> test)
112+
{
113+
test(1);
114+
return 1;
115+
}
116+
}
117+
118+
internal class Test;
119+
""").MatchMarkdownAsync();
120+
}
121+
122+
[Fact]
123+
public async Task GenerateSource_ResolverWithGlobalStateSetStateArgument_MatchesSnapshot()
124+
{
125+
await TestHelper.GetGeneratedSourceSnapshot(
126+
"""
127+
using HotChocolate;
128+
using HotChocolate.Types;
129+
130+
namespace TestNamespace;
131+
132+
[ObjectType<Test>]
133+
internal static partial class TestType
134+
{
135+
public static int GetTest([GlobalState] SetState<int> test)
136+
{
137+
test(1);
138+
return 1;
139+
}
140+
}
141+
142+
internal class Test;
143+
""").MatchMarkdownAsync();
144+
}
145+
}
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
# GenerateSource_ResolverWithGlobalStateArgument_MatchesSnapshot
2+
3+
## HotChocolateResolvers.735550c.g.cs
4+
5+
```csharp
6+
// <auto-generated/>
7+
8+
#nullable enable
9+
#pragma warning disable
10+
11+
using System;
12+
using System.Runtime.CompilerServices;
13+
using HotChocolate;
14+
using HotChocolate.Types;
15+
using HotChocolate.Execution.Configuration;
16+
using HotChocolate.Internal;
17+
18+
namespace TestNamespace
19+
{
20+
internal static class TestTypeResolvers
21+
{
22+
private static bool _bindingsInitialized;
23+
private readonly static global::HotChocolate.Internal.IParameterBinding[] _args_TestType_GetTest = new global::HotChocolate.Internal.IParameterBinding[1];
24+
25+
public static void InitializeBindings(global::HotChocolate.Internal.IParameterBindingResolver bindingResolver)
26+
{
27+
if (_bindingsInitialized)
28+
{
29+
return;
30+
}
31+
_bindingsInitialized = true;
32+
33+
const global::System.Reflection.BindingFlags bindingFlags =
34+
global::System.Reflection.BindingFlags.Public
35+
| global::System.Reflection.BindingFlags.NonPublic
36+
| global::System.Reflection.BindingFlags.Static;
37+
38+
var type = typeof(global::TestNamespace.TestType);
39+
global::System.Reflection.MethodInfo resolver = default!;
40+
global::System.Reflection.ParameterInfo[] parameters = default!;
41+
42+
resolver = type.GetMethod(
43+
"GetTest",
44+
bindingFlags,
45+
new global::System.Type[]
46+
{
47+
typeof(int)
48+
})!;
49+
parameters = resolver.GetParameters();
50+
_args_TestType_GetTest[0] = bindingResolver.GetBinding(parameters[0]);
51+
}
52+
53+
public static HotChocolate.Resolvers.FieldResolverDelegates TestType_GetTest()
54+
{
55+
if(!_bindingsInitialized)
56+
{
57+
throw new global::System.InvalidOperationException("The bindings must be initialized before the resolvers can be created.");
58+
}
59+
return new global::HotChocolate.Resolvers.FieldResolverDelegates(pureResolver: TestType_GetTest_Resolver);
60+
}
61+
62+
private static global::System.Object? TestType_GetTest_Resolver(global::HotChocolate.Resolvers.IResolverContext context)
63+
{
64+
var args0 = context.GetGlobalState<int>("Test");
65+
var result = global::TestNamespace.TestType.GetTest(args0);
66+
return result;
67+
}
68+
}
69+
}
70+
71+
72+
```
73+
74+
## HotChocolateTypeModule.735550c.g.cs
75+
76+
```csharp
77+
// <auto-generated/>
78+
79+
#nullable enable
80+
#pragma warning disable
81+
82+
using System;
83+
using System.Runtime.CompilerServices;
84+
using HotChocolate;
85+
using HotChocolate.Types;
86+
using HotChocolate.Execution.Configuration;
87+
88+
namespace Microsoft.Extensions.DependencyInjection
89+
{
90+
public static partial class TestsTypesRequestExecutorBuilderExtensions
91+
{
92+
public static IRequestExecutorBuilder AddTestsTypes(this IRequestExecutorBuilder builder)
93+
{
94+
AddObjectTypeExtension_8734371<global::TestNamespace.Test>(builder, global::TestNamespace.TestType.Initialize);
95+
return builder;
96+
}
97+
98+
private static void AddObjectTypeExtension_8734371<T>(
99+
global::HotChocolate.Execution.Configuration.IRequestExecutorBuilder builder,
100+
Action<IObjectTypeDescriptor<T>> initialize)
101+
{
102+
builder.ConfigureSchema(sb =>
103+
{
104+
string typeName = typeof(T).FullName!;
105+
string typeKey = $"8734371_Type_ObjectType<{typeName}>";
106+
string hooksKey = $"8734371_Hooks_ObjectType<{typeName}>";
107+
108+
if (!sb.ContextData.ContainsKey(typeKey))
109+
{
110+
sb.AddObjectType<T>(
111+
descriptor =>
112+
{
113+
var hooks = (global::System.Collections.Generic.List<Action<IObjectTypeDescriptor<T>>>)descriptor.Extend().Context.ContextData[hooksKey]!;
114+
foreach (var configure in hooks)
115+
{
116+
configure(descriptor);
117+
};
118+
});
119+
sb.ContextData.Add(typeKey, null);
120+
}
121+
122+
if (!sb.ContextData.TryGetValue(hooksKey, out var value))
123+
{
124+
value = new System.Collections.Generic.List<Action<IObjectTypeDescriptor<T>>>();
125+
sb.ContextData.Add(hooksKey, value);
126+
}
127+
128+
((System.Collections.Generic.List<Action<IObjectTypeDescriptor<T>>>)value!).Add(initialize);
129+
});
130+
}
131+
}
132+
}
133+
134+
```
135+
136+
## HotChocolateTypes.735550c.g.cs
137+
138+
```csharp
139+
// <auto-generated/>
140+
141+
#nullable enable
142+
#pragma warning disable
143+
144+
using System;
145+
using System.Runtime.CompilerServices;
146+
using HotChocolate;
147+
using HotChocolate.Types;
148+
using HotChocolate.Execution.Configuration;
149+
using Microsoft.Extensions.DependencyInjection;
150+
151+
namespace TestNamespace
152+
{
153+
public static partial class TestType
154+
{
155+
internal static void Initialize(global::HotChocolate.Types.IObjectTypeDescriptor<global::TestNamespace.Test> descriptor)
156+
{
157+
const global::System.Reflection.BindingFlags bindingFlags =
158+
global::System.Reflection.BindingFlags.Public
159+
| global::System.Reflection.BindingFlags.NonPublic
160+
| global::System.Reflection.BindingFlags.Static;
161+
162+
var thisType = typeof(global::TestNamespace.TestType);
163+
var bindingResolver = descriptor.Extend().Context.ParameterBindingResolver;
164+
global::TestNamespace.TestTypeResolvers.InitializeBindings(bindingResolver);
165+
166+
descriptor
167+
.Field(thisType.GetMember("GetTest", bindingFlags)[0])
168+
.ExtendWith(c =>
169+
{
170+
c.Definition.SetSourceGeneratorFlags();
171+
c.Definition.Resolvers = global::TestNamespace.TestTypeResolvers.TestType_GetTest();
172+
});
173+
174+
Configure(descriptor);
175+
}
176+
177+
static partial void Configure(global::HotChocolate.Types.IObjectTypeDescriptor<global::TestNamespace.Test> descriptor);
178+
}
179+
}
180+
181+
182+
```
183+

0 commit comments

Comments
 (0)