-
Hi, I'm trying to use the spatial stuff with StrawberryShake (SS). The """
A position is an array of numbers. There MUST be two or more elements. The first two elements are longitude and latitude, or easting and northing, precisely in that order and using decimal numbers. Altitude or elevation MAY be included as an optional third element.
"""
scalar Position As I understand it, SS has a list of scalars it supports natively and any others must be added manually. Therefore I did the following; in
and in a public class PositionSerializer : ScalarSerializer<double[]>
{
public PositionSerializer() : base("Position")
{
}
} However, this results in SS generating invalid code; note the private global::ShakeIt.GraphQL.State.GeoJSONPointTypeData Deserialize_NonNullableIAddIncident_Node_Incident_Location(global::System.Text.Json.JsonElement? obj)
{
if (!obj.HasValue)
{
throw new global::System.ArgumentNullException();
}
var typename = obj.Value.GetProperty("__typename").GetString();
if (typename?.Equals("GeoJSONPointType", global::System.StringComparison.Ordinal) ?? false)
{
return new global::ShakeIt.GraphQL.State.GeoJSONPointTypeData(typename, coordinates: Deserialize_Double[](global::StrawberryShake.Json.JsonElementExtensions.GetPropertyOrNull(obj, "coordinates")));
}
throw new global::System.NotSupportedException();
}
private global::System.Double[]? Deserialize_Double[](global::System.Text.Json.JsonElement? obj) { if
( !obj.HasValue ) { return null ; } return _positionParser.Parse(obj.Value ! ) ; } Am I doing this correctly? If not, what am I doing wrong? thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Ok, engaging the grey matter I found a solution - simply sidestep the issue by using a custom class which wraps a collection of the value. e.g. In some .cs file. public class Position : Collection<double> { }
public class PositionSerializer : ScalarSerializer<JsonElement, Position>
{
public PositionSerializer() : base("Position") {}
public override Position Parse(JsonElement serializedValue)
=> serializedValue.Deserialize<Position>() ?? throw new InvalidOperationException();
protected override JsonElement Format(Position runtimeValue)
=> System.Text.Json.JsonSerializer.SerializeToElement(runtimeValue);
} In extend scalar Position
@serializationType(name: "global::System.Text.Json.JsonElement")
@runtimeType(name: "global::MyApp.Position") In program startup serviceCollection
.AddSerializer<PositionSerializer>()
.AddTestClient(); Then touch wood, everything seems to work now. |
Beta Was this translation helpful? Give feedback.
-
Possible solution to #3685. |
Beta Was this translation helpful? Give feedback.
Ok, engaging the grey matter I found a solution - simply sidestep the issue by using a custom class which wraps a collection of the value.
e.g.
In some .cs file.
In
schema.extensions.graphql