React-apollo: How to transfer variable to query at loading component state

Created on 7 Dec 2016  路  3Comments  路  Source: apollographql/react-apollo

I have a simple react component that must load data from server when user ask it. Problem is that i don't know how to transfer dynamic variable speakerUrl and access it in before component load state. Sure i can access it from this.props.params, but component is not loaded and i can't access it when i make a graphql query. Query - QuerySpeaker is working fine when i manualy set url variable.

Router

<Route path="/speaker/:speakerUrl" component={SpeakerPage} />

Component - SpeakerPage

import React from 'react';
import { graphql } from 'react-apollo';

import { QuerySpeaker } from '../redux/graphql/querys';

class SpeakerPage extends React.Component {
    render( ) {
        console.log( this.props );
        return (
            <div className="ui container">
                <div className="ui grid">
                    <div className="row">
                        Hello
                    </div>
                </div>
            </div>
        )
    }
}

export default graphql(QuerySpeaker, {
     options: ({ url }) => ({ variables: { url } }) <- here is my problem, how can i set this url variable?
})( SpeakerPage );

Most helpful comment

@ARMGAMES - can you not do:

export default graphql(QuerySpeaker, {
     options: ({ params: { speakerUrl } }) => ({ variables: { url: speakerUrl } })
})( SpeakerPage );

All 3 comments

@ARMGAMES - can you not do:

export default graphql(QuerySpeaker, {
     options: ({ params: { speakerUrl } }) => ({ variables: { url: speakerUrl } })
})( SpeakerPage );

@tmeasday Thanks

Or you can do this:

export default graphql(QuerySpeaker, {
     options: props => ({ variables: { url: props.match.params.speakerUrl } })
})( SpeakerPage );
Was this page helpful?
0 / 5 - 0 ratings