Javascript 阿波罗服务器&x2B;对阿波罗pubSub不显示数据作出反应

Javascript 阿波罗服务器&x2B;对阿波罗pubSub不显示数据作出反应,javascript,graphql,apollo-server,react-apollo,Javascript,Graphql,Apollo Server,React Apollo,在一次变异之后,我似乎无法在我的react前端接收到pubSub数据 我有以下Graphql模式: type SSiD { id: ID! name: String status: String hidden: Boolean } type Subscription { SSiDAdded: SSiD } 在我的Apollo服务器上,在mudation之后,我像这样发送pubSub数据 const data = result.dataValues data

在一次变异之后,我似乎无法在我的react前端接收到pubSub数据

我有以下Graphql模式:

type SSiD {
    id: ID!
    name: String
    status: String
    hidden: Boolean
 }
type Subscription {
  SSiDAdded: SSiD
}
在我的Apollo服务器上,在mudation之后,我像这样发送pubSub数据

const data = result.dataValues
data.__typename = 'SSiD'
console.log(data)
context.pubsub.publish('SSiDAdded', data)
该console.log将输出:

{ id: 2208,
  name: 'FooBar',
  hidden: true,
  status: 'broadcasting',
  updatedAt: 2016-10-27T22:07:09.119Z,
  createdAt: 2016-10-27T22:07:09.119Z,
  __typename: 'SSiD' }
最后,在我的react前端,我有以下内容:

const query = gql`
  subscription ssidList{
    SSiDAdded{
      id
      name
      hidden
    }
  }
`
this.subscriptionObserver = this.props.client.subscribe({
  query
})
.subscribe({
  next (data) {
    console.log('The subscription data', data)
  },
  error (err) {
    console.error('Error subscription', err)
  }
})
}

在上面的console.log上,订阅数据始终为空


我是否包装了错误的响应或类似的内容?

这里有一些东西需要检查。在我的模式中,它有一些语法,我目前在你的中没有看到。请参见查询字符串后的
:即时消息

const typeDefinitions = [`

type instant_message {
  id: Int
  fromID: String
  toID: String
  msgText: String
}
type Query {
  instant_message(id: Int, 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(id: Int, fromID: String!, toID: String!): instant_message
}

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

`];
我在客户端上也有一些不同的语法:

subscribe(fromID, toID, updateQueryViaSubscription) {
    const SUBSCRIPTION_QUERY = gql`
      subscription getIMsViaSubscription($fromID: String!, $toID: String!){
          IMAdded(fromID:$fromID, toID: $toID){
            id,
            fromID,
            toID,
            msgText
          }
        } 
`;
    this.subscriptionObserver = this.props.client.subscribe({
        query: SUBSCRIPTION_QUERY,
        variables: { fromID: this.fromID, toID: this.toID },
    }).subscribe({
        next(data) {
            const newMsag = data.IMAdded;
            updateQueryViaSubscription((previousResult) => {
                // if it's our own mutation, we might get the subscription result
                // after the mutation result.
                // if (isDuplicateIM(newComment, previousResult.entry.comments)) {
                //     return previousResult;
                // }
                // update returns a new "immutable" list with the new comment
                // added to the front.
                return update(
                    previousResult,
                    {
                        instant_message: {
                            $push: [newMsag],
                        },
                    }
                );
            });
        },
        error(err) {
            console.error('err', err); },
    });
}
请检查这一点,并让我知道更新后的代码是否消除了该错误

更新:根据我们关于Slack的讨论,您发现您需要您的executableSchema如下:

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

export default executableSchema;
这是我正在使用的解析器:

Subscription: {
    IMAdded(IMThatWasAdded) {
        var ret = IMThatWasAdded;
        return ret;
    }
}

这里有几件事要检查。在我的模式中,它有一些语法,我目前在你的中没有看到。请参见查询字符串后的
:即时消息

const typeDefinitions = [`

type instant_message {
  id: Int
  fromID: String
  toID: String
  msgText: String
}
type Query {
  instant_message(id: Int, 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(id: Int, fromID: String!, toID: String!): instant_message
}

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

`];
我在客户端上也有一些不同的语法:

subscribe(fromID, toID, updateQueryViaSubscription) {
    const SUBSCRIPTION_QUERY = gql`
      subscription getIMsViaSubscription($fromID: String!, $toID: String!){
          IMAdded(fromID:$fromID, toID: $toID){
            id,
            fromID,
            toID,
            msgText
          }
        } 
`;
    this.subscriptionObserver = this.props.client.subscribe({
        query: SUBSCRIPTION_QUERY,
        variables: { fromID: this.fromID, toID: this.toID },
    }).subscribe({
        next(data) {
            const newMsag = data.IMAdded;
            updateQueryViaSubscription((previousResult) => {
                // if it's our own mutation, we might get the subscription result
                // after the mutation result.
                // if (isDuplicateIM(newComment, previousResult.entry.comments)) {
                //     return previousResult;
                // }
                // update returns a new "immutable" list with the new comment
                // added to the front.
                return update(
                    previousResult,
                    {
                        instant_message: {
                            $push: [newMsag],
                        },
                    }
                );
            });
        },
        error(err) {
            console.error('err', err); },
    });
}
请检查这一点,并让我知道更新后的代码是否消除了该错误

更新:根据我们关于Slack的讨论,您发现您需要您的executableSchema如下:

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

export default executableSchema;
这是我正在使用的解析器:

Subscription: {
    IMAdded(IMThatWasAdded) {
        var ret = IMThatWasAdded;
        return ret;
    }
}
在后端,我使用“:“它是:SSiD,只是我没有接受任何参数,所以它变成了name:ReturnType,在前端,我认为有点类似于”queryName{subscriptionName{fields}}“,在后端,我使用“:“它是:SSiD,只是我没有接受任何参数,所以它变成了name:ReturnType,在前端,我认为类似于“queryName{subscriptionName{fields}”