0
我正在做多個項目的突變。但是updater中的連接處理程序返回undefined。我處理的shopItems類型,下面是相關的架構突變後繼電器現代連接處理程序未定義
type Mutation {
shopItem(input: itemsInput): addedItems
}
type addedItems {
addedItems: [itemEdge]
}
type itemEdge {
cursor: Int
node: itemNode
}
type itemNode implements Node {
id: ID!,
name: String,
price: Int
}
type Root {
viewer: viewer
}
type viewer {
id: ID!
shopItems(ShopId: ID, SubCategoryId:ID, first:Int, last: Int): Item
}
type Item {
pageInfo: PageInfo
edges: [itemEdge]
}
這是shopItems查詢片段,
module.exports = createFragmentContainer(
Item,
graphql`
fragment item_viewer on viewer {
// the global parent viewer id
id,
shopItems(first:$first,last:$last,ShopId:$ShopId,SubCategoryId:$SubCategoryId) @connection(key: "item_shopItems",filters:["first","last"]){
// didn't want the pageInfo here yet but relay compiler enforces this because of @connection. It's basically returning null.
pageInfo {
hasNextPage
endCursor
}
edges {
cursor // returns null as well
node {
id
name
price
}
}
}
}
`
)
添加shopItems突變返回addedItems的陣列,
mutation addShopItemMutation($input: itemsInput) {
shopItem(input: $input) {
addedItems {
node {
id
name
price
}
}
}
}
commitMutation(
environment,
{
...
updater: (store) => {
const payload = store.getRootField('shopItem');
//I've seen everyone using getLinkedRecord, but in my case the mutation return type is an array and it gives an error so I'm using getLinkedRecords. I think this is where the problem lies.
const newItem = payload.getLinkedRecords('addedItems');
this.sharedUpdate(store, this.props.viewerId, newItem)
}
})
sharedUpdate(store, viewerId, newItem) {
//viewerProxy here is not undefined
const viewerProxy = store.get(viewerId);
//conn is undefined
const conn = ConnectionHandler.getConnection(
viewerProxy,
'item_shopItems',
);
if(conn) {
ConnectionHandler.insertEdgeAfter(conn, newItem);
}
}
由於某種原因連接返回未定義。另外,當我console.log viewerproxy,我看到連接鍵「item_shopItems」,但新的邊緣不會出現在那裏。以防萬一,我在服務器端使用Node Js - Express。
另一個問題是,addedItem不是單數,而是一個數組。