2017-03-06 15 views
3

createMessage 돌연변이를 앱에 전송 한 후 ApolloStoreupdateQueries으로 업데이트하려고합니다.GraphQL 돌연변이가 Apollo 클라이언트와 작동하지 않는 후 updateQueries가 발생했습니다.

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이 실행될 수는 있지만 발생하지 않는 것입니다 (로깅 문은 절대로 인쇄되지 않습니다).

const findConversations = gql` 
    query allConversations($customerId: ID!) { 
     allConversations(filter: { 
      customer: { 
      id: $customerId 
      } 
     }){ 
      id 
      updatedAt 
      slackChannelName 
      agent { 
       id 
       slackUserName 
      } 
      messages(last: 1) { 
       id 
       text 
       createdAt 
      } 
     } 
    } 
` 

사람이 무엇을 발견 하는가 :

enter image description here 또한

는, 이것이이 내 JS 코드에 정의 된 방법은 다음과 같습니다 참고로

,이 같은 ApolloStoreallConversation 쿼리 모습입니다 내가 잘못하고있어?

답변

1

같은 구성 요소에서 쿼리와 돌연변이를 사용하면 돌연변이와 쿼리를 작성할 수 있습니다. 솔루션 1에서와 마찬가지입니다.

구성 요소에 변이가 필요하지 않은 경우 명명 된 쿼리를 추가 할 수 있습니다 (버전 0.11.1/이후로 관련 검색어는 적어도 한 번 이상 호출해야합니다. 아폴로 매장은 알지 못합니다) 또는 쿼리 자체를 updateQueries에 추가 할 수 있습니다. 사용

1) 구성 요소 만 갖고 싶어하기 때문에, 파일에 정의 된 graphql 쿼리와 돌연변이 및 쿼리

import { compose } from 'react-apollo'; 
 

 
... 
 

 
import findConversationsQuery from './.../findConversationsQuery'; 
 

 
... 
 

 
const ChatWithAllMessages = compose(
 
    graphql(allMessages, {name: 'allMessagesQuery'}), 
 
    findConversationsQuery, 
 
    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 ... 
 
       } 
 
       } 
 
      }) 
 
      } 
 
     } 
 
     } 
 
    })(Chat)

은 한 번

import { graphql } from 'react-apollo'; 
 
import gql from 'graphql-tag'; 
 

 
const findConversations = gql` 
 
    query allConversations($customerId: ID!) { 
 
     allConversations(filter: { 
 
      customer: { 
 
      id: $customerId 
 
      } 
 
     }){ 
 
      id 
 
      updatedAt 
 
      slackChannelName 
 
      agent { 
 
       id 
 
       slackUserName 
 
      } 
 
      messages(last: 1) { 
 
       id 
 
       text 
 
       createdAt 
 
      } 
 
     } 
 
    } 
 
` 
 

 
const findConversationsQuery = graphql(findConversations, { 
 
    name: "findConversationsQuery" 
 
}); 
 

 
export default findConversationsQuery
을 인스턴스화

+0

> updateQuery 기능은 변형에 대한 뷰가 쿼리에 대한 참조를 보유 할 때만 호출됩니다. 해당 진술에 대한 참조가 있습니까? – marktani

+0

아폴로의 문서에 이렇게 쓰여진 것은 아닙니다. [updateQueries definition] (http://dev.apollodata.com/react/cache-updates.html#updateQueries)을 통해 읽으면 "CommentsPage 구성 요소가 호출 할 수있는 함수 prop를 통해이 변형을 노출합니다."라는 두 개의 문이 있습니다. 및 "주석 페이지 자체는 다음 쿼리로 렌더링됩니다." 그래서 쿼리는 구성 요소의 소품에 있어야한다고 생각했습니다. –

+0

[이 댓글] (https://github.com/apollographql/apollo-client/issues/1129#issuecomment-270677147)을 참고하면 동작이 버그 인 것 같습니다. 비록 내가 현재의 합의에 관해 명확하지 않더라도. – marktani