`Updater` не работает с Relay Modern, потому что`ConnectionHandler.getConnection()`возвращает`undefined`
Я использую Relay Modern для своего приложения и пытаюсь обновить кеш после мутации, используя updater
а такжеoptimisticUpdater
но это не совсем работает.
В основном у меня есть Link
введите с votes
соединение - вот соответствующая часть моей схемы:
type Link implements Node {
createdAt: DateTime!
description: String!
id: ID!
postedBy(filter: UserFilter): User
url: String!
votes(filter: VoteFilter, orderBy: VoteOrderBy, skip: Int, after: String, before: String, first: Int, last: Int): VoteConnection
}
type Vote implements Node {
createdAt: DateTime!
id: ID!
link(filter: LinkFilter): Link!
updatedAt: DateTime!
user(filter: UserFilter): User!
}
# A connection to a list of items.
type VoteConnection {
# Information to aid in pagination.
pageInfo: PageInfo
# A list of edges.
edges: [VoteEdge]
# Count of filtered result set without considering pagination arguments
count: Int!
}
# An edge in a connection.
type VoteEdge {
# The item at the end of the edge.
node: Vote
# A cursor for use in pagination.
cursor: String
}
Вот код для моего Link
компонент запроса votes
во фрагменте:
class Link extends Component {
render() {
const userId = localStorage.getItem(GC_USER_ID)
return (
<div>
{userId && <div onClick={() => this._voteForLink()}>▲</div>}
<div>{this.props.link.description} ({this.props.link.url})</div>
<div>{this.props.link.votes.edges.length} votes | by {this.props.link.postedBy ? this.props.link.postedBy.name : 'Unknown'} {this.props.link.createdAt}</div>
</div>
)
}
_voteForLink = () => {
const userId = localStorage.getItem(GC_USER_ID)
const linkId = this.props.link.id
CreateVoteMutation(userId, linkId, this.props.viewer.id)
}
}
export default createFragmentContainer(Link, graphql`
fragment Link_viewer on Viewer {
id
}
fragment Link_link on Link {
id
description
url
createdAt
postedBy {
id
name
}
votes(last: 1000, orderBy: createdAt_DESC) @connection(key: "Link_votes", filters: []) {
edges {
node {
id
user {
id
}
}
}
}
}
`)
Наконец, это CreateVoteMutation
с updater
:
const mutation = graphql`
mutation CreateVoteMutation($input: CreateVoteInput!) {
createVote(input: $input) {
vote {
id
link {
id
}
user {
id
}
}
}
}
`
export default (userId, linkId, viewerId) => {
const variables = {
input: {
userId,
linkId,
clientMutationId: ""
},
}
commitMutation(
environment,
{
mutation,
variables,
updater: (proxyStore) => {
const createVoteField = proxyStore.getRootField('createVote')
const newVote = createVoteField.getLinkedRecord('vote')
const viewerProxy = proxyStore.get(viewerId)
const connection = ConnectionHandler.getConnection(viewerProxy, 'Link_votes')
// `connection` is undefined, so the `newVote` doesn't get inserted
if (connection) {
ConnectionHandler.insertEdgeAfter(connection, newVote)
}
},
onError: err => console.error(err),
},
)
}
Призыв к ConnectionHandler.getConnection(viewerProxy, 'Link_votes')
только возвращается undefined
, Итак newVote
на самом деле не вставляется.
Кто-нибудь видит, что я делаю не так?
1 ответ
Проблема:
Когда вы получаете соединение:
const connection = ConnectionHandler.getConnection(viewerProxy, 'Link_votes')
Вы пытаетесь получить соединение Link_votes на ViewerProxy. Однако то, что вы хотите сделать, это получить соединение по ссылке.
Решение:
Сначала вам нужно будет получить идентификатор ссылки, по которой вы добавляете голос.
const linkId = newVote.getLinkedRecord('link').getValue('id');
Затем вы хотите получить Link Proxy, чтобы вы могли затем установить правильное соединение.
const linkProxy = proxyStore.get(LinkId)
Теперь, когда у вас есть Link Proxy, представляющий ссылку, для которой вы хотели установить соединение, теперь вы можете получить это соединение.
const connection = ConnectionHandler.getConnection(linkProxy, 'Link_votes')
Сладкий, так что теперь у вас есть связь. Что решает проблему, с которой вы столкнулись.
Однако есть еще одна проблема: способ добавления голосования неправильный: сначала нужно создать из него Edge, а затем добавить его.
Сначала нам нужно создать ребро
const voteEdge = createEdge(proxyStore, connection, newVote, 'VoteEdge');
Теперь, когда у нас есть voiceEdge, мы можем добавить его к соединению.
ConnectionHandler.insertEdgeAfter(connection, voteEdge).
Теперь все должно работать. Однако вам, вероятно, не следует использовать функцию обновления для такого рода действий. Вы должны использовать конфигурацию RANGE_ADD https://facebook.github.io/relay/docs/mutations.html и изменить реакцию своего сервера на эту мутацию.