diff --git a/Sources/Graphiti/Scalar/Scalar.swift b/Sources/Graphiti/Scalar/Scalar.swift index b03f992..5242981 100644 --- a/Sources/Graphiti/Scalar/Scalar.swift +++ b/Sources/Graphiti/Scalar/Scalar.swift @@ -4,8 +4,9 @@ import OrderedCollections /// Represents a scalar type in the schema. /// /// It is **highly** recommended that you do not subclass this type. -/// Instead, modify the encoding/decoding behavior through the `MapEncoder`/`MapDecoder` options available through -/// `Coders` or a custom encoding/decoding on the `ScalarType` itself. +/// Encoding/decoding behavior can be modified through the `MapEncoder`/`MapDecoder` options available through +/// `Coders` or a custom encoding/decoding on the `ScalarType` itself. If you need very custom serialization controls, +/// you may provide your own serialize, parseValue, and parseLiteral implementations. open class Scalar: TypeComponent { // TODO: Change this no longer be an open class @@ -14,22 +15,34 @@ open class Scalar: TypeComponent: TypeComponent Map { try encoder.encode(scalar) } + // TODO: Remove open func, instead relying on a passed closure open func parse(map: Map, decoder: MapDecoder) throws -> ScalarType { try decoder.decode(ScalarType.self, from: map) } + let serialize: ((Any, Coders) throws -> Map)? + let parseValue: ((Map, Coders) throws -> Map)? + let parseLiteral: ((GraphQL.Value, Coders) throws -> Map)? + init( type _: ScalarType.Type, - name: String? + name: String?, + serialize: ((Any, Coders) throws -> Map)? = nil, + parseValue: ((Map, Coders) throws -> Map)? = nil, + parseLiteral: ((GraphQL.Value, Coders) throws -> Map)? = nil ) { + self.serialize = serialize + self.parseValue = parseValue + self.parseLiteral = parseLiteral super.init( name: name ?? Reflection.name(for: ScalarType.self), type: .scalar @@ -62,11 +87,17 @@ open class Scalar: TypeComponent Map)? = nil, + parseValue: ((Map, Coders) throws -> Map)? = nil, + parseLiteral: ((GraphQL.Value, Coders) throws -> Map)? = nil ) { self.init( type: type, - name: name + name: name, + serialize: serialize, + parseValue: parseValue, + parseLiteral: parseLiteral ) } }