Skip to content

Commit 9325d0a

Browse files
committed
Fixed variable error messages
1 parent 83d15f6 commit 9325d0a

File tree

8 files changed

+40
-42
lines changed

8 files changed

+40
-42
lines changed

samples/star-wars-api/WebSocketJsonConverters.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ type GraphQLQueryConverter<'a>(executor : Executor<'a>, replacements: Map<string
8484
match vdef.DefaultValue, vdef.TypeDef with
8585
| Some _, _ -> ()
8686
| _, Nullable _ -> ()
87-
| None, _ -> failwithf "Variable %s has no default value and is missing!" vdef.Name
87+
| None, _ -> failwithf "A variable '$%s' has no default value and is missing!" vdef.Name
8888
acc)
8989
(ImmutableDictionary.CreateBuilder<string, JsonElement>())
9090
{ ExecutionPlan = executionPlan; Variables = variables.ToImmutable() }

src/FSharp.Data.GraphQL.Server/ErrorMessages.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
module FSharp.Data.GraphQL.ErrorMessagess
44

5-
let variableNotFound variableName = $"Variable '%s{variableName}' not provided"
5+
let variableNotFound variableName = $"A variable '$%s{variableName}' was not provided"

src/FSharp.Data.GraphQL.Server/Execution.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ let internal coerceVariables (variables: VarDef list) (vars: ImmutableDictionary
703703
match varDef.TypeDef with
704704
| Nullable _ -> Ok <| KeyValuePair(varDef.Name, null)
705705
| Named typeDef -> Error [ {
706-
Message = $"Variable '$%s{varDef.Name}' of type '%s{typeDef.Name}!' is not nullable but neither value was provided, nor a default value was specified."
706+
Message = $"A variable '$%s{varDef.Name}' of type '%s{typeDef.Name}!' is not nullable but neither value was provided, nor a default value was specified."
707707
ErrorKind = InputCoercion
708708
InputSource = Variable varDef
709709
Path = []

src/FSharp.Data.GraphQL.Server/Values.fs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ open System.Linq
1212
open System.Text.Json
1313
open FsToolkit.ErrorHandling
1414

15-
open FSharp.Data.GraphQL
1615
open FSharp.Data.GraphQL.Ast
17-
open FSharp.Data.GraphQL.Errors
1816
open FSharp.Data.GraphQL.Types
1917
open FSharp.Data.GraphQL.Types.Patterns
2018
open FSharp.Data.GraphQL.Validation
@@ -201,7 +199,7 @@ let rec internal compileByType (inputObjectPath: FieldPath) (inputSource : Input
201199
return found
202200
else
203201
Debugger.Break()
204-
return! Error [{ new IGQLError with member _.Message = $"Variable '{variableName}' is not an object" }]
202+
return! Error [{ new IGQLError with member _.Message = $"A variable '${variableName}' is not an object" }]
205203
| false, _ -> return null
206204
}
207205
| _ -> Ok null
@@ -256,7 +254,7 @@ let rec internal compileByType (inputObjectPath: FieldPath) (inputSource : Input
256254
| VariableName variableName ->
257255
match variables.TryGetValue variableName with
258256
| true, var -> Ok var
259-
| false, _ -> Error [ { new IGQLError with member _.Message = $"Variable '{variableName}' not found" } ]
257+
| false, _ -> Error [ { new IGQLError with member _.Message = $"A variable '${variableName}' not found" } ]
260258
| _ -> result {
261259
let! coerced = coerceEnumInput value
262260

@@ -283,11 +281,11 @@ let rec internal coerceVariableValue
283281

284282
let createVariableCoercionError message =
285283
Error [ {
286-
InputSource = Variable varDef
287-
Message = message
288-
ErrorKind = InputCoercion
289-
Path = inputObjectPath
290-
FieldErrorDetails = objectFieldErrorDetails
284+
CoercionError.InputSource = Variable varDef
285+
CoercionError.Message = message
286+
CoercionError.ErrorKind = InputCoercion
287+
CoercionError.Path = inputObjectPath
288+
CoercionError.FieldErrorDetails = objectFieldErrorDetails
291289
} :> IGQLError ]
292290

293291
let createNullError typeDef =
@@ -369,12 +367,12 @@ let rec internal coerceVariableValue
369367
match input with
370368
| _ when input.ValueKind = JsonValueKind.Null && isNullable -> Ok null
371369
| _ when input.ValueKind = JsonValueKind.Null ->
372-
createVariableCoercionError $"Variable '$%s{varDef.Name}' expected value of type '%s{enumdef.Name}!', but no value was found."
370+
createVariableCoercionError $"A variable '$%s{varDef.Name}' expected value of type '%s{enumdef.Name}!', but no value was found."
373371
| _ when input.ValueKind = JsonValueKind.String ->
374372
let value = input.GetString()
375373
match enumdef.Options |> Array.tryFind (fun o -> o.Name.Equals(value, StringComparison.InvariantCultureIgnoreCase)) with
376374
| Some option -> Ok option.Value
377-
| None -> createVariableCoercionError $"Value '%s{value}' is not defined in Enum '%s{enumdef.Name}'."
375+
| None -> createVariableCoercionError $"A value '%s{value}' is not defined in Enum '%s{enumdef.Name}'."
378376
| _ ->
379377
createVariableCoercionError $"Enum values must be strings but got '%O{input.ValueKind}'."
380378
| _ ->
@@ -415,7 +413,7 @@ and private coerceVariableInputObject inputObjectPath (originalObjDef, objDef) (
415413
| valueKind ->
416414
Error [ {
417415
InputSource = Variable varDef
418-
Message = $"Variable '$%s{varDef.Name}' expected to be '%O{JsonValueKind.Object}' but got '%O{valueKind}'."
416+
Message = $"A variable '$%s{varDef.Name}' expected to be '%O{JsonValueKind.Object}' but got '%O{valueKind}'."
419417
ErrorKind = InputCoercion
420418
Path = inputObjectPath
421419
FieldErrorDetails = ValueSome { ObjectDef = originalObjDef; FieldDef = ValueNone }

src/FSharp.Data.GraphQL.Shared/SchemaDefinitions.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ module SchemaDefinitions =
368368
| VariableName variableName ->
369369
match variables.TryGetValue variableName with
370370
| true, value -> Ok value
371-
| false, _ -> Error [{ new IGQLError with member _.Message = $"Variable '%s{variableName}' not found" }]
371+
| false, _ -> Error [{ new IGQLError with member _.Message = $"A variable '$%s{variableName}' not found" }]
372372
| v -> other v
373373

374374
/// GraphQL type of int

src/FSharp.Data.GraphQL.Shared/Validation.fs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -909,8 +909,8 @@ module Ast =
909909
match def.Name with
910910
| _ when count < 2 -> Success
911911
| Some operationName ->
912-
AstError.AsResult $"Variable '$%s{var.VariableName}' in operation '%s{operationName}' is declared %i{count} times. Variables must be unique in their operations."
913-
| None -> AstError.AsResult $"Variable '$%s{var.VariableName}' is declared %i{count} times in the operation. Variables must be unique in their operations.")
912+
AstError.AsResult $"A variable '$%s{var.VariableName}' in operation '%s{operationName}' is declared %i{count} times. Variables must be unique in their operations."
913+
| None -> AstError.AsResult $"A variable '$%s{var.VariableName}' is declared %i{count} times in the operation. Variables must be unique in their operations.")
914914
| _ -> Success)
915915

916916
let internal validateVariablesAsInputTypes (ctx : ValidationContext) =
@@ -921,9 +921,9 @@ module Ast =
921921
|> ValidationResult.collect (fun var ->
922922
match def.Name, ctx.Schema.TryGetInputType(var.Type) with
923923
| Some operationName, None ->
924-
AstError.AsResult($"Variable '$%s{var.VariableName}' in operation '%s{operationName}' has a type that is not an input type defined by the schema (%s{var.Type.ToString ()}).")
924+
AstError.AsResult($"A variable '$%s{var.VariableName}' in operation '%s{operationName}' has a type that is not an input type defined by the schema (%s{var.Type.ToString ()}).")
925925
| None, None ->
926-
AstError.AsResult($"Variable '$%s{var.VariableName}' has a type is not an input type defined by the schema (%s{var.Type.ToString ()}).")
926+
AstError.AsResult($"A variable '$%s{var.VariableName}' has a type is not an input type defined by the schema (%s{var.Type.ToString ()}).")
927927
| _ -> Success)
928928
| _ -> Success)
929929

@@ -934,7 +934,7 @@ module Ast =
934934
| VariableName varName ->
935935
if variableDefinitions |> Set.contains varName
936936
then Success
937-
else AstError.AsResult($"Variable '%s{varName}' is referenced in an argument '%s{arg.Name}' of directive '%s{directive.Name}' of field with alias or name '%O{path.Head}', but that variable is not defined in the operation.", path)
937+
else AstError.AsResult($"A variable '%s{varName}' is referenced in an argument '%s{arg.Name}' of directive '%s{directive.Name}' of field with alias or name '%O{path.Head}', but that variable is not defined in the operation.", path)
938938
| _ -> Success)
939939

940940
let rec private checkVariablesDefinedInSelection (fragmentDefinitions : FragmentDefinition list) (variableDefinitions : Set<string>) (path : FieldPath) =
@@ -948,7 +948,7 @@ module Ast =
948948
| VariableName varName ->
949949
if variableDefinitions |> Set.contains varName
950950
then Success
951-
else AstError.AsResult($"Variable '$%s{varName}' is referenced in argument '%s{arg.Name}' of field with alias or name '%s{field.AliasOrName}', but that variable is not defined in the operation.")
951+
else AstError.AsResult($"A variable '$%s{varName}' is referenced in argument '%s{arg.Name}' of field with alias or name '%s{field.AliasOrName}', but that variable is not defined in the operation.")
952952
| _ -> Success)
953953
variablesValid
954954
@@ (field.SelectionSet |> ValidationResult.collect (checkVariablesDefinedInSelection fragmentDefinitions variableDefinitions path))
@@ -1013,8 +1013,8 @@ module Ast =
10131013
let isUsed = def.SelectionSet |> List.exists (variableIsUsedInSelection varDef.VariableName fragmentDefinitions [])
10141014
match def.Name, isUsed with
10151015
| _, true -> Success
1016-
| Some operationName, _ -> AstError.AsResult $"Variable '$%s{varDef.VariableName}' is not used in operation '%s{operationName}'. Every variable must be used."
1017-
| None, _ -> AstError.AsResult $"Variable '$%s{varDef.VariableName}' is not used in operation. Every variable must be used.")
1016+
| Some operationName, _ -> AstError.AsResult $"A variable '$%s{varDef.VariableName}' is not used in operation '%s{operationName}'. Every variable must be used."
1017+
| None, _ -> AstError.AsResult $"A variable '$%s{varDef.VariableName}' is not used in operation. Every variable must be used.")
10181018
| _ -> Success)
10191019

10201020
let rec private areTypesCompatible (variableTypeRef : IntrospectionTypeRef) (locationTypeRef : IntrospectionTypeRef) =
@@ -1041,7 +1041,7 @@ module Ast =
10411041
| VariableName varName ->
10421042
match varNamesAndTypeRefs.TryFind(varName) with
10431043
| Some (varDef, variableTypeRef) ->
1044-
let err = AstError.AsResult($"Variable '$%s{varName}' can not be used in its reference. The type of the variable definition is not compatible with the type of its reference.", path)
1044+
let err = AstError.AsResult($"A variable '$%s{varName}' can not be used in its reference. The type of the variable definition is not compatible with the type of its reference.", path)
10451045
match inputs |> Array.tryFind (fun x -> x.Name = arg.Name) with
10461046
| Some input ->
10471047
let locationTypeRef = input.Type

tests/FSharp.Data.GraphQL.Tests/AstValidationTests.fs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,7 +1165,7 @@ let ``Validation should grant that variables are unique in their operations`` ()
11651165
|> equals (
11661166
ValidationError [
11671167
GQLProblemDetails.CreateValidation
1168-
"Variable '$atOtherHomes' in operation 'houseTrainedQuery' is declared 2 times. Variables must be unique in their operations."
1168+
"A variable '$atOtherHomes' in operation 'houseTrainedQuery' is declared 2 times. Variables must be unique in their operations."
11691169
]
11701170
)
11711171
let query2 =
@@ -1208,13 +1208,13 @@ query takesCatOrDog($catOrDog: CatOrDog) {
12081208
let expectedFailureResult =
12091209
ValidationError [
12101210
GQLProblemDetails.CreateValidation
1211-
"Variable '$cat' in operation 'takesCat' has a type that is not an input type defined by the schema (Cat)."
1211+
"A variable '$cat' in operation 'takesCat' has a type that is not an input type defined by the schema (Cat)."
12121212
GQLProblemDetails.CreateValidation
1213-
"Variable '$dog' in operation 'takesDogBang' has a type that is not an input type defined by the schema (Dog!)."
1213+
"A variable '$dog' in operation 'takesDogBang' has a type that is not an input type defined by the schema (Dog!)."
12141214
GQLProblemDetails.CreateValidation
1215-
"Variable '$pets' in operation 'takesListOfPet' has a type that is not an input type defined by the schema ([Pet])."
1215+
"A variable '$pets' in operation 'takesListOfPet' has a type that is not an input type defined by the schema ([Pet])."
12161216
GQLProblemDetails.CreateValidation
1217-
"Variable '$catOrDog' in operation 'takesCatOrDog' has a type that is not an input type defined by the schema (CatOrDog)."
1217+
"A variable '$catOrDog' in operation 'takesCatOrDog' has a type that is not an input type defined by the schema (CatOrDog)."
12181218
]
12191219
let shouldFail =
12201220
getContext query1
@@ -1256,7 +1256,7 @@ let ``Validation should grant that all referenced variables are defined variable
12561256
|> equals (
12571257
ValidationError [
12581258
GQLProblemDetails.CreateValidation
1259-
"Variable '$atOtherHomes' is referenced in argument 'atOtherHomes' of field with alias or name 'isHousetrained', but that variable is not defined in the operation."
1259+
"A variable '$atOtherHomes' is referenced in argument 'atOtherHomes' of field with alias or name 'isHousetrained', but that variable is not defined in the operation."
12601260
]
12611261
)
12621262
let query2 =
@@ -1320,10 +1320,10 @@ fragment isHouseTrainedCyclic on Dog {
13201320
}"""
13211321
let expectedFailureResult =
13221322
ValidationError [
1323-
GQLProblemDetails.CreateValidation "Variable '$atOtherHomes' is not used in operation 'variableUnused'. Every variable must be used."
1324-
GQLProblemDetails.CreateValidation "Variable '$atOtherHomes' is not used in operation 'variableNotUsedWithinFragment'. Every variable must be used."
1325-
GQLProblemDetails.CreateValidation "Variable '$extra' is not used in operation 'queryWithExtraVar'. Every variable must be used."
1326-
GQLProblemDetails.CreateValidation "Variable '$extra' is not used in operation 'unusedCyclic'. Every variable must be used."
1323+
GQLProblemDetails.CreateValidation "A variable '$atOtherHomes' is not used in operation 'variableUnused'. Every variable must be used."
1324+
GQLProblemDetails.CreateValidation "A variable '$atOtherHomes' is not used in operation 'variableNotUsedWithinFragment'. Every variable must be used."
1325+
GQLProblemDetails.CreateValidation "A variable '$extra' is not used in operation 'queryWithExtraVar'. Every variable must be used."
1326+
GQLProblemDetails.CreateValidation "A variable '$extra' is not used in operation 'unusedCyclic'. Every variable must be used."
13271327
]
13281328

13291329
[ query1; query2; query3; query4 ]
@@ -1393,13 +1393,13 @@ query listToNonNullList($booleanList: [Boolean]) {
13931393
let expectedFailureResult =
13941394
ValidationError [
13951395
(GQLProblemDetails.CreateValidationFor [ "intCannotGoIntoBoolean"; "arguments"; "booleanArgField" ])
1396-
"Variable '$intArg' can not be used in its reference. The type of the variable definition is not compatible with the type of its reference."
1396+
"A variable '$intArg' can not be used in its reference. The type of the variable definition is not compatible with the type of its reference."
13971397
(GQLProblemDetails.CreateValidationFor [ "booleanListCannotGoIntoBoolean"; "arguments"; "booleanArgField" ])
1398-
"Variable '$booleanListArg' can not be used in its reference. The type of the variable definition is not compatible with the type of its reference."
1398+
"A variable '$booleanListArg' can not be used in its reference. The type of the variable definition is not compatible with the type of its reference."
13991399
(GQLProblemDetails.CreateValidationFor [ "booleanArgQuery"; "arguments"; "nonNullBooleanArgField" ])
1400-
"Variable '$booleanArg' can not be used in its reference. The type of the variable definition is not compatible with the type of its reference."
1400+
"A variable '$booleanArg' can not be used in its reference. The type of the variable definition is not compatible with the type of its reference."
14011401
(GQLProblemDetails.CreateValidationFor [ "listToNonNullList"; "arguments"; "nonNullBooleanListField" ])
1402-
"Variable '$booleanList' can not be used in its reference. The type of the variable definition is not compatible with the type of its reference."
1402+
"A variable '$booleanList' can not be used in its reference. The type of the variable definition is not compatible with the type of its reference."
14031403
]
14041404
let shouldFail =
14051405
getContext query1

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ let ``Execute handles variables and errors on incorrect type`` () =
174174
let params' = paramsWithValueInput testInputObject
175175
let result = sync <| Executor(schema).AsyncExecute (ast, variables = params')
176176
ensureRequestError result <| fun [ error ] ->
177-
let message = $"Variable '$input' expected to be '%O{JsonValueKind.Object}' but got '%O{JsonValueKind.String}'."
177+
let message = $"A variable '$input' expected to be '%O{JsonValueKind.Object}' but got '%O{JsonValueKind.String}'."
178178
error |> ensureInputCoercionError (Variable "input") message "TestInputObject"
179179

180180
[<Fact>]
@@ -204,7 +204,7 @@ let ``Execute handles list inputs and nullability and does not allow invalid typ
204204
let params' = paramsWithValueInput testInputList
205205
let result = sync <| Executor(schema).AsyncExecute (ast, variables = params')
206206
ensureRequestError result <| fun [ error ] ->
207-
let message = $"Variable '$input' expected to be '%O{JsonValueKind.Object}' but got '%O{JsonValueKind.Array}'."
207+
let message = $"A variable '$input' expected to be '%O{JsonValueKind.Object}' but got '%O{JsonValueKind.Array}'."
208208
error |> ensureInputCoercionError (Variable "input") message "TestInputObject!"
209209
match error.Extensions with
210210
| Include extensions -> equals (box "TestInputObject!") extensions[CustomErrorFields.ObjectType]
@@ -222,6 +222,6 @@ let ``Execute handles list inputs and nullability and does not allow unknown typ
222222
let params' = paramsWithValueInput testInputValue
223223
let result = sync <| Executor(schema).AsyncExecute (ast, variables = params')
224224
let expectedError =
225-
let message = "Variable '$input' in operation 'q' has a type that is not an input type defined by the schema (UnknownType!)."
225+
let message = "A variable '$input' in operation 'q' has a type that is not an input type defined by the schema (UnknownType!)."
226226
GQLProblemDetails.CreateWithKind (message, Validation)
227227
ensureRequestError result <| fun [ error ] -> error |> equals expectedError

0 commit comments

Comments
 (0)