Skip to content

Commit 15150f1

Browse files
committed
Fixed ValidStringScalar implementation and added overload
1 parent 8e1d83d commit 15150f1

2 files changed

Lines changed: 35 additions & 8 deletions

File tree

tests/FSharp.Data.GraphQL.Tests/Variables and Inputs/OptionalsNormalizationTests.ValidString.fs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ module String =
8181
Ok <| s
8282

8383
open Validus.Operators
84+
open System.Text.Json.Serialization
8485

8586
let allowEmpty = ValueOption.ofObj >> ValueOption.filter (not << String.IsNullOrWhiteSpace)
8687

@@ -146,7 +147,33 @@ module Scalars =
146147

147148
type Define with
148149

149-
static member ValidStringScalar<'t>(typeName, createValid : Validator<string, 't voption>, ?description: string) =
150+
static member ValidStringScalar<'t>
151+
(typeName, createValid : Validator<string, 't>, toString : 't -> string, ?description : string)
152+
=
153+
let createValid : string -> ValidationResult<'t> = createValid typeName
154+
Define.WrappedScalar (
155+
name = typeName,
156+
coerceInput =
157+
(function
158+
| Variable e when e.ValueKind = JsonValueKind.String ->
159+
e.GetString ()
160+
|> createValid
161+
|> Result.mapError ValidationErrors.toIGQLErrors
162+
| InlineConstant (StringValue s) ->
163+
s
164+
|> createValid
165+
|> Result.mapError ValidationErrors.toIGQLErrors
166+
| Variable e -> e.GetDeserializeError typeName
167+
| InlineConstant value -> value.GetCoerceError typeName),
168+
coerceOutput =
169+
(function
170+
| :? 't as x -> Some (toString x)
171+
| :? string as s -> s |> Some
172+
| _ -> raise <| System.NotSupportedException ()),
173+
?description = description
174+
)
175+
176+
static member ValidStringScalar<'t>(typeName, createValid : Validator<string, 't voption>, toString : 't -> string, ?description: string) =
150177
let createValid = createValid typeName
151178
Define.WrappedScalar
152179
(name = typeName,
@@ -158,8 +185,8 @@ module Scalars =
158185
| InlineConstant value -> value.GetCoerceError typeName),
159186
coerceOutput =
160187
(function
161-
| :? ('t voption) as x -> x |> string |> Some
162-
| :? 't as x -> Some (string x)
188+
| :? ('t voption) as x -> x |> ValueOption.map toString |> ValueOption.toOption
189+
| :? 't as x -> Some (toString x)
163190
| :? string as s -> s |> Some
164191
| null -> None
165192
| _ -> raise <| System.NotSupportedException ()),

tests/FSharp.Data.GraphQL.Tests/Variables and Inputs/OptionalsNormalizationTests.fs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,11 @@ module Address =
9999

100100
open Scalars
101101

102-
let Line1Type = Define.ValidStringScalar<AddressLine1>("AddressLine1", createLine1, "Address line 1")
103-
let Line2Type = Define.ValidStringScalar<AddressLine2>("AddressLine2", createLine2, "Address line 2")
104-
let ZipCodeType = Define.ValidStringScalar<ZipCode>("AddressZipCode", createZipCode, "Address zip code")
105-
let CityType = Define.ValidStringScalar<City>("City", createCity)
106-
let StateType = Define.ValidStringScalar<State>("State", State.createOrWhitespace)
102+
let Line1Type = Define.ValidStringScalar<AddressLine1>("AddressLine1", createLine1, ValidString.value, "Address line 1")
103+
let Line2Type = Define.ValidStringScalar<AddressLine2>("AddressLine2", createLine2, ValidString.value, "Address line 2")
104+
let ZipCodeType = Define.ValidStringScalar<ZipCode>("AddressZipCode", createZipCode, ValidString.value, "Address zip code")
105+
let CityType = Define.ValidStringScalar<City>("City", createCity, ValidString.value)
106+
let StateType = Define.ValidStringScalar<State>("State", State.createOrWhitespace, ValidString.value)
107107

108108
let InputAddressRecordType =
109109
Define.InputObject<AddressRecord>(

0 commit comments

Comments
 (0)