Skip to content

Commit f22b2ae

Browse files
landboltsjoemcbride
authored andcommitted
graphql-dotnet#529 adding support for IEnumerable type arguments other than generic lists (graphql-dotnet#533)
1 parent a3fe568 commit f22b2ae

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

src/GraphQL.Tests/Builders/FieldBuilderTests.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System.Collections.Generic;
2+
using System.Collections.ObjectModel;
23
using System.Linq;
34
using GraphQL.StarWars.Types;
45
using GraphQL.Types;
@@ -331,6 +332,30 @@ public void can_get_list_argument()
331332
});
332333
}
333334

335+
[Fact]
336+
public void can_get_collection_argument()
337+
{
338+
var objectType = new ObjectGraphType();
339+
objectType.Field<StringGraphType>()
340+
.Argument<NonNullGraphType<ListGraphType<NonNullGraphType<StringGraphType>>>>("episodes", "episodes")
341+
.Resolve(context =>
342+
{
343+
context.GetArgument<Collection<string>>("episodes").ShouldBe(new Collection<string> { "JEDI", "EMPIRE" });
344+
return null;
345+
});
346+
347+
var field = objectType.Fields.First();
348+
field.Resolver.Resolve(new ResolveFieldContext
349+
{
350+
Arguments = new Dictionary<string, object>
351+
{
352+
{"episodes", new object[] {"JEDI", "EMPIRE" } }
353+
},
354+
FieldDefinition = field
355+
});
356+
}
357+
358+
334359
[Fact]
335360
public void getting_specified_argument_in_resolver_overrides_default_value()
336361
{

src/GraphQL/ObjectExtensions.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,20 @@ public static object GetPropertyValue(this object propertyValue, Type fieldType)
5656
if (fieldType.Name != "String"
5757
&& enumerableInterface != null)
5858
{
59+
IList newArray;
5960
var elementType = enumerableInterface.GetGenericArguments()[0];
6061
var underlyingType = Nullable.GetUnderlyingType(elementType) ?? elementType;
61-
var genericListType = typeof(List<>).MakeGenericType(elementType);
62-
var newArray = (IList) Activator.CreateInstance(genericListType);
62+
var implementsIList = fieldType.GetInterface("IList") != null;
63+
64+
if (implementsIList)
65+
{
66+
newArray = (IList)Activator.CreateInstance(fieldType);
67+
}
68+
else
69+
{
70+
var genericListType = typeof(List<>).MakeGenericType(elementType);
71+
newArray = (IList)Activator.CreateInstance(genericListType);
72+
}
6373

6474
var valueList = propertyValue as IEnumerable;
6575
if (valueList == null) return newArray;

0 commit comments

Comments
 (0)