Skip to content

Commit 6067bd7

Browse files
authored
Nullable reference type annotations part 3 (graphql-dotnet#2600)
1 parent 1782a68 commit 6067bd7

Some content is hidden

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

60 files changed

+370
-281
lines changed

src/GraphQL.ApiTests/GraphQL.approved.txt

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

src/GraphQL.SystemReactive/SubscriptionExecutionStrategy.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ await listener.AfterExecutionAsync(context)
177177
protected ExecutionNode BuildSubscriptionExecutionNode(ExecutionNode parent, IGraphType graphType, Field field, FieldType fieldDefinition, int? indexInParentNode, object source)
178178
{
179179
if (graphType is NonNullGraphType nonNullFieldType)
180-
graphType = nonNullFieldType.ResolvedType;
180+
graphType = nonNullFieldType.ResolvedType!;
181181

182182
return graphType switch
183183
{

src/GraphQL/Authorization/AuthorizationExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public static class AuthorizationExtensions
2727
/// <see cref="FieldType"/>, <see cref="Schema"/> or others.
2828
/// </param>
2929
/// <returns> List of authorization policy names applied to this metadata provider. </returns>
30-
public static List<string> GetPolicies(this IProvideMetadata provider) => provider.GetMetadata<List<string>>(POLICY_KEY);
30+
public static List<string>? GetPolicies(this IProvideMetadata provider) => provider.GetMetadata<List<string>>(POLICY_KEY);
3131

3232
/// <summary>
3333
/// Gets a boolean value that determines whether any authorization policy is applied to this metadata provider.

src/GraphQL/Builders/ConnectionBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public class ConnectionBuilder<TSourceType>
6161
{
6262
internal const string PAGE_SIZE_METADATA_KEY = "__ConnectionBuilder_PageSize";
6363

64-
private bool IsBidirectional => FieldType.Arguments.Find("before")?.Type == typeof(StringGraphType) && FieldType.Arguments.Find("last")?.Type == typeof(IntGraphType);
64+
private bool IsBidirectional => FieldType.Arguments?.Find("before")?.Type == typeof(StringGraphType) && FieldType.Arguments.Find("last")?.Type == typeof(IntGraphType);
6565

6666
private int? PageSizeFromMetadata
6767
{
@@ -212,7 +212,7 @@ public virtual ConnectionBuilder<TSourceType> Argument<TArgumentGraphType>(strin
212212
Name = name,
213213
};
214214
configure?.Invoke(arg);
215-
FieldType.Arguments.Add(arg);
215+
FieldType.Arguments!.Add(arg);
216216
return this;
217217
}
218218

src/GraphQL/Builders/ConnectionBuilder_Typed.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace GraphQL.Builders
1515
/// </summary>
1616
public class ConnectionBuilder<TSourceType, TReturnType>
1717
{
18-
private bool IsBidirectional => FieldType.Arguments.Find("before")?.Type == typeof(StringGraphType) && FieldType.Arguments.Find("last")?.Type == typeof(IntGraphType);
18+
private bool IsBidirectional => FieldType.Arguments?.Find("before")?.Type == typeof(StringGraphType) && FieldType.Arguments.Find("last")?.Type == typeof(IntGraphType);
1919

2020
private int? PageSizeFromMetadata
2121
{
@@ -145,7 +145,7 @@ public virtual ConnectionBuilder<TSourceType, TReturnType> Argument<TArgumentGra
145145
Name = name,
146146
};
147147
configure?.Invoke(arg);
148-
FieldType.Arguments.Add(arg);
148+
FieldType.Arguments!.Add(arg);
149149
return this;
150150
}
151151

src/GraphQL/DoNotMapClrTypeAttribute.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

35
namespace GraphQL

src/GraphQL/Execution/ExecutionHelper.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public static class ExecutionHelper
2525

2626
var values = new Dictionary<string, ArgumentValue>(definitionArguments.Count);
2727

28-
foreach (var arg in definitionArguments.List)
28+
foreach (var arg in definitionArguments.List!)
2929
{
3030
var value = astArguments?.ValueFor(arg.Name);
3131
var type = arg.ResolvedType;
@@ -49,7 +49,7 @@ public static ArgumentValue CoerceValue(IGraphType type, IValue? input, Variable
4949
{
5050
// validation rules have verified that this is not null; if the validation rule was not executed, it
5151
// is assumed that the caller does not wish this check to be executed
52-
return CoerceValue(nonNull.ResolvedType, input, variables, fieldDefault);
52+
return CoerceValue(nonNull.ResolvedType!, input, variables, fieldDefault);
5353
}
5454

5555
if (input == null)
@@ -78,7 +78,7 @@ public static ArgumentValue CoerceValue(IGraphType type, IValue? input, Variable
7878

7979
if (type is ListGraphType listType)
8080
{
81-
var listItemType = listType.ResolvedType;
81+
var listItemType = listType.ResolvedType!;
8282

8383
if (input is ListValue list)
8484
{
@@ -124,7 +124,7 @@ public static ArgumentValue CoerceValue(IGraphType type, IValue? input, Variable
124124
// default value should be used.
125125

126126
// so: do not pass the field's default value to this method, since the field was specified
127-
obj[field.Name] = CoerceValue(field.ResolvedType, objectField.Value, variables).Value;
127+
obj[field.Name] = CoerceValue(field.ResolvedType!, objectField.Value, variables).Value;
128128
}
129129
else if (field.DefaultValue != null)
130130
{

src/GraphQL/Execution/ExecutionStrategy.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public virtual async Task<ExecutionResult> ExecuteAsync(ExecutionContext context
5454
/// </summary>
5555
protected virtual IObjectGraphType GetOperationRootType(ExecutionContext context)
5656
{
57-
IObjectGraphType type;
57+
IObjectGraphType? type;
5858

5959
switch (context.Operation.OperationType)
6060
{
@@ -102,7 +102,7 @@ protected virtual RootExecutionNode BuildExecutionRootNode(ExecutionContext cont
102102
protected virtual ExecutionNode BuildExecutionNode(ExecutionNode parent, IGraphType graphType, Field field, FieldType fieldDefinition, int? indexInParentNode = null)
103103
{
104104
if (graphType is NonNullGraphType nonNullFieldType)
105-
graphType = nonNullFieldType.ResolvedType;
105+
graphType = nonNullFieldType.ResolvedType!;
106106

107107
return graphType switch
108108
{
@@ -131,7 +131,7 @@ protected virtual bool ShouldIncludeNode(ExecutionContext context, IHaveDirectiv
131131
var directive = directives.Find(context.Schema.Directives.Skip.Name);
132132
if (directive != null)
133133
{
134-
var arg = context.Schema.Directives.Skip.Arguments.Find("if");
134+
var arg = context.Schema.Directives.Skip.Arguments!.Find("if")!;
135135

136136
#pragma warning disable CS8605 // Unboxing a possibly null value.
137137
if ((bool)ExecutionHelper.CoerceValue(arg.ResolvedType, directive.Arguments?.ValueFor(arg.Name), context.Variables, arg.DefaultValue).Value)
@@ -142,7 +142,7 @@ protected virtual bool ShouldIncludeNode(ExecutionContext context, IHaveDirectiv
142142
directive = directives.Find(context.Schema.Directives.Include.Name);
143143
if (directive != null)
144144
{
145-
var arg = context.Schema.Directives.Include.Arguments.Find("if");
145+
var arg = context.Schema.Directives.Include.Arguments!.Find("if")!;
146146

147147
#pragma warning disable CS8605 // Unboxing a possibly null value.
148148
return (bool)ExecutionHelper.CoerceValue(arg.ResolvedType, directive.Arguments?.ValueFor(arg.Name), context.Variables, arg.DefaultValue).Value;
@@ -177,7 +177,7 @@ protected virtual void SetSubFieldNodes(ExecutionContext context, ObjectExecutio
177177
if (fieldDefinition == null)
178178
throw new InvalidOperationException($"Schema is not configured correctly to fetch field '{field.Name}' from type '{parentType.Name}'.");
179179

180-
var node = BuildExecutionNode(parent, fieldDefinition.ResolvedType, field, fieldDefinition);
180+
var node = BuildExecutionNode(parent, fieldDefinition.ResolvedType!, field, fieldDefinition);
181181

182182
subFields[i++] = node;
183183
}
@@ -192,7 +192,7 @@ protected virtual void SetSubFieldNodes(ExecutionContext context, ObjectExecutio
192192
/// Returns a <see cref="FieldType"/> for the specified AST <see cref="Field"/> within a specified parent
193193
/// output graph type within a given schema. For meta-fields, returns the proper meta-field field type.
194194
/// </summary>
195-
protected FieldType GetFieldDefinition(ISchema schema, IObjectGraphType parentType, Field field)
195+
protected FieldType? GetFieldDefinition(ISchema schema, IObjectGraphType parentType, Field field)
196196
{
197197
if (field.Name == schema.SchemaMetaFieldType.Name && schema.Query == parentType)
198198
{
@@ -219,7 +219,7 @@ protected FieldType GetFieldDefinition(ISchema schema, IObjectGraphType parentTy
219219
public virtual Dictionary<string, Field>? GetSubFields(ExecutionContext context, ExecutionNode node)
220220
{
221221
return node.Field?.SelectionSet?.Selections?.Count > 0
222-
? CollectFieldsFrom(context, node.FieldDefinition!.ResolvedType, node.Field.SelectionSet, null)
222+
? CollectFieldsFrom(context, node.FieldDefinition!.ResolvedType!, node.Field.SelectionSet, null)
223223
: null;
224224
}
225225

@@ -336,10 +336,10 @@ protected bool DoesFragmentConditionMatch(ExecutionContext context, string fragm
336336
protected virtual void SetArrayItemNodes(ExecutionContext context, ArrayExecutionNode parent)
337337
{
338338
var listType = (ListGraphType)parent.GraphType!;
339-
var itemType = listType.ResolvedType;
339+
var itemType = listType.ResolvedType!;
340340

341341
if (itemType is NonNullGraphType nonNullGraphType)
342-
itemType = nonNullGraphType.ResolvedType;
342+
itemType = nonNullGraphType.ResolvedType!;
343343

344344
if (!(parent.Result is IEnumerable data))
345345
{

src/GraphQL/Execution/Nodes/ExecutionNode.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ protected ExecutionNode(ExecutionNode? parent, IGraphType? graphType, Field? fie
113113
return objectType;
114114

115115
if (parentType is IAbstractGraphType abstractType)
116-
return abstractType.GetObjectType(Parent!.Result, schema);
116+
return abstractType.GetObjectType(Parent!.Result!, schema);
117117

118118
return null;
119119
}

src/GraphQL/Execution/Nodes/ObjectExecutionNode.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public ObjectExecutionNode(ExecutionNode? parent, IGraphType? graphType, Field?
3636
var objectGraphType = GraphType as IObjectGraphType;
3737

3838
if (GraphType is IAbstractGraphType abstractGraphType)
39-
objectGraphType = abstractGraphType.GetObjectType(Result, schema);
39+
objectGraphType = abstractGraphType.GetObjectType(Result!, schema);
4040

4141
return objectGraphType;
4242
}

src/GraphQL/GraphQLExtensions.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -131,18 +131,18 @@ public static bool IsInputObjectType(this IGraphType type)
131131
typeof(IInputObjectGraphType).IsAssignableFrom(namedType2);
132132
}
133133

134-
internal static bool IsGraphQLTypeReference(this IGraphType type)
134+
internal static bool IsGraphQLTypeReference(this IGraphType? type)
135135
{
136136
var (namedType, _) = type.GetNamedTypes();
137137
return namedType is GraphQLTypeReference;
138138
}
139139

140-
internal static (IGraphType? resolvedType, Type? type) GetNamedTypes(this IGraphType type)
140+
internal static (IGraphType? resolvedType, Type? type) GetNamedTypes(this IGraphType? type)
141141
{
142142
return type switch
143143
{
144-
NonNullGraphType nonNull => nonNull.ResolvedType != null ? GetNamedTypes(nonNull.ResolvedType) : (null, GetNamedType(nonNull.Type)),
145-
ListGraphType list => list.ResolvedType != null ? GetNamedTypes(list.ResolvedType) : (null, GetNamedType(list.Type)),
144+
NonNullGraphType nonNull => nonNull.ResolvedType != null ? GetNamedTypes(nonNull.ResolvedType) : (null, GetNamedType(nonNull.Type!)),
145+
ListGraphType list => list.ResolvedType != null ? GetNamedTypes(list.ResolvedType) : (null, GetNamedType(list.Type!)),
146146
_ => (type, null)
147147
};
148148
}
@@ -316,22 +316,22 @@ public static bool IsSubtypeOf(this IGraphType maybeSubType, IGraphType superTyp
316316
{
317317
if (maybeSubType is NonNullGraphType sub)
318318
{
319-
return IsSubtypeOf(sub.ResolvedType, sup1.ResolvedType);
319+
return IsSubtypeOf(sub.ResolvedType!, sup1.ResolvedType!);
320320
}
321321

322322
return false;
323323
}
324324
else if (maybeSubType is NonNullGraphType sub)
325325
{
326-
return IsSubtypeOf(sub.ResolvedType, superType);
326+
return IsSubtypeOf(sub.ResolvedType!, superType);
327327
}
328328

329329
// If superType type is a list, maybeSubType type must also be a list.
330330
if (superType is ListGraphType sup)
331331
{
332332
if (maybeSubType is ListGraphType sub)
333333
{
334-
return IsSubtypeOf(sub.ResolvedType, sup.ResolvedType);
334+
return IsSubtypeOf(sub.ResolvedType!, sup.ResolvedType!);
335335
}
336336

337337
return false;
@@ -408,7 +408,7 @@ public static bool IsValidDefault(this IGraphType type, object value)
408408

409409
if (type is NonNullGraphType nonNullGraphType)
410410
{
411-
return value == null ? false : nonNullGraphType.ResolvedType.IsValidDefault(value);
411+
return value == null ? false : nonNullGraphType.ResolvedType!.IsValidDefault(value);
412412
}
413413

414414
if (value == null)
@@ -420,7 +420,7 @@ public static bool IsValidDefault(this IGraphType type, object value)
420420
// the value is not an IEnumerable, convert the value using the list's item type.
421421
if (type is ListGraphType listType)
422422
{
423-
var itemType = listType.ResolvedType;
423+
var itemType = listType.ResolvedType!;
424424

425425
if (!(value is string) && value is IEnumerable list)
426426
{
@@ -459,7 +459,7 @@ public static IValue ToAST(this IGraphType type, object value)
459459

460460
if (type is NonNullGraphType nonnull)
461461
{
462-
var astValue = ToAST(nonnull.ResolvedType, value);
462+
var astValue = ToAST(nonnull.ResolvedType!, value);
463463

464464
if (astValue is NullValue)
465465
throw new InvalidOperationException($"Unable to get an AST representation of {(value == null ? "null" : $"'{value}'")} value for type '{nonnull}'.");
@@ -481,7 +481,7 @@ public static IValue ToAST(this IGraphType type, object value)
481481
// the value is not an IEnumerable, convert the value using the list's item type.
482482
if (type is ListGraphType listType)
483483
{
484-
var itemType = listType.ResolvedType;
484+
var itemType = listType.ResolvedType!;
485485

486486
if (!(value is string) && value is IEnumerable list)
487487
{

src/GraphQL/Instrumentation/InstrumentFieldsMiddleware.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class InstrumentFieldsMiddleware : IFieldMiddleware
1717
{
1818
{ "typeName", context.ParentType.Name },
1919
{ "fieldName", context.FieldAst.Name },
20-
{ "returnTypeName", context.FieldDefinition.ResolvedType.ToString() },
20+
{ "returnTypeName", context.FieldDefinition.ResolvedType!.ToString() },
2121
{ "path", context.Path },
2222
};
2323

src/GraphQL/ObjectExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static class ObjectExtensions
2424
/// <typeparam name="T">The type to create.</typeparam>
2525
/// <param name="source">The source of values.</param>
2626
/// <returns>T.</returns>
27-
public static T ToObject<T>(this IDictionary<string, object> source)
27+
public static T ToObject<T>(this IDictionary<string, object?> source)
2828
where T : class
2929
=> (T)ToObject(source, typeof(T));
3030

@@ -43,7 +43,7 @@ public static T ToObject<T>(this IDictionary<string, object> source)
4343
/// In case of configuring field as Field(x => x.FName).Name("FirstName") source dictionary
4444
/// will have 'FirstName' key but its value should be set to 'FName' property of created object.
4545
/// </param>
46-
public static object ToObject(this IDictionary<string, object> source, Type type, IGraphType? mappedType = null)
46+
public static object ToObject(this IDictionary<string, object?> source, Type type, IGraphType? mappedType = null)
4747
{
4848
// Given Field(x => x.FName).Name("FirstName") and key == "FirstName" returns "FName"
4949
string GetPropertyName(string key, out FieldType? field)
@@ -271,7 +271,7 @@ string GetPropertyName(string key, out FieldType? field)
271271
fieldType = nullableFieldType;
272272
}
273273

274-
if (propertyValue is IDictionary<string, object> objects)
274+
if (propertyValue is IDictionary<string, object?> objects)
275275
{
276276
return ToObject(objects, fieldType, mappedType);
277277
}

src/GraphQL/ResolveFieldContext/ResolveFieldContextExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ private static bool TryGetArgument(this IResolveFieldContext context, Type argum
4545
return false;
4646
}
4747

48-
if (arg.Value is IDictionary<string, object> inputObject)
48+
if (arg.Value is IDictionary<string, object?> inputObject)
4949
{
5050
if (argumentType == typeof(object))
5151
{

src/GraphQL/Resolvers/EventStreamResolver.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,32 @@ namespace GraphQL.Resolvers
99
{
1010
public class EventStreamResolver<T> : IEventStreamResolver<T>
1111
{
12-
private readonly Func<IResolveEventStreamContext, IObservable<T>> _subscriber;
12+
private readonly Func<IResolveEventStreamContext, IObservable<T?>> _subscriber;
1313

1414
public EventStreamResolver(
15-
Func<IResolveEventStreamContext, IObservable<T>> subscriber)
15+
Func<IResolveEventStreamContext, IObservable<T?>> subscriber)
1616
{
1717
_subscriber = subscriber ?? throw new ArgumentNullException(nameof(subscriber));
1818
}
1919

20-
public IObservable<T> Subscribe(IResolveEventStreamContext context) => _subscriber(context);
20+
public IObservable<T?> Subscribe(IResolveEventStreamContext context) => _subscriber(context);
2121

22-
IObservable<object> IEventStreamResolver.Subscribe(IResolveEventStreamContext context) => (IObservable<object>)Subscribe(context);
22+
IObservable<object?> IEventStreamResolver.Subscribe(IResolveEventStreamContext context) => (IObservable<object?>)Subscribe(context);
2323
}
2424

2525
public class EventStreamResolver<TSourceType, TReturnType> : IEventStreamResolver<TReturnType>
2626
{
27-
private readonly Func<IResolveEventStreamContext<TSourceType>, IObservable<TReturnType>> _subscriber;
27+
private readonly Func<IResolveEventStreamContext<TSourceType>, IObservable<TReturnType?>> _subscriber;
2828

2929
public EventStreamResolver(
30-
Func<IResolveEventStreamContext<TSourceType>, IObservable<TReturnType>> subscriber)
30+
Func<IResolveEventStreamContext<TSourceType>, IObservable<TReturnType?>> subscriber)
3131
{
3232
_subscriber = subscriber ?? throw new ArgumentNullException(nameof(subscriber));
3333
}
3434

35-
public IObservable<TReturnType> Subscribe(IResolveEventStreamContext context) => _subscriber(context.As<TSourceType>());
35+
public IObservable<TReturnType?> Subscribe(IResolveEventStreamContext context) => _subscriber(context.As<TSourceType>());
3636

37-
IObservable<object> IEventStreamResolver.Subscribe(IResolveEventStreamContext context) => (IObservable<object>)Subscribe(context);
37+
IObservable<object?> IEventStreamResolver.Subscribe(IResolveEventStreamContext context) => (IObservable<object?>)Subscribe(context);
3838
}
3939

4040
public class EventStreamResolver : IEventStreamResolver

0 commit comments

Comments
 (0)