hasura / go-graphql-client

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

Update the updated_at value using the Mutate method #69

Open NajyFannoun opened 1 year ago

NajyFannoun commented 1 year ago
          I'm trying to update the updated_at value using the Mutate method. 

and the solution provided did not work for me, and it throws the following exception:

   `parsing UTCTime failed, expected String, but encountered Object, Locations: []`

The reason behind it is the value of timestamptz(time.Now()) is equivalent to time.Time{} object, and hasura is waiting for String to be parsed. knowing that sending a string will throw the following error:

variable 'time' is declared as 'String!', but used where 'timestamptz' is expected, Locations: []

Building the query inside the buildAndRequest method do the job, but cannot handle having the 'timestamptz' to be sent as String.

If there are no solution, a possible solution might be by editing the queryArguments in ConstructMutation method, to make optional parsing of the arguments manually by the developer(similar to OptionType ..eg.. OperationName implementation by providing the Arguments' names as another Option), rather than only parsing it using only the implemented code.

Originally posted by @NajyFannoun in https://github.com/hasura/go-graphql-client/issues/51#issuecomment-1399336163

hgiasac commented 1 year ago

Hi Najy,

It's very helpful if you can provide the mutation example so we can reproduce the issue.

From my experience with Hasura graphql engine, instead of passing the scalar input directly, we can use the top-level object input.

type user_set_input map[string]interface{}
var mutation struct {
  UpdateUsers struct {
    AffectedRows int `graphql:"affected_rows"`
  } `graphql:"update_user(_set: $_set)"`
}

variables := map[string]interface{}{
  "_set": user_set_input{
      "updated_at": time.Now()
  }
}

The library encodes json directly without caring about internal input types.

You also can try type timestamptz string alias instead of using time.Time

NajyFannoun commented 1 year ago

Thanks for the answer

using type timestamptz string do the trick, and fix the issue.

I was using the provided solution as type timestamptz time.Time which was throwing the exception

  `parsing UTCTime failed, expected String, but encountered Object, Locations: []`