0

我不知道爲什麼會發生這種情況。 我正在收聽訂閱postAdded,每次我使用突變創建帖子時都會發布。Apollo客戶訂閱必須提供模式

import React, { Component } from 'react' 
import { graphql, gql } from 'react-apollo' 

class App extends Component { 
    componentDidMount() { 
    this.props.subscribeToNewPosts() 
    } 

    render() { 
    return (
     <div> 
     <h1>Hello world</h1> 
     {console.log(this.props)} 
     </div> 
    ) 
    } 
} 

const Query = gql` 
    query Posts { 
    posts { 
     id 
     title 
     content 
     author { 
     firstName 
     lastName 
     } 
    } 
    } 
` 

const Subscription = gql` 
    subscription postAdded { 
    postAdded { 
     id 
     title 
     content 
     author { 
     firstName 
     lastName 
     } 
    } 
    } 
` 

App = graphql(Query, { 
    name: 'posts', 
    props: props => { 
    return { 
     subscribeToNewPosts:() => { 
     return props.posts.subscribeToMore({ 
      document: Subscription, 
      updateQuery: (prev, { subscriptionData }) => { 
      if(!subscriptionData.data) { 
       return prev 
      } 

      const newPost = subscriptionData.data.postAdded 

      console.log(newPost) 

      return { 
       ...prev, 
       posts: [...prev.posts, newPost] 
      } 
      } 
     }) 
     }, 
     posts: props.posts 
    } 
    } 
})(App) 

export default App 

錯誤:

enter image description here

+0

我認爲錯誤來自服務器端的實現。這是怎麼樣的? –

回答

0

首先,這是最有可能是服務器的實現問題。該錯誤很可能是由graphql-tools引發的,這是您的graphql終點。

在官方博客上跟隨Full-stack + GraphQL教程的第二部分時,發生了與我完全相同的事情。

儘管情況未必如此,但發生在我身上的是該教程導出模式對象的方式爲:export { schema }相當於export { schema: schema },而不是默認的常規導出。 export default schema

在我的graphql服務器端點導入import schema from './src/schema'這是錯誤的,因爲模式導出爲子對象,而不是默認導出。正確導入我的案件應該是import { schema } from './src/schema'

TL; DR檢查您的服務器代碼,檢查您的導出和導入語句。該錯誤消息實際上是誤導性的,但它最有可能與模塊導入/導出有關。

如果我們使用webpack編譯而不是使用本教程中的babel-node,它可能會被避免或給出更明確的信息。

相關問題