go-graphql-client icon indicating copy to clipboard operation
go-graphql-client copied to clipboard

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

Open tobiasbeck opened this issue 8 months ago • 1 comments

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
}

tobiasbeck avatar May 31 '24 13:05 tobiasbeck