Zik*_*ker 5 apollo reactjs graphql 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})
Run Code Online (Sandbox Code Playgroud)
有没有办法在查询创建期间访问组件状态?
或者其他一些方法?
任何想法表示赞赏
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)
Run Code Online (Sandbox Code Playgroud)
希望这是有效的:)
由于graphql不支持动态查询,请尝试使用 apollo 客户端并有条件地注入查询,甚至可以使用声明性<Query .../>
组件。
归档时间: |
|
查看次数: |
3660 次 |
最近记录: |