Skip to content

Commit 1e567ad

Browse files
Copilotxperiandri
andauthored
Add OperationError unit and integration tests
Agent-Logs-Url: https://github.com/fsprojects/FSharp.Data.GraphQL/sessions/d64ab4ee-eb67-4b7c-a114-d6ad4b640254 Co-authored-by: xperiandri <2365592+xperiandri@users.noreply.github.com>
1 parent 898818a commit 1e567ad

3 files changed

Lines changed: 81 additions & 1 deletion

File tree

tests/FSharp.Data.GraphQL.IntegrationTests.Server/Schema.fs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ namespace FSharp.Data.GraphQL.Samples.StarWarsApi
22

33
open System
44
open System.Text
5+
open System.Collections.Generic
56
open Microsoft.AspNetCore.Http
67
open Microsoft.Extensions.DependencyInjection
78

@@ -135,7 +136,19 @@ module Schema =
135136
typedef = StructNullable OutputType,
136137
description = "Enters an input type and get it back.",
137138
args = [ Define.Input("input", Nullable InputType, description = "The input to be echoed as an output.") ],
138-
resolve = fun ctx _ -> ctx.TryArg("input")) ])
139+
resolve = fun ctx _ -> ctx.TryArg("input"))
140+
Define.Field(
141+
name = "alwaysError",
142+
typedef = Nullable StringType,
143+
description = "Always produces an execution error for integration tests.",
144+
args = [],
145+
resolve =
146+
fun _ _ ->
147+
let extensions = Dictionary<string, obj> 2
148+
extensions["code"] <- box "OPERATION_ERROR_TEST"
149+
extensions["severity"] <- box 7
150+
raise (GQLMessageException("Always fails for tests", extensions))
151+
) ])
139152

140153
let InputFileObject = Define.InputObject<InputFile>(
141154
name = "InputFile",

tests/FSharp.Data.GraphQL.IntegrationTests/FSharp.Data.GraphQL.IntegrationTests.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
</None>
2323
<Content Include="xunit.runner.json" CopyToOutputDirectory="PreserveNewest" />
2424
<Compile Include="Helpers.fs" />
25+
<Compile Include="OperationErrorTests.fs" />
2526
<Compile Include="ReservedScalarNameProviderTests.fs" />
2627
<Compile Include="LocalProviderTests.fs" />
2728
<Compile Include="LocalProviderWithOptionalParametersOnlyTests.fs" />
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
module FSharp.Data.GraphQL.IntegrationTests.OperationErrorTests
2+
3+
open System.Net.Http
4+
open Xunit
5+
open Helpers
6+
open FSharp.Data.GraphQL
7+
open FSharp.Data.GraphQL.Client
8+
9+
let [<Literal>] ServerUrl = "http://localhost:8085"
10+
11+
type Provider = GraphQLProvider<ServerUrl, uploadInputTypeName = "File", explicitOptionalParameters = false>
12+
13+
module ErrorOperation =
14+
let operation =
15+
Provider.Operation<"""query ErrorQuery {
16+
alwaysError
17+
}""">()
18+
19+
type Operation = Provider.Operations.ErrorQuery
20+
21+
[<Fact; Trait("OperationError", "Unit")>]
22+
let ``Should parse operation error fields from raw response`` () =
23+
let result =
24+
OperationResultBase(
25+
rawResponse = new HttpResponseMessage(),
26+
responseJson =
27+
JsonValue.Parse
28+
"""{
29+
"errors": [{
30+
"message": "unit-test error",
31+
"path": ["alwaysError", 0],
32+
"locations": [{ "line": 2, "column": 13 }],
33+
"extensions": { "code": "UNIT_TEST", "retryable": false, "severity": 7 }
34+
}]
35+
}""",
36+
operationFields = [||],
37+
operationTypeName = "Query"
38+
)
39+
40+
result.Errors.Length |> equals 1
41+
42+
let error : FSharp.Data.GraphQL.OperationError = result.Errors.[0]
43+
error.Message |> equals "unit-test error"
44+
error.Path |> equals [| box "alwaysError"; box 0 |]
45+
error.Locations |> equals [| { Line = 2; Column = 13 } |]
46+
error.Extensions.["code"] |> equals (box "UNIT_TEST")
47+
error.Extensions.["retryable"] |> equals (box false)
48+
error.Extensions.["severity"] |> equals (box 7)
49+
50+
[<Fact; Trait("OperationError", "Integration")>]
51+
let ``Should map server error extensions and locations into operation result`` () =
52+
let result = ErrorOperation.operation.Run()
53+
54+
result.Errors.Length |> equals 1
55+
56+
let error : FSharp.Data.GraphQL.OperationError = result.Errors.[0]
57+
error.Message |> equals "Always fails for tests"
58+
error.Path |> equals [| box "alwaysError" |]
59+
60+
error.Locations |> equals [||]
61+
62+
error.Extensions.ContainsKey "code" |> equals true
63+
error.Extensions.["code"] |> equals (box "OPERATION_ERROR_TEST")
64+
error.Extensions.ContainsKey "severity" |> equals true
65+
error.Extensions.["severity"] |> equals (box 7)
66+
error.Extensions.ContainsKey "kind" |> equals true

0 commit comments

Comments
 (0)