Javascript GraphQL变异后的UpdateSeries无法与Apollo客户端一起工作
在我的应用程序中发送Javascript GraphQL变异后的UpdateSeries无法与Apollo客户端一起工作,javascript,graphql,apollo,apollostack,react-apollo,Javascript,Graphql,Apollo,Apollostack,React Apollo,在我的应用程序中发送createMessage变异后,我想使用更新本地ApolloStore 我的设置如下所示: const ChatWithAllMessages = graphql(allMessages, {name: 'allMessagesQuery'})(Chat) export default graphql(createMessage, { props({ownProps, mutate}) { return { createMessageMutatio
createMessage
变异后,我想使用更新本地ApolloStore
我的设置如下所示:
const ChatWithAllMessages = graphql(allMessages, {name: 'allMessagesQuery'})(Chat)
export default graphql(createMessage, {
props({ownProps, mutate}) {
return {
createMessageMutation(text, conversationId) {
return mutate({
variables: { text, conversationId },
updateQueries: {
allConversations: (previousState, {mutationResult}) => {
console.log('Chat - did send mutation for allConversationsQuery: ', previousState, mutationResult)
return ...
}
}
})
}
}
}
})(ChatWithAllMessages)
我在我的代码中调用createMessageMutation
,如下所示:
_onSend = () => {
this.props.createMessageMutation(this.state.message, this.props.conversationId)
}
使用此设置,我希望执行我在updateQueries
的值中指定的函数,但是,这似乎没有发生(日志记录语句从未打印)
作为参考,以下是ApolloStore
中的allConversation
查询的内容:
另外,我的JS代码中定义它的方式如下:
const findConversations = gql`
query allConversations($customerId: ID!) {
allConversations(filter: {
customer: {
id: $customerId
}
}){
id
updatedAt
slackChannelName
agent {
id
slackUserName
}
messages(last: 1) {
id
text
createdAt
}
}
}
`
有人发现我做错了什么吗?如果在同一组件中使用查询和变异,则可以组合变异和查询。就像解决方案1一样 如果不需要组件中的变异,可以添加命名查询(自版本0.11.1/起,必须至少调用一次相关查询,否则apollo store不知道该查询),或者可以将查询本身添加到UpdateSeries 1) 使用变异和查询的组件
从'react apollo'导入{compose};
...
从“/…/findConversationsQuery”导入findConversationsQuery;
...
const ChatWithAllMessages=compose(
graphql(allMessages,{name:'allMessagesQuery'}),
findConversationsQuery,
graphql(createMessage{
道具({ownProps,mutate}){
返回{
CreateMessageId(文本,会话ID){
返回变异({
变量:{
文本,
会话ID
},
UpdateSeries:{
allConversations:(先前状态{
突变结果
}) => {
log('Chat-did为allConversationsQuery:'发送了变异,previousState,mutationResult)
返回。。。
}
}
})
}
}
}
})(聊天)
>只有当突变视图包含对查询的引用时,才会调用updateQuery功能。您是否有该语句的引用?它在apollo的文档中不是这样直接编写的。当您通读时,有两条语句“我们通过CommentsPage组件可以调用的函数prop公开了这种变异”和“comments页面本身通过以下查询呈现”"。因此,我认为查询必须是组件的道具。引用它听起来像是一个错误。尽管我不清楚当前的协议。是的,你是对的,因此可能在版本0.11.1中,它可以在不构成查询的情况下工作,而突变不需要保留对查询的引用,它只需要要知道查询的名称(不是JSvariable名称,而是graphql查询字符串中给定的名称,即在“query(var1:string,…){…”中).UpdateSeries仅在之前执行过命名查询时才起作用,否则Apollo客户端不会意识到该查询。为了避免此问题,您可以直接向UpdateSeries提供查询+变量(而不是查询名称)。您能否相应地更新您的答案?