ReactDOM.render(
<Router
history={browserHistory}
render={applyRouterMiddleware(useRelay)}
environment={Relay.Store}
>
<Route
path="/user/:userId"
component={User}
queries={StoreQueries}
/>
</Router>,
document.getElementById('root')
);
来自GraphQL的My StoreType如下所示,用户字段接受表示非空整数的id参数:
let StoreType = new GraphQLObjectType({
name: 'Store',
fields: () => ({
user: {
type: UserType,
args: {
id: {
type: new GraphQLNonNull(GraphQLInt)
}
},
resolve: (root, args) => db.user.findById(args.id)
},
})
})
user/:userId
路线的我的中继容器:
import React from 'react';
import Relay from 'react-relay'
class User extends React.Component {
render () {
return <div>
User goes here!
</div>;
}
}
User = Relay.createContainer(User, {
initialVariables: {
userId: null,
},
fragments: {
store: () => Relay.QL`
fragment on Store {
user(id: $userId) {
name
}
}
`
}
});
export default User;
当我在浏览器中访问/ user / 1时,react-router-relay将:userId
视为路由中的字符串而不是整数,并且GraphQL返回JSON编码的错误消息"Argument \"id\" has invalid value \"1\".\nExpected type \"Int\", found \"1\"."
有没有办法将URL中传递的userId参数转换为整数?或者是否有自定义GraphQL标量类型,它支持格式良好的数字字符串以及整数?任何意见或方向都表示赞赏。
答案 0 :(得分:1)
我最近遇到了同样的问题并最终得到了这个解决方案:
<Route
path="/user/:userId"
component={User}
queries={StoreQueries}
prepareParams={({userId}) => ({userId: parseInt(userId, 10)})}/>
/>