我正在努力探索Relay路线,反应路由器参数以及构建查询和容器一般非常困难!
我想在用户点击FeatureList中的特定功能时编辑功能。它传递了一个名为" id"这是Route.js中的功能的ID
<Route path='/' component={AppComponent} queries={ViewerQuery}>
<IndexRoute component={FeaturesContainer} queries={ViewerQuery} />
<Route path='/feature' component={FeatureComponent} queries={ViewerQuery} />
<Route path="/feature/edit/:id" component={FeatureEditComponent} queries={FeatureQuery}/>
<Redirect from='*' to='/' />
</Route>
在我的FeatureQuery文件中,我有以下查询:
export default {
viewer: (Component) => Relay.QL`
query {
viewer {
${Component.getFragment('viewer')}
}
}
`
};
此时我完全卡住了。如何扩展它以包含&#34; id&#34;并使用&#34; id&#34;来查询功能? 相关的继电器容器片段的形状是什么样的?我只看到一个深层次的例子。
我试过了,但我知道它不对:
export default {
feature: (Component) => Relay.QL`
query {
viewer {
features(id:$id) {
${Component.getFragment('feature')}
}
}
}
`
};
这是获取功能列表的当前中继容器,如何通过id返回1功能? :
export default Relay.createContainer(CreativeEditComponent, {
fragments: {
viewer: () => Relay.QL`
fragment on User {
id,
features(first: 20) {
edges {
node {
id
name
description
}
}
}
}`
}
});
我在GraphiQL中测试了一个查询,它按预期工作:
query {
viewer {
features(id:"1") {
edges {
node {
id
name
description
}
}
}
}
}
结果:
{
"data": {
"viewer": {
"features": {
"edges": [
{
"node": {
"id": "Q3JlYXRpdmU6MQ==",
"name": "React",
"description": "A JavaScript library for building user interfaces."
}
}
]
}
}
}
}
schema.js:
const userType = new GraphQLObjectType({
name: 'User',
description: 'A person who uses our app',
fields: () => ({
id: globalIdField('User'),
features: {
type: featureConnection,
description: 'Features that I have',
//args: connectionArgs,
args: {
id: {
type: GraphQLString,
},
after: {
type: GraphQLString,
},
first: {
type: GraphQLInt,
},
before: {
type: GraphQLString,
},
last: {
type: GraphQLInt,
},
},
resolve: (_, args) => {
return resolveGetFeatures(args)
},
},
}),
interfaces: [nodeInterface]
});
const featureType = new GraphQLObjectType({
name: 'Feature',
description: 'Feature integrated in our starter kit',
fields: () => ({
id: globalIdField('Feature'),
name: {
type: GraphQLString,
description: 'Name of the feature'
},
description: {
type: GraphQLString,
description: 'Description of the feature'
}
}),
interfaces: [nodeInterface]
});
答案 0 :(得分:0)
您需要将路由器变量传递给您的片段。
const ViewerQuery = {
viewer: (Component, vars) => Relay.QL`
query {
viewer {
${Component.getFragment('viewer', vars)} # <-- this
}
}
`
}
我从此处复制了此代码详细信息:https://github.com/relay-tools/react-router-relay/issues/92#issuecomment-235641397
然后您可以在组件中使用id变量,但是您需要id的初始值:
export default Relay.createContainer(CreativeEditComponent, {
initialVariables: {
id: ''
},
fragments: {
viewer: () => Relay.QL`
fragment on User {
id,
feature(id:$id) {
id
name
description
}
}`
}
});
在schema.js中为您的用户类型定义一个字段,该字段仅提供一个功能:
const userType = new GraphQLObjectType({
name: 'User',
description: 'A person who uses our app',
fields: () => ({
id: globalIdField('User'),
feature: {
type: featureType,
description: 'feature',
args: {
id: {
type: GraphQLString,
description: 'The id of the feature'
}
},
resolve: (_, args) => resolveGetFeature (args),
},
...