-
Notifications
You must be signed in to change notification settings - Fork 76
* First pass at custom coerce for input objects #350
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -38,31 +38,36 @@ let rec internal compileByType (errMsg: string) (inputDef: InputDef): ExecuteInp | |||||||||||||
| | Scalar scalardef -> | ||||||||||||||
| variableOrElse (scalardef.CoerceInput >> Option.toObj) | ||||||||||||||
| | InputObject objdef -> | ||||||||||||||
| let objtype = objdef.Type | ||||||||||||||
| let ctor = ReflectionHelper.matchConstructor objtype (objdef.Fields |> Array.map (fun x -> x.Name)) | ||||||||||||||
| let mapper = | ||||||||||||||
| ctor.GetParameters() | ||||||||||||||
| |> Array.map(fun param -> | ||||||||||||||
| match objdef.Fields |> Array.tryFind(fun field -> field.Name = param.Name) with | ||||||||||||||
| | Some x -> x | ||||||||||||||
| | None -> | ||||||||||||||
| failwithf "Input object '%s' refers to type '%O', but constructor parameter '%s' doesn't match any of the defined input fields" objdef.Name objtype param.Name) | ||||||||||||||
| fun value variables -> | ||||||||||||||
| match value with | ||||||||||||||
| | ObjectValue props -> | ||||||||||||||
| let args = | ||||||||||||||
| mapper | ||||||||||||||
| |> Array.map (fun field -> | ||||||||||||||
| match Map.tryFind field.Name props with | ||||||||||||||
| | None -> null | ||||||||||||||
| | Some prop -> field.ExecuteInput prop variables) | ||||||||||||||
| let instance = ctor.Invoke(args) | ||||||||||||||
| instance | ||||||||||||||
| | Variable variableName -> | ||||||||||||||
| match Map.tryFind variableName variables with | ||||||||||||||
| | Some found -> found | ||||||||||||||
| | None -> null | ||||||||||||||
| | _ -> null | ||||||||||||||
| match objdef.CoerceInput with | ||||||||||||||
| | Some coerceInput -> | ||||||||||||||
| // Apply the user-provided coerce function, rather than attempt reflection | ||||||||||||||
| variableOrElse (coerceInput >> Option.toObj) | ||||||||||||||
| | None -> | ||||||||||||||
| let objtype = objdef.Type | ||||||||||||||
| let ctor = ReflectionHelper.matchConstructor objtype (objdef.Fields |> Array.map (fun x -> x.Name)) | ||||||||||||||
| let mapper = | ||||||||||||||
| ctor.GetParameters() | ||||||||||||||
| |> Array.map(fun param -> | ||||||||||||||
| match objdef.Fields |> Array.tryFind(fun field -> field.Name = param.Name) with | ||||||||||||||
| | Some x -> x | ||||||||||||||
| | None -> | ||||||||||||||
| failwithf "Input object '%s' refers to type '%O', but constructor parameter '%s' doesn't match any of the defined input fields" objdef.Name objtype param.Name) | ||||||||||||||
| fun value variables -> | ||||||||||||||
| match value with | ||||||||||||||
| | ObjectValue props -> | ||||||||||||||
| let args = | ||||||||||||||
| mapper | ||||||||||||||
| |> Array.map (fun field -> | ||||||||||||||
| match Map.tryFind field.Name props with | ||||||||||||||
| | None -> null | ||||||||||||||
| | Some prop -> field.ExecuteInput prop variables) | ||||||||||||||
| let instance = ctor.Invoke(args) | ||||||||||||||
| instance | ||||||||||||||
| | Variable variableName -> | ||||||||||||||
| match Map.tryFind variableName variables with | ||||||||||||||
| | Some found -> found | ||||||||||||||
| | None -> null | ||||||||||||||
| | _ -> null | ||||||||||||||
| | List (Input innerdef) -> | ||||||||||||||
| let inner = compileByType errMsg innerdef | ||||||||||||||
| let cons, nil = ReflectionHelper.listOfType innerdef.Type | ||||||||||||||
|
|
@@ -153,17 +158,29 @@ let rec private coerceVariableValue isNullable typedef (vardef: VarDef) (input: | |||||||||||||
| | _ -> raise (GraphQLException <| errMsg + "Only Scalars, Nullables, Lists and InputObjects are valid type definitions.") | ||||||||||||||
|
|
||||||||||||||
| and private coerceVariableInputObject (objdef) (vardef: VarDef) (input: obj) errMsg = | ||||||||||||||
| //TODO: this should be eventually coerced to complex object | ||||||||||||||
| match input with | ||||||||||||||
| | :? Map<string, obj> as map -> | ||||||||||||||
| let mapped = | ||||||||||||||
| objdef.Fields | ||||||||||||||
| |> Array.map (fun field -> | ||||||||||||||
| let valueFound = Map.tryFind field.Name map |> Option.toObj | ||||||||||||||
| (field.Name, coerceVariableValue false field.TypeDef vardef valueFound (errMsg + (sprintf "in field '%s': " field.Name)))) | ||||||||||||||
| |> Map.ofArray | ||||||||||||||
| upcast mapped | ||||||||||||||
| | _ -> input | ||||||||||||||
| match objdef.CoerceInput with | ||||||||||||||
| | Some coerceInput -> | ||||||||||||||
| // Apply the user-provided coerce function | ||||||||||||||
| match input with | ||||||||||||||
| | :? Value as v -> | ||||||||||||||
| coerceInput v |> Option.toObj | ||||||||||||||
| | _ -> failwithf "Expected an input Value but got %A" input | ||||||||||||||
| | None -> | ||||||||||||||
| match input with | ||||||||||||||
| | :? Map<string, obj> as map -> | ||||||||||||||
| let mapping = | ||||||||||||||
| objdef.Fields | ||||||||||||||
| |> Array.map (fun field -> | ||||||||||||||
| let valueFound = Map.tryFind field.Name map |> Option.toObj | ||||||||||||||
| let coercedValue = coerceVariableValue false field.TypeDef vardef valueFound (errMsg + (sprintf "in field '%s': " field.Name)) | ||||||||||||||
|
Comment on lines
+173
to
+175
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Maybe it does not worth to materialize it to an array |
||||||||||||||
|
|
||||||||||||||
| (field.Name, coercedValue)) | ||||||||||||||
| |> Map.ofSeq | ||||||||||||||
|
|
||||||||||||||
| // TODO: Should we apply reflection tricks here? | ||||||||||||||
|
|
||||||||||||||
| upcast mapping | ||||||||||||||
| | _ -> input | ||||||||||||||
|
|
||||||||||||||
| let internal coerceVariable (vardef: VarDef) (inputs) = | ||||||||||||||
| let vname = vardef.Name | ||||||||||||||
|
|
||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jberzy can we switch this to
Result<,>and pass an error to the response?