似乎没有调用Apollo订阅突变

时间:2016-10-26 02:34:46

标签: apollostack apollo-server graphql-subscriptions

Apollo的新手,所以我决定采用most simple example I found并尝试以稍微不同的方式进行操作。我的代码可以是found here

我遇到的问题是,当我调用Mutation createTask()时,似乎没有调用Subscription。 Mutation和Subscription在schema.graphql中定义为:

type Mutation {
  createTask(
    text: String!
  ): Task
}

type Subscription {
  taskCreated: Task
}

在resolvers.js中:

Mutation: {
  createTask(_, { text }) {
    const task = { id: nextTaskId(), text, isComplete: false };
    tasks.push(task);
    pubsub.publish('taskCreated', task);
    return task;
  },
},
Subscription: {
  taskCreated(task) {
    console.log(`Subscript called for new task ID ${task.id}`);
    return task;
  },
},

我期待发生的是每次在客户端运行以下命令时,我都会在服务器中获得console.log:

mutation Mutation($text: String!) {
  createTask(text:$text) {
    id
    text
    isComplete
  }
}

但没有任何反应。我错过了什么?

1 个答案:

答案 0 :(得分:0)

当实际订阅GraphQL订阅时,将调用订阅解析程序函数。

由于您没有添加使用SubscriptionClientsetupFunctions订阅您的websocket和订阅的客户端,因此无法使用。

您可以做的是将订阅频道添加到SubscriptionManager的{​​{1}},然后您将获得pubsub.publish功能提供的值。

看起来像这样:



...

const WS_PORT = 8080;

const websocketServer = createServer((request, response) => {
  response.writeHead(404);
  response.end();
});

websocketServer.listen(WS_PORT, () => console.log( // eslint-disable-line no-console
  `Websocket Server is now running on http://localhost:${WS_PORT}`
));

const subscriptionManager = new SubscriptionManager({
  schema: executableSchema,
  pubsub: pubsub,
  setupFunctions: testRunChanged: (options, args) => {
    return {
      taskCreated: {
        filter: (task) => {
          console.log(task); // sould be log when the pubsub is called
          return true;
        }
      },
    };
  },
  ,
});

subscriptionServer = new SubscriptionServer({
  subscriptionManager: subscriptionManager
}, {
  server: websocketServer,
  path: '/',
});

...