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

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

问题描述

这里有一个模型情况 我的数据库中有一些字段,可以说颜色,尺寸,高度...

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

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

示例

//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 .../>组件.

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

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

Example

//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})

Is there a way to access components state during query creation ?

Or some other approach ?

Any ideas appreciated

解决方案

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天全站免登陆