0

Suppose that I have the following root query in a relay with react-router-relay project:

export default {
  calc: () => Relay.QL`query queryType ($number: String) { auth (number: $number) }`,
}

Initial value of $number comes from server like a hidden input and I want to use this number in my first query to server. How can I pass $number to my query using the current react-router-relay API? This is neither a queryParam or stateParam.

Chris
  • 57,622
  • 19
  • 111
  • 137
msin
  • 23
  • 1
  • 5
  • It appears that this is currently unsupported by react-router-relay - there is an [open pull request](https://github.com/relay-tools/react-router-relay/pull/61) to provide support via a `prepareVariables` function. – Joe Savona Oct 07 '15 at 19:23

1 Answers1

2

You should be able to do something like:

import {createHistory} from 'history';
import React from 'react';
import ReactDOM from 'react-dom';
import {Route, Router} from 'react-router-relay';
import MyComponent from './components/MyComponent';
import MyComponentQueries from './routes/MyComponentQueries';

function addAuthParam(params, route) {
  return {
    ...params,
    number: 'SECRET',
  }; 
}

ReactDOM.render(
  <Router
    history={createHistory()}
    createElement={ReactRouterRelay.createElement}>
    <Route
      component={MyComponent}
      path="/thing/:id"
      prepareParams={addAuthParam}
      queries={MyComponentQueries}
    />
  </Router>,
  document.getElementById('relay-root')
);

prepareParams was added in react-router-relay in v0.6.2. The above syntax should work for that or v0.7.0 (the current release) as well.

steveluscher
  • 4,144
  • 24
  • 42
Greg Hurrell
  • 5,177
  • 23
  • 27