-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathErrors.fs
More file actions
308 lines (263 loc) · 10.9 KB
/
Copy pathErrors.fs
File metadata and controls
308 lines (263 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
namespace FSharp.Data.GraphQL
open System
open System.Collections.Generic
open System.Collections.ObjectModel
open System.Linq
open System.Runtime
open System.Runtime.InteropServices
open System.Text.Json.Serialization
type internal FieldPath = obj list
type IGQLError =
abstract member Message : string with get
type IGQLExceptionError =
inherit IGQLError
abstract member Exception : Exception with get
type internal ICoerceGQLError =
inherit IGQLError
abstract member VariableMessage : string with get
type IGQLErrorExtensions =
abstract member Extensions : IReadOnlyDictionary<string, obj> voption with get
[<Struct>]
type GQLProblemLocation = { Line : int; Column : int }
[<RequireQualifiedAccess>]
module CustomErrorFields =
[<Literal>]
let Kind = "kind"
[<Literal>]
let Path = "path"
[<Literal>]
let VariableName = "variableName"
[<Literal>]
let VariableType = "variableType"
[<Literal>]
let ArgumentName = "argumentName"
[<Literal>]
let ArgumentType = "argumentType"
[<Literal>]
let ObjectType = "objectType"
[<Literal>]
let FieldType = "fieldType"
type ErrorKind =
/// GraphQL request validation and planning
| Validation
/// Input variables or inline values coercion
| InputCoercion
/// Input variable object or inline object fields validation as a whole
| InputObjectValidation
/// GraphQL field execution
| Execution
module IGQLError =
let create message = { new IGQLError with member _.Message = message }
let createList message = [create message]
let createResultErrorList message = Result.Error (createList message)
#nowarn "0386"
/// <summary>
/// A machine-readable format for specifying errors in GraphQL API responses based on <see href="http://spec.graphql.org/October2021/#sec-Errors"/>.
/// </summary>
//[<JsonConverter(typeof(ProblemDetailsJsonConverter))>]
[<CustomEquality; NoComparison>]
type GQLProblemDetails = {
/// <summary>
/// A short, human-readable summary of the problem type. It SHOULD NOT change from occurrence to occurrence
/// of the problem, except for purposes of localization.
/// </summary>
[<JsonPropertyName("message")>]
Message : string
/// <summary>
/// The possible exception associated with this error. It won't be serialized.
/// </summary>
[<JsonIgnore>]
Exception : Exception voption
/// <summary>
/// An array of fields path segments that that identify the specific field path in a GraphQL query where the resolution problem occurs.
/// </summary>
[<JsonPropertyName("path")>]
Path : FieldPath Skippable
/// <summary>
/// An array of line and columnt pairs that identify the specific positions in a GraphQL query where the problem occurs.
/// </summary>
[<JsonPropertyName("locations")>]
Locations : GQLProblemLocation list Skippable
/// <summary>
/// Gets the <see cref="IDictionary{TKey, TValue}"/> for extension members.
/// <para>
/// Problem type definitions MAY extend the problem details object with additional members. Extension members appear in the same namespace as
/// other members of a problem type.
/// </para>
/// </summary>
/// <remarks>
/// The round-tripping behavior for <see cref="Extensions"/> is determined by the implementation of the Input \ Output formatters.
/// In particular, complex types or collection types may not round-trip to the original type when using the built-in JSON or XML formatters.
/// </remarks>
[<JsonPropertyName("extensions")>]
Extensions : IReadOnlyDictionary<string, obj> Skippable
} with
//member internal error.WithErrorKind (errorKind : ErrorKind) =
// let extensions =
// error.Extensions
// |> Skippable.map (fun extensions -> GQLProblemDetails.SetErrorKind errorKind extensions)
// if error.Extensions = extensions then error
// else { error with Extensions = extensions }
//static member internal withErrorKind (errorKind : ErrorKind) (error: GQLProblemDetails) =
// let extensions =
// error.Extensions
// |> Skippable.map (fun extensions -> GQLProblemDetails.SetErrorKind errorKind extensions)
// |> Skippable.defaultWith (fun () -> Dictionary<string, obj> 1 |> GQLProblemDetails.SetErrorKind errorKind)
// |> Include
// if error.Extensions = extensions then error
// else { error with Extensions = extensions }
static member SetErrorKind (errorKind : ErrorKind) (extensions : IReadOnlyDictionary<string, obj>) =
let mutableExtensions =
match extensions with
| :? IDictionary<string, obj> as extensions when not extensions.IsReadOnly -> extensions
| _ ->
#if NETSTANDARD2_0
let dictionary = Dictionary<string, obj> (extensions.Count)
for pair in extensions do
dictionary.Add (pair.Key, pair.Value)
dictionary
#else
Dictionary<string, obj> (collection = (extensions :> IEnumerable<KeyValuePair<string, obj>>))
#endif
mutableExtensions[CustomErrorFields.Kind] <- errorKind
match mutableExtensions with
| :? IReadOnlyDictionary<string, obj> as extensions -> extensions
| _ -> ReadOnlyDictionary<string, obj> mutableExtensions
static member internal CreateWithKind (message, kind : ErrorKind, ?path) = {
Message = message
Exception = ValueNone
Path = path |> Skippable.ofOption
Locations = Skip
Extensions = Dictionary<string, obj> 1 |> GQLProblemDetails.SetErrorKind kind |> Include
}
static member Create (message, extensions : IReadOnlyDictionary<string, obj>) = {
Message = message
Exception = ValueNone
Path = Skip
Locations = Skip
Extensions = Include extensions
}
static member Create (message, [<Optional>] extensions) = {
Message = message
Exception = ValueNone
Path = Skip
Locations = Skip
Extensions = extensions
}
static member Create (message : string, ex : Exception, [<Optional>] path : FieldPath Skippable, [<Optional>] extensions : IReadOnlyDictionary<string, obj> Skippable) = {
Message = message
Exception = ValueSome ex
Path = path
Locations = Skip
Extensions = extensions
}
static member Create (message : string, ex : Exception, path : FieldPath, [<Optional>] extensions : IReadOnlyDictionary<string, obj> Skippable) = {
Message = message
Exception = ValueSome ex
Path = Include path
Locations = Skip
Extensions = extensions
}
static member Create (message, path, extensions : IReadOnlyDictionary<string, obj>) = {
Message = message
Exception = ValueNone
Path = Include path
Locations = Skip
Extensions = Include extensions
}
static member Create (message, path, [<Optional>] extensions) = {
Message = message
Exception = ValueNone
Path = Include path
Locations = Skip
Extensions = extensions
}
static member Create (message, locations, extensions : IReadOnlyDictionary<string, obj>) = {
Message = message
Exception = ValueNone
Path = Skip
Locations = Include locations
Extensions = Include extensions
}
static member Create (message, locations, extensions) = {
Message = message
Exception = ValueNone
Path = Skip
Locations = Include locations
Extensions = extensions
}
static member OfError (error : IGQLError) =
let extensions =
match error with
| :? IGQLErrorExtensions as ext -> ext.Extensions |> Skippable.ofValueOption
| _ -> Skip
let message =
match error with
| :? ICoerceGQLError as error -> error.VariableMessage + error.Message
| _ -> error.Message
match error with
| :? IGQLExceptionError as exceptionError ->
GQLProblemDetails.Create(exceptionError.Message, exceptionError.Exception, extensions = extensions)
| _ ->
GQLProblemDetails.Create (message, extensions)
static member OfFieldError (path : FieldPath) (error : IGQLError) =
let extensions =
match error with
| :? IGQLErrorExtensions as ext -> ext.Extensions |> Skippable.ofValueOption
| _ -> Skip
let message =
match error with
| :? ICoerceGQLError as error -> error.VariableMessage + error.Message
| _ -> error.Message
match error with
| :? IGQLExceptionError as exceptionError ->
GQLProblemDetails.Create(exceptionError.Message, exceptionError.Exception, path, extensions)
| _ ->
GQLProblemDetails.Create (message, path, extensions)
static member internal OfFieldExecutionError (path : FieldPath) (error : IGQLError) =
let extensions =
match error with
| :? IGQLErrorExtensions as ext -> ext.Extensions
| _ -> ValueNone
|> ValueOption.defaultWith (fun () -> Dictionary<string, obj> 1)
|> GQLProblemDetails.SetErrorKind Execution
|> Include
let message =
match error with
| :? ICoerceGQLError as error -> error.VariableMessage + error.Message
| _ -> error.Message
match error with
| :? IGQLExceptionError as exceptionError ->
GQLProblemDetails.Create(exceptionError.Message, exceptionError.Exception, path, extensions)
| _ ->
GQLProblemDetails.Create (message, path, extensions)
override this.GetHashCode () =
let extensionsHashCode =
match this.Extensions with
| Skip -> 0
| Include extensions ->
extensions
|> Seq.fold (fun hash kvp ->
hash ^^^ kvp.Key.GetHashCode () ^^^ kvp.Value.GetHashCode ()
) 0
#if NETSTANDARD2_0
this.Message.GetHashCode () ^^^ this.Path.GetHashCode () ^^^ this.Locations.GetHashCode () ^^^ extensionsHashCode
#else
HashCode.Combine (this.Message, this.Path, this.Locations, extensionsHashCode)
#endif
override this.Equals (obj : obj) =
match obj with
| :? GQLProblemDetails as other -> (this :> IEquatable<GQLProblemDetails>).Equals other
| _ -> false
interface IEquatable<GQLProblemDetails> with
member this.Equals (other : GQLProblemDetails) =
let extensionsEqual =
match this.Extensions, other.Extensions with
| Skip, Skip -> true
| Include thisExtensions, Include otherExtensions ->
Enumerable.SequenceEqual(thisExtensions, otherExtensions)
| _ -> false
this.Message = other.Message
&& this.Path = other.Path
&& this.Locations = other.Locations
&& extensionsEqual