Apollo Publish&订阅错误:无法查询字段?

时间:2016-10-03 22:15:52

标签: graphql apollo apollo-server

我正在使用GitHunt-API和GitHunt-React中的示例来学习新的Apollo pubsub代码。当我在客户端上调用subscribe时,我收到控制台日志错误:

  

“无法在”订阅“类型上查询字段”createIM“。

我试图非常密切地关注示例代码,但我遗漏了一些东西。

如何更正此错误?

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;

订阅(服务器代码)

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 个答案:

答案 0 :(得分:1)

您正在订阅中选择一个变异字段。您必须改为使用订阅字段。只需更改查询中具有使用订阅的变异的行:

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

将其更改为:

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

如果您正确设置了订阅,那么IMAdded解析器将通过pubsub系统获得变异结果,您可以直接在订阅上选择instant_message的子字段。

另外,请注意我删除了IMAdded的msgText参数。它没有任何意义。订阅参数可用于过滤消息,但实际消息将通过根值传入。