在這種情況下,鍵入「X」是Application
並且類型「Y」是類型「節點」 - 我可以看到爲什麼這正在發生,但我對Relay的理解還不足以理解如何解決它。由繼電器產生的查詢是片段不能在這裏傳播,因爲類型「X」的對象永遠不可能是「Y」類型
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和一些的問題,但它不是我清楚我應該如何實現nodeInterface
。
編輯:短長目前nodeInterface
代碼是
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]
}
)
應用程序是不是一個DB模式,但是,只是一個通用的接口,通過獲取數據。我試着檢查type === 'Application'
,並返回null(雖然我明白了爲什麼不起作用),返回Application
(GraphQLObject),但這不起作用......並不確定從那裏去哪裏。
是的我覺得我需要實現'nodeInterface',但我真的不知道如何...檢查我的編輯。 –
對不起,誤解了。在'nodeInterface'中,您基本上需要將全局定義的ID映射到數據對象,以便Relay能夠獲取對象。我認爲這個https://github.com/entria/graphql-dataloader-boilerplate/blob/8d661425538e7881f8eb12437929aba77d4cfbba/src/interface/NodeInterface.js完整的例子會很有幫助。 – Zero
但是對於那些不是/技術上的/有邊緣和關係的對象,只是其他事物的接口 - 比如在這種情況下的應用程序。我已經嘗試過(或多或少)我認爲實現應用程序的nodeinterface的樣子。 –