我无法使用 Hasura 发送带有 where 条件的 GraphQL 查询。

huangapple go评论89阅读模式
英文:

I can't send graphql query with where condition on hasura

问题

我必须使用golang和Hasura发送graphql查询。但是我无法实现,因为我使用的查询不接受where条件。原因是我想将where作为一种类型发送。例如;

query MyQuery($where: popular_streamers_bool_exp!) {
	popular_streamers(where: $where) {
		first_name
		last_name
	}
}
type conditions struct {
	FollowersCount struct {
		Gte int `json:"_gte"`
	} `json:"followers_count"`
	Gender struct {
		Eq string `json:"_eq,omitempty"`
	} `json:"gender,omitempty"`
}
condition := conditions{}
condition.FollowersCount.Gte = 1
condition.Gender.Eq = "Male"

data, _ := json.Marshal(condition)

如上所示,我有一个查询和where条件。但是当我发送查询时,我收到以下错误;

graphql: expected an object for type 'popular_streamers_bool_exp', but found a string

如何解决这个错误?谢谢你的帮助。

英文:

I have to send graphql query with using golang and Hasura. But I can't achieve that because the query I used doesn't accept where condition. The reason is that I want to send the where as a type. For example;

query MyQuery($where: popular_streamers_bool_exp!) {
	popular_streamers(where: $where) {
		first_name
		last_name
	}
}
type conditions struct {
	FollowersCount struct {
		Gte int `json:"_gte"`
	} `json:"followers_count"`
	Gender struct {
		Eq string `json:"_eq,omitempty"`
	} `json:"gender,omitempty"`
}
condition := conditions{}
condition.FollowersCount.Gte = 1
condition.Gender.Eq = "Male"

data, _ := json.Marshal(condition)

As you see above I have a query and where condition. But when I send the query I get an error like this;

graphql: expected an object for type 'popular_streamers_bool_exp', but found a string

How can solve this error? Thanks for your help.

答案1

得分: 1

在查询中,where应该是一个对象。你没有正确编写where子句。

你想查询"popular_streamers",所以你正在访问数据库以根据条件获取一些数据,并且你使用where来指定这个条件。

query MyQuery($where: popular_streamers_bool_exp!) {
    // specificProperty是你想要写条件的表中的列之一
    // 获取所有specificProperty等于变量$where的popular_streamers
    popular_streamers(where: {specificProperty: {_eq: $where}}) {
        first_name
        last_name
    }
}
英文:

where in the query should be an object. You did not write where clause properly

You want to query "popular_streamers. so you are visiting the database to get some data based on a condition and you specify this condition with where`.

query MyQuery($where: popular_streamers_bool_exp!) {
    // specificProperty one of the columns in the table where you want to write condition
    // get me all popular_streamers where specificProperty is equal to variable $where
    popular_streamers(where: {specificProperty:{_eq:$where}) {
        first_name
        last_name
    }
}

答案2

得分: 0

在代码块中,查询使用了变量进行搜索。

查询

query($query: String!) {
  popular_streamers(where: {
    popular_streamers_bool_exp: $query
  }) {
    first_name
    last_name
  }
}

GraphQL 变量

{
    "query" : "Male"    
}

我无法使用 Hasura 发送带有 where 条件的 GraphQL 查询。

英文:

In the code block query does search with variables.

QUERY

query($query: String!) {
  popular_streamers(where: {
    popular_streamers_bool_exp: $query
  }) {
    first_name
    last_name
  }
}

GRAPHQL VARIABLES

{
    "query" : "Male"    
}

我无法使用 Hasura 发送带有 where 条件的 GraphQL 查询。

huangapple
  • 本文由 发表于 2022年10月11日 15:50:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/74024695.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定