Skip to content

Commit 1782a68

Browse files
authored
Nullable reference type annotations part 2 (graphql-dotnet#2599)
1 parent cb2f8a7 commit 1782a68

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+320
-250
lines changed

src/GraphQL.ApiTests/GraphQL.approved.txt

Lines changed: 84 additions & 84 deletions
Large diffs are not rendered by default.

src/GraphQL.SystemReactive/SubscriptionExecutionStrategy.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ private async Task<IDictionary<string, IObservable<ExecutionResult>>> ExecuteSub
6868
{
6969
FieldAst = node.Field,
7070
FieldDefinition = node.FieldDefinition,
71-
ParentType = node.GetParentType(context.Schema),
71+
ParentType = node.GetParentType(context.Schema)!,
7272
Arguments = arguments,
7373
Source = source,
7474
Schema = context.Schema,
@@ -87,7 +87,7 @@ private async Task<IDictionary<string, IObservable<ExecutionResult>>> ExecuteSub
8787
var eventStreamField = node.FieldDefinition as EventStreamFieldType;
8888

8989

90-
IObservable<object> subscription;
90+
IObservable<object?> subscription;
9191

9292
if (eventStreamField?.Subscriber != null)
9393
{
@@ -103,7 +103,7 @@ private async Task<IDictionary<string, IObservable<ExecutionResult>>> ExecuteSub
103103
}
104104

105105
return subscription
106-
.Select(value => BuildSubscriptionExecutionNode(node.Parent!, node.GraphType!, node.Field, node.FieldDefinition, node.IndexInParentNode, value))
106+
.Select(value => BuildSubscriptionExecutionNode(node.Parent!, node.GraphType!, node.Field, node.FieldDefinition, node.IndexInParentNode, value!))
107107
.SelectMany(async executionNode =>
108108
{
109109
if (context.Listeners != null)

src/GraphQL/Builders/ConnectionBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ public virtual void Resolve(Func<IResolveConnectionContext<TSourceType>, object>
361361
/// <summary>
362362
/// Sets the resolver method for the connection field.
363363
/// </summary>
364-
public virtual void ResolveAsync(Func<IResolveConnectionContext<TSourceType>, Task<object>> resolver)
364+
public virtual void ResolveAsync(Func<IResolveConnectionContext<TSourceType>, Task<object?>> resolver)
365365
{
366366
var isUnidirectional = !IsBidirectional;
367367
var pageSize = PageSizeFromMetadata;

src/GraphQL/Builders/ConnectionBuilder_Typed.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ public virtual void Resolve(Func<IResolveConnectionContext<TSourceType>, TReturn
259259
/// Sets the resolver method for the connection field. This method must be called after
260260
/// <see cref="PageSize(int?)"/> and/or <see cref="Bidirectional"/> have been called.
261261
/// </summary>
262-
public virtual void ResolveAsync(Func<IResolveConnectionContext<TSourceType>, Task<TReturnType>> resolver)
262+
public virtual void ResolveAsync(Func<IResolveConnectionContext<TSourceType>, Task<TReturnType?>> resolver)
263263
{
264264
var isUnidirectional = !IsBidirectional;
265265
var pageSize = PageSizeFromMetadata;

src/GraphQL/DirectivesExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ internal static void AddAppliedDirectivesField<TSourceType>(this ComplexGraphTyp
178178
description: $"Directives applied to the {element}",
179179
resolve: async context =>
180180
{
181-
if (context.Source.HasAppliedDirectives())
181+
if (context.Source!.HasAppliedDirectives())
182182
{
183183
var appliedDirectives = context.Source.GetAppliedDirectives();
184184
var result = context.ArrayPool.Rent<AppliedDirective>(appliedDirectives!.Count);

src/GraphQL/Execution/DocumentExecuter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ protected virtual ExecutionContext BuildExecutionContext(ExecutionOptions option
274274
Operation = operation,
275275
Variables = variables,
276276
Errors = new ExecutionErrors(),
277-
Extensions = new Dictionary<string, object>(),
277+
Extensions = new Dictionary<string, object?>(),
278278
CancellationToken = options.CancellationToken,
279279

280280
Metrics = metrics,

src/GraphQL/Execution/ExecutionContext.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class ExecutionContext : IExecutionContext, IExecutionArrayPool, IDisposa
2828
public object? RootValue { get; set; }
2929

3030
/// <inheritdoc/>
31-
public IDictionary<string, object> UserContext { get; set; }
31+
public IDictionary<string, object?> UserContext { get; set; }
3232

3333
/// <inheritdoc/>
3434
public Operation Operation { get; set; }
@@ -58,7 +58,7 @@ public class ExecutionContext : IExecutionContext, IExecutionArrayPool, IDisposa
5858
public int? MaxParallelExecutionCount { get; set; }
5959

6060
/// <inheritdoc/>
61-
public Dictionary<string, object> Extensions { get; set; }
61+
public Dictionary<string, object?> Extensions { get; set; }
6262
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
6363

6464
/// <inheritdoc/>

src/GraphQL/Execution/ExecutionOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class ExecutionOptions : IProvideUserContext
4646
public IEnumerable<IValidationRule>? ValidationRules { get; set; }
4747

4848
/// <inheritdoc/>
49-
public IDictionary<string, object> UserContext { get; set; } = new Dictionary<string, object>();
49+
public IDictionary<string, object?> UserContext { get; set; } = new Dictionary<string, object?>();
5050

5151
/// <summary>Complexity constraints for <see cref="IComplexityAnalyzer"/> to use to validate maximum query complexity</summary>
5252
public ComplexityConfiguration? ComplexityConfiguration { get; set; }

src/GraphQL/Execution/ExecutionResult.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public class ExecutionResult
5353
/// <summary>
5454
/// Returns additional user-defined data; see <see cref="IExecutionContext.Extensions"/> and <see cref="IResolveFieldContext.Extensions"/>. This property is serialized as part of the GraphQL json response.
5555
/// </summary>
56-
public Dictionary<string, object>? Extensions { get; set; }
56+
public Dictionary<string, object?>? Extensions { get; set; }
5757

5858
/// <summary>
5959
/// Initializes a new instance with all properties set to their defaults.

src/GraphQL/Execution/IExecutionContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public interface IExecutionContext : IProvideUserContext
8585
/// The response map may also contain an entry with key extensions. This entry is reserved for implementors to extend the
8686
/// protocol however they see fit, and hence there are no additional restrictions on its contents.
8787
/// </summary>
88-
Dictionary<string, object> Extensions { get; }
88+
Dictionary<string, object?> Extensions { get; }
8989

9090
/// <summary>
9191
/// The service provider for the executing request. Typically this is a scoped service provider

src/GraphQL/Execution/IProvideUserContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ public interface IProvideUserContext
1515
/// A custom implementation of <see cref="IDictionary{TKey, TValue}">IDictionary</see> may be
1616
/// used in place of the default <see cref="Dictionary{TKey, TValue}">Dictionary</see>.
1717
/// </summary>
18-
IDictionary<string, object> UserContext { get; }
18+
IDictionary<string, object?> UserContext { get; }
1919
}
2020
}

src/GraphQL/Instrumentation/ApolloTrace.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#nullable enable
2+
13
using System;
24
using System.Collections.Generic;
35

@@ -95,22 +97,22 @@ public class ResolverTrace : OperationTrace
9597
/// <summary>
9698
/// Gets or sets the path of the field.
9799
/// </summary>
98-
public List<object> Path { get; set; }
100+
public List<object>? Path { get; set; }
99101

100102
/// <summary>
101103
/// Gets or sets the parent graph type name.
102104
/// </summary>
103-
public string ParentType { get; set; }
105+
public string? ParentType { get; set; }
104106

105107
/// <summary>
106108
/// Gets or sets the field name.
107109
/// </summary>
108-
public string FieldName { get; set; }
110+
public string? FieldName { get; set; }
109111

110112
/// <summary>
111113
/// Gets or sets the returned graph type name.
112114
/// </summary>
113-
public string ReturnType { get; set; }
115+
public string? ReturnType { get; set; }
114116
}
115117
}
116118
}

src/GraphQL/Instrumentation/ApolloTracingExtensions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#nullable enable
2+
13
using System;
24
using System.Collections.Generic;
35
using System.Linq;
@@ -22,7 +24,7 @@ public static void EnrichWithApolloTracing(this ExecutionResult result, DateTime
2224
{
2325
var perf = result?.Perf;
2426
if (perf != null)
25-
(result.Extensions ??= new Dictionary<string, object>())["tracing"] = CreateTrace(perf, start);
27+
(result!.Extensions ??= new Dictionary<string, object?>())["tracing"] = CreateTrace(perf, start);
2628
}
2729

2830
/// <summary>

src/GraphQL/Instrumentation/FieldMiddlewareBuilder.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#nullable enable
2+
13
using System;
24
using System.Threading.Tasks;
35
using GraphQL.Resolvers;
@@ -9,7 +11,7 @@ namespace GraphQL.Instrumentation
911
/// </summary>
1012
public class FieldMiddlewareBuilder : IFieldMiddlewareBuilder
1113
{
12-
private Func<FieldMiddlewareDelegate, FieldMiddlewareDelegate> _middleware;
14+
private Func<FieldMiddlewareDelegate, FieldMiddlewareDelegate>? _middleware;
1315

1416
/// <inheritdoc/>
1517
public IFieldMiddlewareBuilder Use(Func<FieldMiddlewareDelegate, FieldMiddlewareDelegate> middleware)
@@ -33,7 +35,7 @@ public IFieldMiddlewareBuilder Use(Func<FieldMiddlewareDelegate, FieldMiddleware
3335
private static readonly FieldMiddlewareDelegate _defaultDelegate = context => Task.FromResult(NameFieldResolver.Instance.Resolve(context));
3436

3537
/// <inheritdoc/>
36-
public Func<FieldMiddlewareDelegate, FieldMiddlewareDelegate> Build()
38+
public Func<FieldMiddlewareDelegate, FieldMiddlewareDelegate>? Build()
3739
{
3840
if (_middleware == null)
3941
return null;

src/GraphQL/Instrumentation/FieldMiddlewareBuilderExtensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#nullable enable
2+
13
using System;
24
using GraphQL.Types;
35

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#nullable enable
2+
13
using System.Threading.Tasks;
24

35
namespace GraphQL.Instrumentation
@@ -6,5 +8,5 @@ namespace GraphQL.Instrumentation
68
/// Encapsulates a method that has a parameter of type <see cref="IResolveFieldContext"/> and
79
/// asynchronously returns an object.
810
/// </summary>
9-
public delegate Task<object> FieldMiddlewareDelegate(IResolveFieldContext context);
11+
public delegate Task<object?> FieldMiddlewareDelegate(IResolveFieldContext context);
1012
}

src/GraphQL/Instrumentation/IFieldMiddleware.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#nullable enable
2+
13
using System.Threading.Tasks;
24

35
namespace GraphQL.Instrumentation
@@ -13,6 +15,6 @@ public interface IFieldMiddleware
1315
/// <param name="context">Contains parameters pertaining to the currently executing field.</param>
1416
/// <param name="next">The delegate representing the remaining middleware and field resolver in the pipeline.</param>
1517
/// <returns>Asynchronously returns the result for the field.</returns>
16-
Task<object> Resolve(IResolveFieldContext context, FieldMiddlewareDelegate next);
18+
Task<object?> Resolve(IResolveFieldContext context, FieldMiddlewareDelegate next);
1719
}
1820
}

src/GraphQL/Instrumentation/IFieldMiddlewareBuilder.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#nullable enable
2+
13
using System;
24
using GraphQL.Types;
35

@@ -21,6 +23,6 @@ public interface IFieldMiddlewareBuilder
2123
/// Returns a transform for field resolvers, or <see langword="null"/> if no middleware is defined.
2224
/// The transform is a cumulation of all middlewares configured within this builder.
2325
/// </summary>
24-
Func<FieldMiddlewareDelegate, FieldMiddlewareDelegate> Build();
26+
Func<FieldMiddlewareDelegate, FieldMiddlewareDelegate>? Build();
2527
}
2628
}

src/GraphQL/Instrumentation/InstrumentFieldsMiddleware.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#nullable enable
2+
13
using System.Collections.Generic;
24
using System.Threading.Tasks;
35

@@ -9,9 +11,9 @@ namespace GraphQL.Instrumentation
911
public class InstrumentFieldsMiddleware : IFieldMiddleware
1012
{
1113
/// <inheritdoc/>
12-
public async Task<object> Resolve(IResolveFieldContext context, FieldMiddlewareDelegate next)
14+
public async Task<object?> Resolve(IResolveFieldContext context, FieldMiddlewareDelegate next)
1315
{
14-
var metadata = new Dictionary<string, object>
16+
var metadata = new Dictionary<string, object?>
1517
{
1618
{ "typeName", context.ParentType.Name },
1719
{ "fieldName", context.FieldAst.Name },

src/GraphQL/Instrumentation/Metrics.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#nullable enable
2+
13
using System;
24
using System.Collections.Generic;
35
using System.Linq;
@@ -10,8 +12,8 @@ namespace GraphQL.Instrumentation
1012
public class Metrics
1113
{
1214
private ValueStopwatch _stopwatch;
13-
private readonly List<PerfRecord> _records;
14-
private PerfRecord _main;
15+
private readonly List<PerfRecord>? _records;
16+
private PerfRecord? _main;
1517

1618
/// <summary>
1719
/// Gets an instance of the metrics for which metrics collection is disabled.
@@ -38,15 +40,15 @@ public Metrics(bool enabled = true)
3840
/// Logs the start of the execution.
3941
/// </summary>
4042
/// <param name="operationName">The name of the GraphQL operation.</param>
41-
public Metrics Start(string operationName)
43+
public Metrics Start(string? operationName)
4244
{
4345
if (Enabled)
4446
{
4547
if (_main != null)
4648
throw new InvalidOperationException("Metrics.Start has already been called");
4749

4850
_main = new PerfRecord("operation", operationName, 0);
49-
_records.Add(_main);
51+
_records!.Add(_main);
5052
_stopwatch = ValueStopwatch.StartNew();
5153
}
5254

@@ -56,7 +58,7 @@ public Metrics Start(string operationName)
5658
/// <summary>
5759
/// Sets the name of the GraphQL operation.
5860
/// </summary>
59-
public Metrics SetOperationName(string name)
61+
public Metrics SetOperationName(string? name)
6062
{
6163
if (Enabled && _main != null)
6264
_main.Subject = name;
@@ -67,7 +69,7 @@ public Metrics SetOperationName(string name)
6769
/// <summary>
6870
/// Records an performance metric.
6971
/// </summary>
70-
public Marker Subject(string category, string subject, Dictionary<string, object> metadata = null)
72+
public Marker Subject(string category, string? subject, Dictionary<string, object?>? metadata = null)
7173
{
7274
if (!Enabled)
7375
return Marker.Empty;
@@ -76,15 +78,15 @@ public Marker Subject(string category, string subject, Dictionary<string, object
7678
throw new InvalidOperationException("Metrics.Start should be called before calling Metrics.Subject");
7779

7880
var record = new PerfRecord(category, subject, _stopwatch.Elapsed.TotalMilliseconds, metadata);
79-
lock (_records)
81+
lock (_records!)
8082
_records.Add(record);
8183
return new Marker(record, _stopwatch);
8284
}
8385

8486
/// <summary>
8587
/// Returns the collected performance metrics.
8688
/// </summary>
87-
public PerfRecord[] Finish()
89+
public PerfRecord[]? Finish()
8890
{
8991
if (!Enabled)
9092
return null;

src/GraphQL/Instrumentation/PerfRecord.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#nullable enable
2+
13
using System.Collections.Generic;
24
using System.Diagnostics;
35

@@ -12,7 +14,7 @@ public class PerfRecord
1214
/// <summary>
1315
/// Initializes a new instance with the specified properties.
1416
/// </summary>
15-
public PerfRecord(string category, string subject, double start, Dictionary<string, object> metadata = null)
17+
public PerfRecord(string category, string? subject, double start, Dictionary<string, object?>? metadata = null)
1618
{
1719
Category = category;
1820
Subject = subject;
@@ -33,12 +35,12 @@ public PerfRecord(string category, string subject, double start, Dictionary<stri
3335
/// <summary>
3436
/// Gets or sets the subject name.
3537
/// </summary>
36-
public string Subject { get; set; }
38+
public string? Subject { get; set; }
3739

3840
/// <summary>
3941
/// Gets or sets a dictionary of additional metadata.
4042
/// </summary>
41-
public Dictionary<string, object> Metadata { get; set; }
43+
public Dictionary<string, object?>? Metadata { get; set; }
4244

4345
/// <summary>
4446
/// Gets or sets the start time, represented as an offset in milliseconds from starting the GraphQL operation's execution.
@@ -59,10 +61,10 @@ public PerfRecord(string category, string subject, double start, Dictionary<stri
5961
/// Returns metadata for the specified key. Similar to <see cref="Metadata"/>[<paramref name="key"/>], but returns <c>default</c>
6062
/// if <see cref="Metadata"/> is <c>null</c> or the specified key does not exist.
6163
/// </summary>
62-
public T MetaField<T>(string key)
64+
public T? MetaField<T>(string key)
6365
{
6466
var local = Metadata;
65-
return local != null && local.TryGetValue(key, out var value) ? (T)value : default;
67+
return local != null && local.TryGetValue(key, out var value) ? (T?)value : default;
6668
}
6769
}
6870
}

src/GraphQL/Instrumentation/ValueStopwatch.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#nullable enable
2+
13
using System;
24
using System.Diagnostics;
35

src/GraphQL/ObjectExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ string GetPropertyName(string key, out FieldType? field)
188188
/// will have 'FirstName' key but its value should be set to 'FName' property of created object.
189189
/// </param>
190190
/// <remarks>There is special handling for strings, IEnumerable&lt;T&gt;, Nullable&lt;T&gt;, and Enum.</remarks>
191-
public static object? GetPropertyValue(this object propertyValue, Type fieldType, IGraphType? mappedType = null)
191+
public static object? GetPropertyValue(this object? propertyValue, Type fieldType, IGraphType? mappedType = null)
192192
{
193193
// Short-circuit conversion if the property value already of the right type
194194
if (propertyValue == null || fieldType == typeof(object) || fieldType.IsInstanceOfType(propertyValue))

src/GraphQL/Reflection/AccessorFieldResolver.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#nullable enable
2+
13
using System;
24
using GraphQL.Resolvers;
35

@@ -14,7 +16,7 @@ public AccessorFieldResolver(IAccessor accessor, IServiceProvider serviceProvide
1416
_serviceProvider = serviceProvider;
1517
}
1618

17-
public object Resolve(IResolveFieldContext context)
19+
public object? Resolve(IResolveFieldContext context)
1820
{
1921
var arguments = ReflectionHelper.BuildArguments(_accessor.Parameters, context);
2022

0 commit comments

Comments
 (0)