React Apollo 从状态动态创建查询 [英] React Apollo dynamically create query from state

查看:21
本文介绍了React Apollo 从状态动态创建查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个模型情况我的数据库中有一些字段可以说颜色、大小、高度...

Heres a model situation I have some fields in my DB lets say color,size,height ...

我可以获取这些字段并将其显示给可以选择这些字段的用户,然后将它们设置为组件状态

I can fetch and display these fields to user who can choose these fields and they are afterwards set to components state

我想要实现的是从存储在状态中的这些字段动态创建 GQL 查询(不是查询变量)

What i want to achieve is to dynamically create GQL query (not query variables) from these fields stored in state

示例

//import react gql ....
class MyComponent extends Component {

constructor(props){
   super(props)
   this.state = {
     fields : []
   }
}

render(){
...
}

componentWillMount(){
    fetch(...)
      .then(fields => this.setState({fields}))
   }

}

export default graphql( --->state => CREATE_QUERY_DYNAMICALLY_FROM_FIELDS(state.fields)<----,{..some options})

有没有办法在查询创建过程中访问组件状态?

或者其他一些方法?

感谢任何想法

推荐答案

由于graphql不支持动态查询,尝试使用apollo客户端并注入查询有条件地甚至你可以使用声明性的 <Query .../> 组件.

Since graphql doesn't support dynamic queries, try to use the apollo client and inject the query conditionally or even you could use the declarative <Query .../> Component.

这篇关于React Apollo 从状态动态创建查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆