Skip to content

Decoders#351

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

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

Conversation

@njlr
Copy link
Copy Markdown
Contributor

@njlr njlr commented Oct 6, 2021

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:

type DecodeResult<'t> = Result<'t, DecodeError>

type Decoder<'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.

Copy link
Copy Markdown
Collaborator

@xperiandri xperiandri left a comment

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 thread src/FSharp.Data.GraphQL.Server/Relay/Node.fs
Comment thread src/FSharp.Data.GraphQL.Server/Values.fs
Comment thread src/FSharp.Data.GraphQL.Server/Values.fs
Comment thread src/FSharp.Data.GraphQL.Server/Values.fs
Comment thread src/FSharp.Data.GraphQL.Shared/Decoding.fs Outdated
Comment thread src/FSharp.Data.GraphQL.Shared/Decoding.fs Outdated
Comment thread src/FSharp.Data.GraphQL.Shared/TypeSystem.fs Outdated
njlr and 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
Copy link
Copy Markdown
Contributor Author

njlr commented Oct 6, 2021

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
let message = sprintf "Variable '$%s': expected value of type %s, but no value was found" vname (varDef.TypeDef.ToString())
let message = sprintf "Variable '$%s': expected value of type '%s', but no value was found" vname (varDef.TypeDef.ToString())

@xperiandri xperiandri force-pushed the dev branch 4 times, most recently from d90f0e9 to 434b4be Compare October 11, 2024 16:35
@xperiandri xperiandri force-pushed the dev branch 3 times, most recently from f3a1bf8 to 9d4b950 Compare December 1, 2025 00:26
@xperiandri xperiandri force-pushed the dev branch 2 times, most recently from 6dc591e to 85a9d8c Compare December 19, 2025 02:20
Sign up for free to 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