Skip to content

Commit e7fa923

Browse files
xperiandriViktor Tochonov
authored andcommitted
Changed ObjectListFilter reporting to middleware with full path to a GraphQL fields
1 parent 3c2a25f commit e7fa923

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

src/FSharp.Data.GraphQL.Server.Middleware/MiddlewareDefinitions.fs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ type internal ObjectListFilterMiddleware<'ObjectType, 'ListType>(reportToMetadat
7676
let typesWithListFields =
7777
ctx.TypeMap.GetTypesWithListFields<'ObjectType, 'ListType>()
7878
if Seq.isEmpty typesWithListFields
79-
then failwith <| sprintf "No lists with specified type '%A' where found on object of type '%A'." typeof<'ObjectType> typeof<'ListType>
79+
then failwith $"No lists with specified type '{typeof<'ObjectType>}' where found on object of type '{typeof<'ListType>}'."
8080
let modifiedTypes =
8181
typesWithListFields
8282
|> Seq.map (fun (object, fields) -> modifyFields object fields)
@@ -85,8 +85,8 @@ type internal ObjectListFilterMiddleware<'ObjectType, 'ListType>(reportToMetadat
8585
next ctx
8686

8787
let reportMiddleware (ctx : ExecutionContext) (next : ExecutionContext -> AsyncVal<GQLExecutionResult>) =
88-
let rec collectArgs (acc : KeyValuePair<string, ObjectListFilter> list) (fields : ExecutionInfo list) =
89-
let fieldArgs field =
88+
let rec collectArgs (path: obj list) (acc : KeyValuePair<obj list, ObjectListFilter> list) (fields : ExecutionInfo list) =
89+
let fieldArgs currentPath field =
9090
let filterResults =
9191
field.Ast.Arguments
9292
|> Seq.map (fun x ->
@@ -99,29 +99,30 @@ type internal ObjectListFilterMiddleware<'ObjectType, 'ListType>(reportToMetadat
9999
| Ok filters ->
100100
filters
101101
|> removeNoFilter
102-
|> Seq.map (fun x -> KeyValuePair (field.Ast.AliasOrName, x))
102+
|> Seq.map (fun x -> KeyValuePair (currentPath |> List.rev, x))
103103
|> Seq.toList
104104
|> Ok
105105
match fields with
106106
| [] -> Ok acc
107107
| x :: xs ->
108+
let currentPath = box x.Ast.AliasOrName :: path
108109
let accResult =
109110
match x.Kind with
110111
| SelectFields fields ->
111-
collectArgs acc fields
112+
collectArgs currentPath acc fields
112113
| ResolveCollection field ->
113-
fieldArgs field
114+
fieldArgs currentPath field
114115
| ResolveAbstraction typeFields ->
115116
let fields = typeFields |> Map.toList |> List.collect (fun (_, v) -> v)
116-
collectArgs acc fields
117+
collectArgs currentPath acc fields
117118
| _ -> Ok acc
118119
match accResult with
119120
| Error errs -> Error errs
120-
| Ok acc -> collectArgs acc xs
121+
| Ok acc -> collectArgs path acc xs
121122
let ctxResult = result {
122123
match reportToMetadata with
123124
| true ->
124-
let! args = collectArgs [] ctx.ExecutionPlan.Fields
125+
let! args = collectArgs [] [] ctx.ExecutionPlan.Fields
125126
let filters = ImmutableDictionary.CreateRange args
126127
return { ctx with Metadata = ctx.Metadata.Add("filters", filters) }
127128
| false -> return ctx

src/FSharp.Data.GraphQL.Server.Middleware/TypeSystemExtensions.fs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ open System
44
open System.Collections.Immutable
55
open FsToolkit.ErrorHandling
66

7+
open FSharp.Data.GraphQL
78
open FSharp.Data.GraphQL.Types
89

910
/// Contains extensions for the type system.
@@ -20,14 +21,7 @@ module TypeSystemExtensions =
2021
/// <param name="weight">A float value representing the weight that this field have on the query.</param>
2122
member this.WithQueryWeight (weight : float) : FieldDef<'Val> = this.WithMetadata (this.Metadata.Add ("queryWeight", weight))
2223

23-
type ObjectListFilters = ImmutableDictionary<string, ObjectListFilter>
24-
25-
type ExecutionContext with
26-
27-
/// <summary>
28-
/// Gets the filters applied to the lists.
29-
/// </summary>
30-
member this.Filters = this.Metadata.TryFind<ObjectListFilters> "filters"
24+
open ObjectListFilter.Operators
3125

3226
type ResolveFieldContext with
3327

@@ -40,3 +34,12 @@ module TypeSystemExtensions =
4034
| true, (:? ObjectListFilter as f) -> ValueSome f
4135
| false, _ -> ValueNone
4236
| true, _ -> raise (InvalidOperationException "Invalid filter argument type.")
37+
38+
type ObjectListFilters = ImmutableDictionary<obj list, ObjectListFilter>
39+
40+
type ExecutionContext with
41+
42+
/// <summary>
43+
/// Gets the filters applied to the lists.
44+
/// </summary>
45+
member this.Filters = this.Metadata.TryFind<ObjectListFilters> "filters"

0 commit comments

Comments
 (0)