-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from Linq2GraphQL/add-support-custom-scalars
Add support custom scalars
- Loading branch information
Showing
23 changed files
with
693 additions
and
33 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
src/Linq2GraphQL.Client/Converters/CustomScalarConverter.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System.Text.Json.Serialization; | ||
using System.Text.Json; | ||
|
||
|
||
namespace Linq2GraphQL.Client | ||
{ | ||
public class CustomScalar | ||
{ | ||
public string Value { get; set; } | ||
|
||
public override string ToString() => Value; | ||
} | ||
|
||
public class CustomScalarConverter<TScalar> : JsonConverter<TScalar> | ||
where TScalar : CustomScalar, new() | ||
{ | ||
public override TScalar Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
var value = reader.GetString(); | ||
if (value is null) | ||
{ | ||
return null; | ||
} | ||
|
||
var scalar = new TScalar | ||
{ | ||
Value = value | ||
}; | ||
|
||
return scalar; | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, TScalar value, JsonSerializerOptions options) | ||
{ | ||
writer.WriteStringValue(value.Value); | ||
} | ||
|
||
} | ||
} |
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
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
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
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
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
Oops, something went wrong.