Add support for binding with a custom marshal/unmarshal function (#104)

## Summary:
This is useful if you want to bind to a type you don't control (or use
for other things) but need different serialization than its default.
This is a feature gqlgen has and we've found it very useful.  For
example, in webapp we want to bind `DateTime` to `time.Time`, but its
default serialization is not compatible with Python, so currently we
have to bind to a wrapper type and cast all over the place, which is
exactly the sort of boilerplate genqlient is supposed to avoid.

For unmarshaling, the implementation basically just follows the existing
support for abstract types; instead of calling our own generated
helper, we now call your specified function.  This required some
refactoring to abstract the handling of custom unmarshalers generally
from abstract types specifically, and to wire in not only the
unmarshaler-name but also the `generator` (in order to compute the right
import alias).

For marshaling, I had to implement all that stuff over again; it's
mostly parallel to unmarshaling (and I made a few minor changes to
unmarshaling to make the two more parallel).  Luckily, after #103 I at
least only had to do it once, rather than implementing the same
functionality for arguments and for input-type fields.  It was still
quite a bit of code; I didn't try to be quite as completionist about the
tests as with unmarshal but still had to add a few.

Issue: https://github.com/Khan/genqlient/issues/38

## Test plan:
make check


Author: benjaminjkraft

Reviewers: StevenACoffman, dnerdy, benjaminjkraft, aberkan, jvoll, mahtabsabet, MiguelCastillo

Required Reviewers: 

Approved By: StevenACoffman, dnerdy

Checks:  Test (1.17),  Test (1.16),  Test (1.15),  Test (1.14),  Lint,  Test (1.17),  Test (1.16),  Test (1.15),  Test (1.14),  Lint

Pull Request URL: https://github.com/Khan/genqlient/pull/104
This commit is contained in:
Ben Kraft
2021-09-24 11:16:01 -07:00
committed by GitHub
parent 5995653583
commit 8de55d352e
42 changed files with 1900 additions and 455 deletions
+3 -1
View File
@@ -23,10 +23,12 @@ When releasing a new version:
### Breaking changes:
- The [`graphql.Client`](https://pkg.go.dev/github.com/Khan/genqlient/graphql#Client) interface now accepts `variables interface{}` (containing a JSON-marshalable value) rather than `variables map[string]interface{}`. Clients implementing the interface themselves will need to change the signature; clients who simply call `graphql.NewClient` are unaffected.
- genqlient's handling of the `omitempty` option has changed to match that of `encoding/json`, from which it had inadvertently differed. In particular, this means struct-typed arguments with `# @genqlient(omitempty: true)` will no longer be omitted if they are the zero value. (Struct-pointers are still omitted if nil, so adding `pointer: true` will typically work fine.)
- genqlient's handling of the `omitempty` option has changed to match that of `encoding/json`, from which it had inadvertently differed. In particular, this means struct-typed arguments with `# @genqlient(omitempty: true)` will no longer be omitted if they are the zero value. (Struct-pointers are still omitted if nil, so adding `pointer: true` will typically work fine. It's also now possible to use a custom marshaler to explicitly map zero to null.)
### New features:
- The new `bindings.marshaler` and `bindings.unmarshaler` options in `genqlient.yaml` allow binding to a type without using its standard JSON serialization; see the [documentation](genqlient.yaml) for details.
### Bug fixes:
- The `omitempty` option now works correctly for struct- and map-typed variables, matching `encoding/json`, which is to say it never omits structs, and omits empty maps. (#43)
+37
View File
@@ -104,6 +104,42 @@ bindings:
# - a nonstandard way of spelling those, (interface {/* hi */},
# map[ string ]T)
type: time.Time
# Optionally, the fully-qualified name of the function to use when
# marshaling this type.
#
# This is useful when you want to bind to a standard type, but use
# nonstandard marshaling, for example when making requests to a server
# that's not compatible with Go's default time format. It is only used for
# types passed as arguments, i.e. input types, scalars, and enums.
#
# The function should have a signature similar to json.Marshal, i.e., it
# will be passed one argument which will be a pointer to a value of the
# given type, and must return two values: the JSON as a `[]byte`, and an
# error. For example, you might specify
# unmarshaler: github.com/you/yourpkg.MarshalMyType
# and that function is defined as e.g.:
# func MarshalMyType(v *MyType) ([]byte, error)
#
# Note that the `omitempty` option is ignored for types with custom
# marshalers; the custom marshaler can of course choose to map any value it
# wishes to `"null"` which in GraphQL has the same effect.
#
# The default is to use ordinary JSON-marshaling.
marshaler: github.com/you/yourpkg.MarshalDateTime
# Optionally, the fully-qualified name of the function to use when
# unmarshaling this type.
#
# This is similar to marshaler, above, but for unmarshaling. The specified
# function should have a signature similar to json.Unmarshal, i.e., it will
# be passed two arguments, a []byte of JSON to unmarshal and a pointer to a
# value of the given type, and must return an error. For example, you
# might specify
# unmarshaler: github.com/you/yourpkg.UnmarshalMyType
# and that function is defined as e.g.:
# func UnmarshalMyType(b []byte, v *MyType) error
#
# The default is to use ordinary JSON-unmarshaling.
unmarshaler: github.com/you/yourpkg.UnmarshalDateTime
# To bind an object type:
MyType:
@@ -124,3 +160,4 @@ bindings:
# or something, if you want to say, for example, that you have to request
# certain fields but others are optional.
expect_exact_fields: "{ id name }"
# unmarshaler and marshaler are also valid here, see above for details.
+2 -1
View File
@@ -44,7 +44,8 @@ directive genqlient(
# which will pass {"arg": null} to GraphQL if arg is "", and the actual
# value otherwise.
#
# Only applicable to arguments of nullable types.
# Only applicable to arguments of nullable types. Ignored for types with
# custom marshalers (see their documentation in genqlient.yaml for details).
omitempty: Boolean
# If set, this argument or field will use a pointer type in Go. Response