React Apollo dynamically create query from state
Asked Answered
O

3

6

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

Orthogonal answered 15/6, 2018 at 9:18 Comment(4)
I don't think there is any built-in function for dynamically creating queries, but since the query is just a string you could format that string by yourself. Eg: `query{ topLevelField { ${ fields.join(',') } }}` Also you should declare the list of fields outside of your react component, if you are going to declare the graphql query outside of your component.Adder
Since graphql doesn't support dynamic queries, try to use the apollo client and inject the query conditionally or even the declarative <Query .../> Component.Whitening
@LefiTarik great comment i have upgraded to react-apollo 2.1 and with <Query> works like a charm if you write it as an answer i would definitely accept :)Orthogonal
great @Orthogonal :).Whitening
W
2

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.

Whitening answered 20/6, 2018 at 13:57 Comment(1)
actually graphql does support dynamic queries (the query is simply sent as a json payload in the post). I am going to try what @a-moynet suggested workingCoquina
F
5
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)

Hope this is working :)

Finedraw answered 20/6, 2018 at 16:11 Comment(1)
Your suggestion works! In fact, the entire query can be dynamic. I do not know what downstream impact there might be (related to offline or subscriptions).Coquina
W
2

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.

Whitening answered 20/6, 2018 at 13:57 Comment(1)
actually graphql does support dynamic queries (the query is simply sent as a json payload in the post). I am going to try what @a-moynet suggested workingCoquina
O
0

If you have a list of fields, you could try mapping them to your schema with the package I created. See graphql-pick

Orjonikidze answered 23/5, 2024 at 11:17 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.