【发布时间】:2017-11-08 01:28:30
【问题描述】:
在这种情况下,类型“X”是Application,类型“Y”是类型“Node” - 我可以看到 为什么会发生这种情况,但我对 Relay 的理解还不够了解如何修复它。 Relay 生成的查询是
query {
node(id: $some_id) {
...F0
}
}
fragment F0 on Application {
...
}
我有一个看起来像
的架构query {
application {
/* kind of a generic endpoint for fetching lists, etc */
invites(token: $token) {
name
}
}
viewer { /* the current user */ }
}
我正在尝试从会话外部获取特定邀请(viewer 是 null)。
我试过了
const application = Relay.QL`query { application }`
...
<Route ... queries={{ application }}/>
...
Relay.createContainer(Component, {
initialValues: { token: null },
fragments: {
application: () => {
fragment on Application {
invites(token: $token) {
...
}
}
}
}
})
这给了我错误
片段“F0”不能在这里传播,因为“节点”类型的对象永远不能是“应用程序”类型 - 或类似的东西。
我有点困惑,因为如果我要编写一个原始查询并直接通过 GraphQL 运行它
query {
application {
invites(token: "asdasdasd") {
edges {
node {
name
}
}
}
}
}
它给了我想要的东西......
在后端,我的图表定义如下
export const Application = new GraphQLObjectType({
name: 'Application',
fields: () => ({
id: {
type: GraphQLString,
resolve: () => 'APPLICATION_ID'
},
invites: {
type: InviteConnectionType,
args: connectionArgs,
resolve: (application, args) => {
...
}
}
})
})
export default new GraphQLSchema({
query: new GraphQLObjectType({
name: 'query',
fields: {
node: nodeField,
application: {
type: Application,
resolve: (root, args, ctx) => {
return Promise.resolve({})
}
}
}
})
我一直在研究诸如this 和一些issues on the Relay github 之类的问题,但我不清楚应该如何实现nodeInterface。
编辑:当前nodeInterface 代码的short-long 是
export const {
nodeInterface,
nodeField
} = nodeDefinitions(
(globalId) => {
const { type, id } = fromGlobalId(globalId)
return db[type].findById(id)
},
(obj) => {
const name = obj.$modelOptions.name.singular
return types[name]
}
)
应用程序不是数据库模型,但是,它只是一个用于获取数据的通用接口。我试过检查是否type === 'Application',并返回null(虽然我明白为什么这不起作用),返回Application(GraphQLObject),但这不起作用......不太确定去哪里从那里去。
【问题讨论】: