Skip to content

Decoders - #351

Draft
njlr wants to merge 5 commits into
fsprojects:devfrom
njlr:experiment-decoders
Draft

Decoders#351
njlr wants to merge 5 commits into
fsprojects:devfrom
njlr:experiment-decoders

Conversation

@njlr

@njlrnjlr commented Oct 6, 2021

Copy link
Copy Markdown
Contributor

This PR introduces "decoders" to input types in the schema.

A decoder is like a coerceInput function, but it returns a structured Error rather than an option:

typeDecodeResult<'t>= Result<'t, DecodeError>typeDecoder<'t>= Value -> DecodeResult<'t>

A helper library of decoders is provided for common types like string, int, etc. There is also a utility for automatic decoder generation using reflection.

All input types (not just scalars) now have a decoder attached to them.

The user can provide a decoder (recommended) or they can allow a default one to be generated automatically. This allows the user to interpret the GraphQL AST with the context of the type being decoded.

There are currently some scrappy comments etc. If we like the idea I can clean it up before merging.

@xperiandrixperiandri left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks interesting.
But I have not noticed a practical usage sample in tests.

Comment threadsrc/FSharp.Data.GraphQL.Server/Relay/Node.fs
Comment threadsrc/FSharp.Data.GraphQL.Server/Values.fs
Comment threadsrc/FSharp.Data.GraphQL.Server/Values.fs
Comment threadsrc/FSharp.Data.GraphQL.Server/Values.fs
Comment threadsrc/FSharp.Data.GraphQL.Shared/Decoding.fs Outdated
Comment threadsrc/FSharp.Data.GraphQL.Shared/Decoding.fs Outdated
Comment threadsrc/FSharp.Data.GraphQL.Shared/TypeSystem.fs Outdated
njlrand others added 3 commits October 6, 2021 20:31
Co-authored-by: Andrii Chebukin <xperiandri@live.ru>
Co-authored-by: Andrii Chebukin <xperiandri@live.ru>
Co-authored-by: Andrii Chebukin <xperiandri@live.ru>
@njlr

njlr commented Oct 6, 2021

Copy link
Copy Markdown
ContributorAuthor

Looks interesting. But I have not noticed a practical usage sample in tests.

Good point - I can update one of the samples perhaps

 * Updates star-wars to new API
* Updates star-wars to new API
* Updates star-wars to new API
* Updates star-wars to new API
* Updates star-wars to new API
* Updates star-wars to new API
* Updates star-wars to new API

match error.Reason with
| DecodeErrorReason.TypeMismatch (expected, actual) ->
sprintf "expected value of type %s but got %s" expected actual

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
sprintf "expected value of type %s but got %s" expected actual
sprintf "expected value of type '%s' but got '%s'" expected actual

match varDef.TypeDef with
| Nullable _ -> Ok NullValue
|_ ->
let message = sprintf "Variable '$%s': expected value of type %s, but no value was found" vname (varDef.TypeDef.ToString())

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
letmessage= sprintf "Variable '$%s': expected value of type %s, but no value was found" vname (varDef.TypeDef.ToString())
letmessage= sprintf "Variable '$%s': expected value of type '%s', but no value was found" vname (varDef.TypeDef.ToString())

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@njlr@xperiandri