2016-10-03 11 views
0

私は、GitHunt-APIとGitHunt-Reactの例を使って、新しいApollo pubsubコードを学んでいます。私は、クライアント上でsubscribeを呼び出すと、私はコンソールログのエラーになっています:Apolloパブリッシュ&サブスクライブエラー:Can not Query Field?

"Cannot query field "createIM" on type "Subscription".

を私は非常に密接にサンプルコードを追跡しようとしましたが、私は何かが欠けています。

このエラーを修正するにはどうすればよいですか?

CREATE_IM.JSX

class CreateIM extends React.Component { 
[.....] 
    subscribe(fromID, toID, updateCommentsQuery) { 
     const SUBSCRIPTION_QUERY = gql` 
      subscription IMAdded($fromID: String!, $toID: String!, $msgText: String!){ 
       createIM(fromID: $fromID, toID: $toID, msgText: $msgText){ 
       fromID 
       toID 
       msgText 
    } 
} 
    `; 
     this.subscriptionObserver = this.props.client.subscribe({ 
      query: SUBSCRIPTION_QUERY, 
      variables: { fromID: this.fromID, toID: this.toID }, 
     }).subscribe({ 
      next(data) { 
       debugger; 
       const newComment = data.commentAdded; 
       updateCommentsQuery((previousResult) => { 
        // if it's our own mutation, we might get the subscription result 
        // after the mutation result. 
        if (isDuplicateComment(newComment, previousResult.entry.comments)) { 
         return previousResult; 
        } 
        // update returns a new "immutable" list with the new comment 
        // added to the front. 
        return update(
         previousResult, 
         { 
          entry: { 
           comments: { 
            $unshift: [newComment], 
           }, 
          }, 
         } 
        ); 
       }); 
      }, 
      error(err) { debugger; console.error('err', err); }, //<== ERROR ON THIS LINE 
     }); 
    } 

SCHEMA

import Resolvers from '/imports/api/resolvers'; 
import Connectors from '/imports/api/db-connectors'; 
import { makeExecutableSchema } from 'graphql-tools'; 

const typeDefinitions = [` 

type instant_message { 
    id: Int 
    fromID: String 
    toID: String 
    msgText: String 
} 
type Query { 
    instant_message(fromID: String, toID: String, msgText: String): [instant_message] 
} 
type Mutation { 
    createIM(
    fromID: String! 
    toID: String! 
    msgText: String! 
): instant_message 
} 
type Subscription { 
    # Subscription fires on every comment added 
    IMAdded(fromID: String!, toID: String!, msgText: String!): instant_message 
} 

schema { 
    query: Query, 
    mutation: Mutation 
    subscription: Subscription 
} 

`]; 


const executableSchema = makeExecutableSchema({ 
    typeDefs: typeDefinitions, 
    resolvers: Resolvers, 
    connectors: Connectors, 
    logger: console, 
}); 

export default executableSchema; 

SUBSCRIPTIONS(サーバ・コード)

import { print } from 'graphql-tag/printer'; 
import { PubSub, SubscriptionManager } from 'graphql-subscriptions'; 
import schema from '/imports/api/schema'; 

const pubsub = new PubSub(); 
const subscriptionManager = new SubscriptionManager({ 
    schema, 
    pubsub, 
    setupFunctions: { 
     IMAdded: (options, args) => ({ 
      IMAdded: comment => true, //not quite sure yet what validation needs to be here 
     }), 
    }, 
}); 

export { subscriptionManager, pubsub }; 

のリゾルバ

const resolvers = { 
    Query: { 
     instant_message(_, args) { 
      var ret = connectors.IM.findAll({ where: args }).then((res) => res.map((item) => item.dataValues)); 
      return ret; 
     } 
    }, 
    Mutation: { 
     createIM(root, args, context) { 
      return Promise.resolve() 
       .then(() => (
        connectors.IM.create(args) 
       )) 
       .then(([args]) => 
        connectors.IM.findAll({ where: args }).then((res) => res.map((item) => item.dataValues)) 
       ) 
       .then(comment => { 
        // publish subscription notification 
        pubsub.publish('IMAdded', comment); 
        return comment; 
       }); 
     }, 
    }, 
    Subscription: { 
     IMAdded(fromID, toID, msgText) { 
      // the subscription payload is the comment. 
      return msgText; 
     }, 
    } 

}; 

答えて

1

は、あなたのサブスクリプション内の突然変異のフィールドを選択しています。代わりにサブスクリプションフィールドを使用する必要があります。ただ、サブスクリプションを使用するために変異を持つクエリで行を変更します。これに

createIM(fromID: $fromID, toID: $toID, msgText: $msgText){ 

変更して:あなたが正しくあなたのサブスクリプションを設定した場合

IMAdded(fromID: $fromID, toID: $toID){ 

を、そしてIMAddedリゾルバが得られます突然変異の結果をpubsubシステムで取得し、サブスクリプションのサブフィールドをサブスクリプションで直接選択することができます。instant_message

また、IMAddedのmsgText引数を削除しました。それは本当に意味をなさない。サブスクリプション引数を使用してメッセージをフィルタリングすることはできますが、実際のメッセージはルート値を介して送られます。

+0

私はあなたが正しく言っていることに従うようにしたいと思います。 'それは' ''購読(fromID、TOID、updateCommentsQuery){(:文字列!、$ TOID::文字列!、$ MSGTEXT!文字列$ fromID){} のconst SUBSCRIPTION_QUERY = gql' 購読IMAdded}でなければなりません; '' ' – VikR

+0

私はあなたがアドバイスしているかもしれないいくつかの他のバリエーションを考えることができます。私は考えることのできるすべてのバリエーションを含むエラーメッセージを受け取っているので、正しいものを手に入れたいと思っています。正しく続けるために、この部分を正しく取得したいと思っています。 :) – VikR

+0

私はあなたのコードを慎重に読んでいませんでした。私は実際の問題が何であるかを修正するために私の答えを更新しました。 – helfer