hasura / go-graphql-client

Package graphql provides a GraphQL client implementation.
MIT License
405 stars 94 forks source link

Using date/time fields #51

Closed matiasinsaurralde closed 2 years ago

matiasinsaurralde commented 2 years ago

Hi, I'm facing an issue when trying to pass time.Time values as variables during a query:

type Record struct {
    ID  string `graphql:"id"`
    CreatedAt time.Time `graphql:"created_at"`
}
type Query struct {
    Record []Record `graphql:"record(where:{created_at: {_gte: $gte, _lte: $lte}})"`
}

err := client.Query(context.Background(), &query, map[string]interface{}{
    "gte": time.Now(),
    "lte": time.Now(),
})

The following error shows up:

% go run main.go 
panic: Message: variable "gte" is declared as Time!, but used where timestamptz is expected, Locations: []

Same error occurs if I pass a string version of the date as gte. Wandering what's the appropriate way here.

hgiasac commented 2 years ago

@matiasinsaurralde you can set the alias, or define a custom type with GetGraphQLType method https://github.com/hasura/go-graphql-client#specify-graphql-type-name

type timestamptz time.Time

type Record struct {
    ID  string `graphql:"id"`
    CreatedAt timestamptz `graphql:"created_at"`
}
type Query struct {
    Record []Record `graphql:"record(where:{created_at: {_gte: $gte, _lte: $lte}})"`
}

err := client.Query(context.Background(), &query, map[string]interface{}{
    "gte": timestamptz(time.Now()),
    "lte": timestamptz(time.Now()),
})
matiasinsaurralde commented 2 years ago

@hgiasac Thanks. Closing this one.

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.