hasura / go-graphql-client

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

Add filter tag for Where #106

Closed subhasishgoswami closed 1 year ago

subhasishgoswami commented 1 year ago

Currently filter using where is not supported and the only way to do is by using native raw queries and client.Exec. Added a graph_filter tag which can allow for users to specify filter pairs and use the already available value mapping to create filtered queries.

For Example-

{
routerTxReceipts(
      first: $first,
      where:
      {
      user: $user
    ) {
      txId
      user
      initiationHash
    }
}

The given query can be converted to- {routerTxReceipts(first: $first, where:{user: $user}){txId,user,initiationHash,srcChainId,dstChainId,initiationTimestamp}}

hgiasac commented 1 year ago

Hi @subhasishgoswami,

Thank you for contributing. However, we should make the library to be general and don't use custom tag for opinional use cases. Anyway, what's your issue here? The library still can support the above use case.

var query struct {
  RouterTxReceipts []struct {
      TxId                      `graphql:"txId"`
      User                     `graphql:"user"`
      InitiationHash    `graphql:"initiationHash"`
  } `graphql:"routerTxReceipts(first: $first, where:{user: $user})"`
} 

variables := map[string]interface{} {
  "first": 1,
  "user":  ...
}
subhasishgoswami commented 1 year ago

Hi @hgiasac I was trying to add custom filter tags but I think what you gave works fine-

var query struct {
  RouterTxReceipts []struct {
      TxId                      `graphql:"txId"`
      User                     `graphql:"user"`
      InitiationHash    `graphql:"initiationHash"`
  } `graphql:"routerTxReceipts(first: $first, where:{user: $user})"`
}

I will close this pr for now and use it but if find anything that doesn't work for the use case I will try discussing in the comments.