variables
Closed this issue · 5 comments
for the following query:
var number = "1232321";
export default {
auth: () => Relay.QL`query queryType ($number: String) { calc (number: $number ) }`,
}
and the following route:
<Route
path="calc" component={Calc}
queries={myQueries} }
/>
how should I pass my $number variable to the query?
currently, I am get the following error:
Uncaught Error: Invariant Violation: callsFromGraphQL(): Expected a declared value for variable, `$number`.
thanks
Drop it into your fragment and use initialVariables
.
thank you taion but we have the following in relay starwars example:
export default class extends Relay.Route {
static queries = {
factions: () => Relay.QL`query { factions(names: $factionNames) }`,
};
static routeName = 'StarWarsAppHomeRoute';
}
ReactDOM.render(
<Relay.RootContainer
Component={StarWarsApp}
route={new StarWarsAppHomeRoute({
factionNames: ['empire', 'rebels']
})}
/>,
document.getElementById('root')
);
you mean there is no way of doing the same thing using react-router-relay? I need a way to use variables in my root query. thanks.
Queries don't have variables. They have parameters. By design, all parameters come from the route. If you need anything else, you can always write that into the GraphQL for the query itself, e.g. calc(number: 3)
.
using variables for QL tag does not work: (type of number argument in schema is string):
var number = "1232321";
export default {
auth: () => Relay.QL`query queryType ($number: String) { calc (number: "${number}" ) }`,
}
this is converted by relay-babel-plugin to
return (function (sub_0) {
var GraphQL = _reactRelay2['default'].QL.__GraphQL;
return new GraphQL.Query('calc', new GraphQL.CallValue('...sub_0,'), null, null, {
rootArg: 'number'
}, 'QueryType');
})(number);
how can I create the query using template value substitution?
You can't. That's not valid syntax. This is also not the right forum for general "how do I use Relay" questions.