首先,如果我有下一个代码结构,订阅就可以工作
图式
type Subscription {
somethingChanged: IResult
}
type IResult {
id: String!
}
解析器
export const resolvers = {
Query: {
},
Mutation: {
},
Subscription: {
somethingChanged: {
subscribe: () => pubSub.asyncIterator(SOMETHING_CHANGED_TOPIC)
}
},
};
当我尝试将somethingChanged
节点作为另一个account
节点的子节点时,问题就变成了一个问题,如下所示:
所以下一个模式和解析器不起作用
type Subscription {
account(token: String!): IAccountAction
}
type IResult {
id: String!
}
type IAccountAction {
somethingChanged: IResult
}
export const resolvers = {
Query: {
},
Mutation: {
},
Subscription: {
account: (root: any, token: string) => ({
somethingChanged: {
subscribe: () => pubSub.asyncIterator(SOMETHING_CHANGED_TOPIC)
}
})
},
};
错误
{ "error": { "message": "订阅字段必须返回 Async Iterable。收到:未定义" } }
我相信这不是问题,只是误解))请帮助我))
可能有帮助的附加信息:
服务器.ts
import {resolvers} from './generated/resolvers';
import {typeDefs} from './generated/mergedGQLSchemas';
import http from 'http';
const express = require('express');
const { ApolloServer, gql } = require('apollo-server-express');
const PORT = 4001;
const app = express();
const server = new ApolloServer({
typeDefs,
resolvers,
});
server.applyMiddleware({
app
});
const httpServer = http.createServer(app);
server.installSubscriptionHandlers(httpServer);
httpServer.listen(PORT, () => {
console.log(` Server ready at http://localhost:${PORT}${server.graphqlPath}`);
console.log(` Subscriptions ready at ws://localhost:${PORT}${server.subscriptionsPath}`);
});