Created
December 12, 2016 01:01
-
-
Save DanielRobinsonSoftware/9ef4aa2834f2c912b5a3d4ef63ba6188 to your computer and use it in GitHub Desktop.
Mutate a list of custom objects in GraphQL for .NET
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using GraphQL.Types; | |
using Xunit; | |
namespace GraphQL.Tests.Execution.Collections | |
{ | |
public class Foo | |
{ | |
public Foo() | |
{ | |
} | |
public Foo(string name) | |
{ | |
Name = name; | |
} | |
public string Name { get; set; } | |
} | |
public class Root | |
{ | |
public void BarFoos(Foo[] foos) | |
{ | |
foreach (var foo in foos) | |
{ | |
BarFoo(foo); | |
} | |
} | |
public void BarFoo(Foo foo) | |
{ | |
foo.Name = string.Format("{0} - barred", foo.Name); | |
} | |
} | |
public class FooType : ObjectGraphType | |
{ | |
public FooType() | |
{ | |
Name = "Foo"; | |
Field<StringGraphType>("name"); | |
} | |
} | |
public class FoosType : ObjectGraphType<ListGraphType<FooType>> | |
{ | |
public FoosType() | |
{ | |
Name = "Foos"; | |
Field<ListGraphType<FooType>>("foos"); | |
} | |
} | |
public class FoosResultType : ObjectGraphType | |
{ | |
public FoosResultType() | |
{ | |
Field<ListGraphType<FooType>>("foosBarred"); | |
} | |
} | |
public class FoosResult | |
{ | |
public IEnumerable<Foo> FoosBarred { get; set; } | |
} | |
public class FooInput : InputObjectGraphType | |
{ | |
public FooInput() | |
{ | |
Name = "Foo"; | |
Field<StringGraphType>("name"); | |
Field<ObjectGraphType>("foosResult"); | |
} | |
} | |
public class MutationSchema : Schema | |
{ | |
public MutationSchema() | |
{ | |
Mutation = new FooMutations(); | |
} | |
} | |
public class FooMutations : ObjectGraphType | |
{ | |
public FooMutations() | |
{ | |
Name = "Mutation"; | |
Field<FooType>( | |
"barFoo", | |
arguments: new QueryArguments( | |
new QueryArgument<FooInput> | |
{ | |
Name = "barFooQueryArgument" | |
} | |
), | |
resolve: context => | |
{ | |
var root = context.Source as Root; | |
var foo = context.GetArgument<Foo>("barFooQueryArgument"); | |
root.BarFoo(foo); | |
return foo; | |
} | |
); | |
Field<FoosResultType>( | |
"barFoos", | |
arguments: new QueryArguments( | |
new QueryArgument<ListGraphType<FooInput>> | |
{ | |
Name = "barFoosQueryArgument" | |
} | |
), | |
resolve: context => | |
{ | |
var root = context.Source as Root; | |
var foos = context.GetArgument<List<Foo>>("barFoosQueryArgument"); | |
root.BarFoos(foos.ToArray()); | |
return new FoosResult { FoosBarred = foos }; | |
} | |
); | |
} | |
} | |
public class MutationTests : QueryTestBase<MutationSchema> | |
{ | |
[Fact] | |
public void mutation_on_single_object_can_alter_field_values() | |
{ | |
var query = @" | |
mutation M1 { | |
singleFoo: barFoo(barFooQueryArgument: {name: ""First Foo""}) { | |
name | |
} | |
}"; | |
var expected = @"{ | |
""singleFoo"": { | |
""name"": ""First Foo - barred"" | |
} | |
}"; | |
AssertQuerySuccess(query, expected, root: new Root()); | |
} | |
[Fact] | |
public void mutation_on_collection_can_alter_field_values() | |
{ | |
var query = @" | |
mutation M2 { | |
fooCollection: barFoos(barFoosQueryArgument: [ | |
{name: ""First Foo""}, | |
{name: ""Second Foo""} | |
]) { | |
foosBarred { name } | |
} | |
}"; | |
var expected = @"{ | |
""fooCollection"": { | |
""foosBarred"": [ | |
{ | |
""name"": ""First Foo - barred"" | |
}, | |
{ | |
""name"": ""Second Foo - barred"" | |
} | |
] | |
} | |
}"; | |
AssertQuerySuccess(query, expected, root: new Root()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment