Skip to content

Commit 74bc3e6

Browse files
committed
Add serialize to Scalar types.
1 parent 73548fc commit 74bc3e6

15 files changed

+176
-15
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
using GraphQL.Types;
2+
3+
namespace GraphQL.Tests.Execution
4+
{
5+
public class EnumAsInputsTests : QueryTestBase<EnumMutationSchema>
6+
{
7+
[Test]
8+
public void something()
9+
{
10+
AssertQuerySuccess(
11+
@"
12+
mutation createUser {
13+
createUser(userInput:{
14+
profileImage:""myimage.png"",
15+
gender: Female
16+
}){
17+
id
18+
gender
19+
}
20+
}
21+
",
22+
@"{}");
23+
}
24+
}
25+
26+
public class EnumMutationSchema : Schema
27+
{
28+
public EnumMutationSchema()
29+
{
30+
Mutation = new MutationRoot();
31+
}
32+
}
33+
34+
public class UserInputType : InputObjectGraphType
35+
{
36+
public UserInputType()
37+
{
38+
Name = "UserInput";
39+
Description = "User information for user creation";
40+
Field<StringGraphType>("profileImage", "profileImage of user.");
41+
Field<GenderEnum>("gender", "user gender.");
42+
}
43+
}
44+
45+
public class GenderEnum : EnumerationGraphType
46+
{
47+
public GenderEnum()
48+
{
49+
Name = "Gender";
50+
Description = "User gender";
51+
AddValue("NotSpecified", "NotSpecified gender.", 0);
52+
AddValue("Male", "gender Male", 1);
53+
AddValue("Female", "gender female", 2);
54+
}
55+
}
56+
57+
public class MutationRoot : ObjectGraphType
58+
{
59+
public MutationRoot()
60+
{
61+
Name = "MutationRoot";
62+
Description = "GraphQL MutationRoot for supporting create, update, delete or perform custom actions";
63+
64+
Field<UserType>("createUser", "create user api",
65+
new QueryArguments(
66+
new QueryArgument[]
67+
{
68+
new QueryArgument<NonNullGraphType<UserInputType>>
69+
{
70+
Name = "userInput",
71+
Description = "user info details"
72+
}
73+
}
74+
),
75+
context =>
76+
{
77+
return new User();
78+
});
79+
}
80+
}
81+
82+
public class UserType : ObjectGraphType
83+
{
84+
public UserType()
85+
{
86+
Name = "User";
87+
Field<IntGraphType>("id");
88+
Field<GenderEnum>("gender");
89+
}
90+
}
91+
92+
public class User
93+
{
94+
public int Id { get; set; }
95+
public Gender Gender { get; set; }
96+
}
97+
98+
public enum Gender
99+
{
100+
Female,
101+
Male
102+
}
103+
}

src/GraphQL.Tests/Execution/VariablesTests.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,18 @@ public TestComplexScalarType()
1313
Name = "ComplexScalar";
1414
}
1515

16-
public override object ParseValue(object value)
16+
public override object Serialize(object value)
1717
{
1818
if (value is string && value.Equals("DeserializedValue"))
1919
{
2020
return "SerializedValue";
2121
}
2222

23+
return value;
24+
}
25+
26+
public override object ParseValue(object value)
27+
{
2328
if (value is string && value.Equals("SerializedValue"))
2429
{
2530
return "DeserializedValue";

src/GraphQL.Tests/GraphQL.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
<Compile Include="Builders\FieldBuilderTests.cs" />
5858
<Compile Include="Execution\Cancellation\CancellationTests.cs" />
5959
<Compile Include="Execution\AstFromValueTests.cs" />
60+
<Compile Include="Execution\EnumAsInputsTests.cs" />
6061
<Compile Include="Execution\Directives\DirectivesTests.cs" />
6162
<Compile Include="Execution\InputConversionTests.cs" />
6263
<Compile Include="Execution\MutationTests.cs" />

src/GraphQL.Tests/Utilities/SchemaPrinterTests.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,11 @@ public OddType()
553553
Name = "Odd";
554554
}
555555

556+
public override object Serialize(object value)
557+
{
558+
return null;
559+
}
560+
556561
public override object ParseValue(object value)
557562
{
558563
return null;

src/GraphQL/Execution/DocumentExecuter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ public async Task<object> CompleteValue(ExecutionContext context, GraphType fiel
232232
if (fieldType is ScalarGraphType)
233233
{
234234
var scalarType = fieldType as ScalarGraphType;
235-
var coercedValue = scalarType.ParseValue(result);
235+
var coercedValue = scalarType.Serialize(result);
236236
return coercedValue;
237237
}
238238

src/GraphQL/Types/BooleanGraphType.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ public BooleanGraphType()
99
Name = "Boolean";
1010
}
1111

12+
public override object Serialize(object value)
13+
{
14+
if (value is bool)
15+
{
16+
return (bool) value;
17+
}
18+
19+
return false;
20+
}
21+
1222
public override object ParseValue(object value)
1323
{
1424
if (value != null)

src/GraphQL/Types/DateGraphType.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ public DateGraphType()
1414
"to be formatted in accordance with the [ISO-8601](https://en.wikipedia.org/wiki/ISO_8601) standard.";
1515
}
1616

17+
public override object Serialize(object value)
18+
{
19+
return ParseValue(value);
20+
}
21+
1722
public override object ParseValue(object value)
1823
{
1924
string inputValue;

src/GraphQL/Types/DecimalGraphType.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ public DecimalGraphType()
99
Name = "Decimal";
1010
}
1111

12+
public override object Serialize(object value)
13+
{
14+
return ParseValue(value);
15+
}
16+
1217
public override object ParseValue(object value)
1318
{
1419
decimal result;

src/GraphQL/Types/EnumerationGraphType.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,23 @@ public void AddValue(EnumValueDefinition value)
3131

3232
public EnumValues Values { get; private set; }
3333

34-
public override object ParseValue(object value)
34+
public override object Serialize(object value)
3535
{
3636
var found = Values.FirstOrDefault(v => v.Value.Equals(value));
37-
return found != null ? found.Name : null;
37+
return found?.Name;
3838
}
3939

40-
public override object ParseLiteral(IValue value)
40+
public override object ParseValue(object value)
4141
{
42-
var enumVal = value as EnumValue;
43-
return ParseValue(enumVal?.Name);
42+
var found = Values.FirstOrDefault(v =>
43+
StringComparer.InvariantCultureIgnoreCase.Equals(v.Name, value.ToString()));
44+
return found?.Value;
4445
}
4546

46-
public object GetValue(string name)
47+
public override object ParseLiteral(IValue value)
4748
{
48-
var found = Values.FirstOrDefault(v =>
49-
StringComparer.InvariantCultureIgnoreCase.Equals(v.Name, name));
50-
return found != null ? found.Value : null;
49+
var enumVal = value as EnumValue;
50+
return ParseValue(enumVal?.Name);
5151
}
5252
}
5353

src/GraphQL/Types/FloatGraphType.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ public FloatGraphType()
99
Name = "Float";
1010
}
1111

12+
public override object Serialize(object value)
13+
{
14+
return ParseValue(value);
15+
}
16+
1217
public override object ParseValue(object value)
1318
{
1419
double result;

src/GraphQL/Types/GraphType.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ public FieldType Field(
4747
{
4848
if (_fields.Exists(x => x.Name == name))
4949
{
50-
throw new ArgumentOutOfRangeException("name", "A field with that name is already registered.");
50+
throw new ArgumentOutOfRangeException(nameof(name), "A field with that name is already registered.");
5151
}
5252

5353
if (!type.IsSubclassOf(typeof(GraphType)))
5454
{
55-
throw new ArgumentOutOfRangeException("type", "Field type must derive from GraphType.");
55+
throw new ArgumentOutOfRangeException(nameof(type), "Field type must derive from GraphType.");
5656
}
5757

5858
var fieldType = new FieldType
@@ -104,7 +104,7 @@ public override bool Equals(object obj)
104104

105105
public override int GetHashCode()
106106
{
107-
return (Name != null ? Name.GetHashCode() : 0);
107+
return Name?.GetHashCode() ?? 0;
108108
}
109109
}
110110

src/GraphQL/Types/IdGraphType.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ public IdGraphType()
1414
// "as `\"4\"`) or integer (such as `4`) input value will be accepted as an `ID`.";
1515
}
1616

17+
public override object Serialize(object value)
18+
{
19+
return value?.ToString();
20+
}
21+
1722
public override object ParseValue(object value)
1823
{
1924
return value?.ToString().Trim(' ', '"');

src/GraphQL/Types/IntGraphType.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,18 @@ public IntGraphType()
99
Name = "Int";
1010
}
1111

12+
public override object Serialize(object value)
13+
{
14+
return ParseValue(value);
15+
}
16+
1217
public override object ParseValue(object value)
1318
{
19+
if (value == null)
20+
{
21+
return null;
22+
}
23+
1424
/* The Int scalar type represents non-fractional signed whole numeric values.
1525
Int can represent values between -(2^53 - 1) and 2^53 - 1 since represented
1626
in JSON as double-precision floating point numbers specified by IEEE 754 */
@@ -33,7 +43,7 @@ public override object ParseLiteral(IValue value)
3343
var intValue = value as IntValue;
3444
if (intValue != null)
3545
{
36-
return intValue?.Value;
46+
return intValue.Value;
3747
}
3848

3949
var longValue = value as LongValue;

src/GraphQL/Types/ScalarGraphType.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ namespace GraphQL.Types
44
{
55
public abstract class ScalarGraphType : GraphType
66
{
7+
public abstract object Serialize(object value);
8+
79
public abstract object ParseValue(object value);
810

911
public abstract object ParseLiteral(IValue value);

src/GraphQL/Types/StringGraphType.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ public StringGraphType()
99
Name = "String";
1010
}
1111

12+
public override object Serialize(object value)
13+
{
14+
return ParseValue(value);
15+
}
16+
1217
public override object ParseValue(object value)
1318
{
1419
return value != null ? ProcessString(value.ToString()) : null;

0 commit comments

Comments
 (0)