Skip to content

Commit eefc942

Browse files
committed
Fix potential null ref with unknown variable types
1 parent 2d1ca76 commit eefc942

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/GraphQL.Tests/Validation/KnownTypeNamesTests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,28 @@ fragment PetFields on Pet {
2323
");
2424
}
2525

26+
[Fact]
27+
public void unknown_nonnull_type_name_is_invalid()
28+
{
29+
ShouldFailRule(_ =>{
30+
_.Query = @"
31+
query Foo($var: Abcd!) {
32+
user(id: 4) {
33+
pets {
34+
... on Pet { name },
35+
...PetFields
36+
}
37+
}
38+
}
39+
fragment PetFields on Pet {
40+
name
41+
}
42+
";
43+
44+
_.Error(Rule.UnknownTypeMessage("Abcd", null), 2, 37);
45+
});
46+
}
47+
2648
[Fact]
2749
public void unknown_type_names_are_invalid()
2850
{

src/GraphQL/Types/TypeExtensions.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,16 @@ public static class TypeExtensions
77
{
88
public static IGraphType GraphTypeFromType(this IType type, ISchema schema)
99
{
10+
if (type == null) return null;
11+
1012
if (type is NonNullType)
1113
{
1214
var nonnull = (NonNullType)type;
1315
var ofType = GraphTypeFromType(nonnull.Type, schema);
16+
if(ofType == null)
17+
{
18+
return null;
19+
}
1420
var nonnullGraphType = typeof(NonNullGraphType<>).MakeGenericType(ofType.GetType());
1521
var instance = (NonNullGraphType)Activator.CreateInstance(nonnullGraphType);
1622
instance.ResolvedType = ofType;
@@ -21,6 +27,10 @@ public static IGraphType GraphTypeFromType(this IType type, ISchema schema)
2127
{
2228
var list = (ListType)type;
2329
var ofType = GraphTypeFromType(list.Type, schema);
30+
if(ofType == null)
31+
{
32+
return null;
33+
}
2434
var listGraphType = typeof(ListGraphType<>).MakeGenericType(ofType.GetType());
2535
var instance = (ListGraphType)Activator.CreateInstance(listGraphType);
2636
instance.ResolvedType = ofType;

0 commit comments

Comments
 (0)