hasura / go-graphql-client

Package graphql provides a GraphQL client implementation.
MIT License
395 stars 91 forks source link

Add option to add custom parsing logic similar to json.Unmarshaler and json.Marshaler #140

Open tobiasbeck opened 3 months ago

tobiasbeck commented 3 months ago

Add option to add custom parsing logic similar to json.Unmarshaler and json.Marshaler.

In my code I'd like to execute custom logic when decoding a struct but still have the struct beeing part of a query, which is currently not possible as far as i understand the code. When adding a UnmarshalJSON method to the struct it is excluded from the query.

A lot of libraries, such as json or bson allow doing so with the interfaces above.

I think an interface such as graphql.Unmarshaler would be really helpful.

My use case is the following:

func (ent *Entity) UnmarshalGraphql(data []byte) error {
    type Alias Entity
    dta := &Alias{}
    err := graphql.UnmarshalGraphQL(data, dta)
    if err != nil {
        return err
    }
    *ent = Entity(*dta)
        // Do some custom logic here
    return nil
}
hgiasac commented 3 months ago

@tobiasbeck For the time being you can use QueryRaw and MutateRaw methods to return raw bytes and decode manually with json.Unmarshal