Skip to content
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

Support gqlgen marshalling #147

Open
johnmaguire opened this issue Oct 27, 2021 · 5 comments
Open

Support gqlgen marshalling #147

johnmaguire opened this issue Oct 27, 2021 · 5 comments
Labels
enhancement New feature or request help wanted Issues that anyone could pick up and implement if useful to them

Comments

@johnmaguire
Copy link
Contributor

Is your feature request related to a problem? Please describe.
Custom scalars require marshal and unmarshal functions to tell genqlient how they work.

I already have marshal and unmarshal functions written for gqlgen. I don't want to have to write them a second time or add a layer of indirection to support both gqlgen and genqlient.

Describe the solution you'd like
I would love for genqlient to respect the interfaces prescribed by gqlgen - specifically looking for MarshalGQL / UnmarshalGQL on custom types and supporting the UnmarshalX(v interface{}) (X, error) / MarshalX(v X) graphql.Marshaler APIs.

Describe alternatives you've considered
Currently, I need to write functions that satisfy both the gqlgen and genqlient interfaces, both of which call into generic functions. This is particularly annoying when dealing with scalar types built-in to gqlgen.

Additional context

@johnmaguire johnmaguire added the enhancement New feature or request label Oct 27, 2021
@benjaminjkraft
Copy link
Collaborator

Oh interesting idea! You can use UnmarshalX/MarshalX functions via genqlient's binding option, you'll just have to configure them manually. Looking for those automatically would be a neat idea, we'd have to add new code to use go/types to look at your source but I suspect that's going to be necessary sooner or later anyway.

@benjaminjkraft benjaminjkraft added the help wanted Issues that anyone could pick up and implement if useful to them label Oct 27, 2021
@zreigz
Copy link

zreigz commented Jul 13, 2022

@benjaminjkraft I have a similar problem. I would like to use this type https://github.com/99designs/gqlgen/blob/master/graphql/upload.go but to be honest I don't know how to convert this for
genqlient.

@benjaminjkraft
Copy link
Collaborator

To be more specific about the configuration, it looks like gqlgen uses a slightly different API for marshaling, but if you only need to unmarshal (i.e. the type is only used as an output), you can add to your config for example:

bindings:
  Time:
    type: github.com/99designs/gqlgen/graphql.Time
    unmarshaler: github.com/99designs/gqlgen/graphql.UnmarshalTime

If you want to marshal, you could define your own wrapper function matching genqlient's API, and use that. We could try supporting the gqlgen-style marshalers, perhaps as a different option gqlgenmarshaler: ..., if it's not too complex to add. (Pull requests welcome!)

But for Upload specifically, that won't help you: uploads are specially implemented in gqlgen and would need to be specially implemented in genqlient because they're not just a type we can unmarshal; they require the entire request to have a different structure. Issue #139 tracks implementing that (again PRs welcome; the issue has some details as to what the spec looks like and what the genqlient API might look like).

@zreigz
Copy link

zreigz commented Jul 13, 2022

@benjaminjkraft thanks a lot I will take a look

@Obsecurus
Copy link

any updates on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Issues that anyone could pick up and implement if useful to them
Projects
None yet
Development

No branches or pull requests

4 participants