Skip to content

Commit

Permalink
Fixed Service Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib committed Oct 16, 2024
1 parent 81a40ed commit d7f67ed
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public T Resolver<T>()
public T Service<T>() where T : notnull => Services.GetRequiredService<T>();

#if NET8_0_OR_GREATER
public T Service<T>(object key) where T : notnull => parentContext.Service<T>(key);
public T Service<T>(object key) where T : notnull => Services.GetRequiredKeyedService<T>(key);
#endif

public object Service(Type service)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ namespace HotChocolate.Resolvers.Expressions.Parameters;
internal static class ServiceExpressionHelper
{
private const string _serviceResolver = nameof(GetService);
#if NET8_0_OR_GREATER
private const string _keyedServiceResolver = nameof(GetKeyedService);
#endif
private static readonly Expression _true = Expression.Constant(true);
private static readonly Expression _false = Expression.Constant(false);

Expand Down Expand Up @@ -46,12 +48,18 @@ public static Expression Build(

private static Expression BuildDefaultService(ParameterInfo parameter, Expression context)
{
#if NET7_0_OR_GREATER
var parameterType = parameter.ParameterType;
var argumentMethod = _getServiceMethod.MakeGenericMethod(parameterType);
var nullabilityContext = new NullabilityInfoContext();
var nullabilityInfo = nullabilityContext.Create(parameter);
var isRequired = nullabilityInfo.ReadState == NullabilityState.NotNull;
return Expression.Call(argumentMethod, context, isRequired ? _true : _false);
#else
var parameterType = parameter.ParameterType;
var argumentMethod = _getServiceMethod.MakeGenericMethod(parameterType);
return Expression.Call(argumentMethod, context, _true);
#endif
}

#if NET8_0_OR_GREATER
Expand All @@ -65,6 +73,7 @@ private static Expression BuildDefaultService(ParameterInfo parameter, Expressio
var isRequired = nullabilityInfo.ReadState == NullabilityState.NotNull;
return Expression.Call(argumentMethod, context, keyExpression, isRequired ? _true : _false);
}
#endif

public static TService? GetService<TService>(
IResolverContext context,
Expand All @@ -76,6 +85,7 @@ private static Expression BuildDefaultService(ParameterInfo parameter, Expressio
: context.Services.GetService<TService>();
}

#if NET8_0_OR_GREATER
public static TService? GetKeyedService<TService>(
IResolverContext context,
object? key,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,7 @@ public static Task<IDictionary<int, string>> GetEntityByIdAsync(
""").MatchMarkdownAsync();
}

#if NET8_0_OR_GREATER
[Fact]
public async Task GenerateSource_BatchDataLoader_With_SelectorBuilder_MatchesSnapshot()
{
Expand Down Expand Up @@ -499,4 +500,5 @@ public static Task<IDictionary<int, string>> GetEntityByIdAsync(
}
""").MatchMarkdownAsync();
}
#endif
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,26 +100,3 @@ namespace Microsoft.Extensions.DependencyInjection

```

## Compilation Diagnostics

```json
[
{
"Id": "GD0002",
"Title": "Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.",
"Severity": "Error",
"WarningLevel": 0,
"Location": ": (13,8)-(13,47)",
"HelpLinkUri": "https://msdn.microsoft.com/query/roslyn.query?appId=roslyn&k=k(CS9204)",
"MessageFormat": "'{0}' is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.",
"Message": "'GreenDonut.Predicates.IPredicateBuilder' is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.",
"Category": "Compiler",
"CustomTags": [
"Compiler",
"Telemetry",
"CustomObsolete"
]
}
]
```

Original file line number Diff line number Diff line change
Expand Up @@ -100,26 +100,3 @@ namespace Microsoft.Extensions.DependencyInjection

```

## Compilation Diagnostics

```json
[
{
"Id": "GD0001",
"Title": "Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.",
"Severity": "Error",
"WarningLevel": 0,
"Location": ": (13,8)-(13,45)",
"HelpLinkUri": "https://msdn.microsoft.com/query/roslyn.query?appId=roslyn&k=k(CS9204)",
"MessageFormat": "'{0}' is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.",
"Message": "'GreenDonut.Selectors.ISelectorBuilder' is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.",
"Category": "Compiler",
"CustomTags": [
"Compiler",
"Telemetry",
"CustomObsolete"
]
}
]
```

Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,6 @@ public async Task Resolver_KeyedService()

result.MatchMarkdownSnapshot();
}
#endif

[Fact]
public async Task Resolver_Optional_KeyedService_Does_Not_Exist()
Expand Down Expand Up @@ -350,6 +349,7 @@ public async Task Resolver_Optional_KeyedService_Exists()

result.MatchMarkdownSnapshot();
}
#endif

public sealed class SayHelloService
{
Expand Down

0 comments on commit d7f67ed

Please sign in to comment.