Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 31 additions & 31 deletions src/FSharp.Data.GraphQL.Client/BaseTypes.fs
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,15 @@ type RecordBase (name : string, properties : RecordProperty seq) =
| :? string -> v // We need this because strings are enumerables, and we don't want to enumerate them recursively as an object
| :? EnumBase as v -> v.GetValue () |> box
| :? RecordBase as v -> box (v.ToDictionary ())
| OptionValue v -> v |> Option.map mapDictionaryValue |> Option.toObj
| OptionValue v -> v |> ValueOption.map mapDictionaryValue |> ValueOption.toObj
| EnumerableValue v -> v |> Array.map mapDictionaryValue |> box
| _ -> v
x.GetProperties ()
|> Seq.choose (fun p ->
|> Seq.vchoose (fun p ->
if not (isNull p.Value) then
Some (p.Name, mapDictionaryValue p.Value)
ValueSome (p.Name, mapDictionaryValue p.Value)
else
None)
ValueNone)
|> dict

override x.ToString () =
Expand Down Expand Up @@ -309,8 +309,8 @@ module internal JsonValueHelper =

let getTypeName (fields : (string * JsonValue) seq) =
fields
|> Seq.tryFind (fun (name, _) -> name = "__typename")
|> Option.map (fun (_, value) ->
|> Seq.vtryFind (fun (name, _) -> name = "__typename")
|> ValueOption.map (fun (_, value) ->
match value with
| JsonValue.String x -> x
| _ -> failwithf "Expected \"__typename\" field to be a string field, but it was %A." value)
Expand Down Expand Up @@ -379,16 +379,16 @@ module internal JsonValueHelper =
| JsonValue.Record props ->
let typeName =
match getTypeName props with
| Some typeName -> typeName
| None -> failwith "Expected type to have a \"__typename\" field, but it was not found."
| ValueSome typeName -> typeName
| ValueNone -> failwith "Expected type to have a \"__typename\" field, but it was not found."
let mapRecordProperty (aliasOrName : string, value : JsonValue) =
let schemaField =
match
schemaField.Fields
|> Array.tryFind (fun f -> f.AliasOrName = aliasOrName)
|> Array.vtryFind (fun f -> f.AliasOrName = aliasOrName)
with
| Some f -> f
| None ->
| ValueSome f -> f
| ValueNone ->
failwithf
"Expected to find field information for field with alias or name \"%s\" of type \"%s\" but it was not found."
aliasOrName
Expand Down Expand Up @@ -479,49 +479,49 @@ module internal JsonValueHelper =
let getErrors (errors : JsonValue[]) =
let tryFindField fieldName (fields : (string * JsonValue)[]) =
fields
|> Array.tryFind (fun (name, _) -> name = fieldName)
|> Option.map snd
|> Array.vtryFind (fun (name, _) -> name = fieldName)
|> ValueOption.map snd

let parsePath =
function
| Some (JsonValue.Array path) ->
let parsePath jsonValueOpt =
match jsonValueOpt with
| ValueSome (JsonValue.Array path) ->
let pathMapper =
function
| JsonValue.String x -> box x
| JsonValue.Integer x -> box x
| _ -> failwith "Error parsing response errors. An item in the path is neither a String nor an Integer."
path |> Array.map pathMapper
| Some JsonValue.Null
| None -> [||]
| ValueSome JsonValue.Null
| ValueNone -> [||]
| _ -> failwith "Error parsing response errors. Path field must be an Array."

let parseLocations =
function
| Some (JsonValue.Array locations) ->
let parseLocations jsonValueOpt =
match jsonValueOpt with
| ValueSome (JsonValue.Array locations) ->
let parseLocation =
function
| JsonValue.Record locationFields ->
match tryFindField "line" locationFields, tryFindField "column" locationFields with
| Some (JsonValue.Integer line), Some (JsonValue.Integer column) -> { Line = line; Column = column }
| ValueSome (JsonValue.Integer line), ValueSome (JsonValue.Integer column) -> { Line = line; Column = column }
| _ -> failwith "Error parsing response errors. A location item must contain Integer fields named \"line\" and \"column\"."
| _ -> failwith "Error parsing response errors. A location item is not a Record."
locations |> Array.map parseLocation
| Some JsonValue.Null
| None -> [||]
| ValueSome JsonValue.Null
| ValueNone -> [||]
| _ -> failwith "Error parsing response errors. Locations field must be an Array."

let parseExtensions =
function
| Some (JsonValue.Record fields) -> Serialization.deserializeMap fields
| Some JsonValue.Null
| None -> Map.empty
let parseExtensions jsonValueOpt =
match jsonValueOpt with
| ValueSome (JsonValue.Record fields) -> Serialization.deserializeMap fields
| ValueSome JsonValue.Null
| ValueNone -> Map.empty
| _ -> failwith "Error parsing response errors. Extensions field must be a Record."

let errorMapper =
function
| JsonValue.Record fields ->
match tryFindField "message" fields with
| Some (JsonValue.String message) -> {
| ValueSome (JsonValue.String message) -> {
Message = message
Locations = tryFindField "locations" fields |> parseLocations
Path = tryFindField "path" fields |> parsePath
Expand Down Expand Up @@ -597,6 +597,6 @@ module VariableMapping =
| :? string -> value
| :? EnumBase as v -> v.GetValue () |> box
| :? RecordBase as v -> v.ToDictionary () |> box
| OptionValue v -> v |> Option.map mapVariableValue |> box
| OptionValue v -> v |> ValueOption.map mapVariableValue |> ValueOption.toObj
| EnumerableValue v -> v |> Array.map mapVariableValue |> box
| v -> v
27 changes: 12 additions & 15 deletions src/FSharp.Data.GraphQL.Client/GraphQLClient.fs
Original file line number Diff line number Diff line change
Expand Up @@ -125,24 +125,21 @@ module GraphQLClient =
let rec tryMapFileVariable (name : string, value : obj) =
match value with
| null
| :? string -> None
| :? Upload as x -> Some [| name, x |]
| OptionValue x -> x |> Option.bind (fun x -> tryMapFileVariable (name, x))
| :? string -> [||]
| :? Upload as x -> [| struct (name, x) |]
| OptionValue x -> x |> ValueOption.map (fun x -> tryMapFileVariable (name, x)) |> ValueOption.defaultValue [||]
| :? IDictionary<string, obj> as x ->
x
|> Seq.collect (fun kvp ->
tryMapFileVariable (name + "." + (kvp.Key.FirstCharLower ()), kvp.Value)
|> Option.defaultValue [||])
|> Array.ofSeq
|> Some
|> Seq.collect (fun kvp -> tryMapFileVariable (name + "." + (kvp.Key.FirstCharLower ()), kvp.Value))
|> Seq.toArray
| EnumerableValue x ->
x
|> Array.mapi (fun ix x -> tryMapFileVariable ($"%s{name}.%i{ix}", x))
|> Array.collect (Option.defaultValue [||])
|> Some
| _ -> None
|> Seq.mapi (fun ix x -> tryMapFileVariable ($"%s{name}.%i{ix}", x))
|> Seq.collect id
|> Seq.toArray
| _ -> [||]
request.Variables
|> Array.collect (tryMapFileVariable >> (Option.defaultValue [||]))
|> Array.collect tryMapFileVariable

let operationContent =
let variables =
Expand Down Expand Up @@ -171,15 +168,15 @@ module GraphQLClient =
let mapContent =
let files =
files
|> Array.mapi (fun ix (name, _) -> ix.ToString (), JsonValue.Array [| JsonValue.String ("variables." + name) |])
|> Array.mapi (fun ix struct (name, _) -> ix.ToString (), JsonValue.Array [| JsonValue.String ("variables." + name) |])
|> JsonValue.Record
let content = new StringContent (files.ToString (JsonSaveOptions.DisableFormatting))
content.Headers.Add ("Content-Disposition", "form-data; name=\"map\"")
content
content.Add (mapContent)
let fileContents =
files
|> Seq.mapi (fun _ (_, value) ->
|> Seq.mapi (fun _ struct (_, value) ->
let content = new StreamContent (value.Stream)
content.Headers.Add ("Content-Disposition", $"form-data; name=\"%s{value.Name}\"; filename=\"%s{value.FileName}\"")
content.Headers.Add ("Content-Type", value.ContentType)
Expand Down
14 changes: 7 additions & 7 deletions src/FSharp.Data.GraphQL.Client/ReflectionPatterns.fs
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ module ReflectionPatterns =
let xtype = x.GetType()
let tryGetValue optionType =
match FSharpValue.GetUnionFields(x, optionType) with
| (_, [|value|]) -> ValueSome (OptionValue Some value)
| _ -> ValueSome (OptionValue None)
| (_, [|value|]) -> ValueSome (OptionValue ValueSome value)
| _ -> ValueSome (OptionValue ValueNone)
if isOption xtype
then tryGetValue xtype
elif isValueOption xtype
Expand All @@ -162,10 +162,10 @@ module ReflectionPatterns =
let isOption = isOption t
match value, isOption with
| null, true -> makeNone t
| OptionValue (Some null), true -> box (makeSome (Convert.ChangeType(null, t)))
| OptionValue (Some value), true -> box (makeSome value)
| OptionValue (Some value), false -> Convert.ChangeType(value, t)
| OptionValue None, false -> Convert.ChangeType(null, t)
| OptionValue None, true -> box (makeNone t)
| OptionValue (ValueSome null), true -> box (makeSome (Convert.ChangeType(null, t)))
| OptionValue (ValueSome value), true -> box (makeSome value)
| OptionValue (ValueSome value), false -> Convert.ChangeType(value, t)
| OptionValue ValueNone, false -> Convert.ChangeType(null, t)
| OptionValue ValueNone, true -> box (makeNone t)
| value, true -> makeSome value
| value, false -> Convert.ChangeType(value, t)
4 changes: 2 additions & 2 deletions src/FSharp.Data.GraphQL.Client/Serialization.fs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ module Serialization =
Tracer.runAndMeasureExecutionTime $"Converted object type %O{t} to JsonValue" (fun _ ->
match x with
| null -> JsonValue.Null
| OptionValue None -> JsonValue.Null
| OptionValue ValueNone -> JsonValue.Null
| :? int as x -> JsonValue.Integer (int x)
| :? float as x -> JsonValue.Float x
| :? string as x -> JsonValue.String x
Expand All @@ -189,7 +189,7 @@ module Serialization =
items
|> Array.map toJsonValue
|> JsonValue.Array
| OptionValue (Some x) -> toJsonValue x
| OptionValue (ValueSome x) -> toJsonValue x
| EnumValue x -> JsonValue.String x
| _ ->
let props = t.GetProperties(BindingFlags.Public ||| BindingFlags.Instance)
Expand Down
Loading
Loading