这是一个模型情况
我的数据库中有一些字段,比如颜色、大小、高度......
我可以获取这些字段并将其显示给可以选择这些字段的用户,然后将它们设置为组件状态
我想要实现的是动态创建GQL查询(不是查询变量)来自存储在状态中的这些字段
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})
有没有办法在查询创建期间访问组件状态?
或者其他一些方法?
任何想法表示赞赏
class MyComponent extends Component {
constructor(props){
super(props)
this.state = {
fields : []
}
}
render(){
...
}
componentWillMount(){
const query = gql`
query myDynamicQuery {
viewer {
endpoint {
${this.state.fields.join('\n')}
}
}
}
`
this.props.client.query({ query }).then((res) => ...)
}
}
export default withApollo(MyComponent)
希望这有效:)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)