我是阿波罗联盟的新手。我尝试将我在远程服务器上(在写在 aiohttp 上的其他主机上)上的订阅包含到联合模式中。当我启动我的节点 index.js 时没有问题,但文档中没有订阅,如果我在 altair 中尝试相同的结果。请考虑到订阅本身是完全可行的,因为我可以通过 Swart Web Socket 客户端访问它,它会收到我的计数。我缺少什么以及如何修复它。先感谢您。
这是我的订阅(由 aiohttp 在 python 上编写):
class Subscriptions(graphene.ObjectType):
count_seconds_one = graphene.Float(up_to=graphene.Int())
@async_generator
async def resolve_count_seconds_one(self, info, up_to):
for i in range(up_to):
await yield_(i)
await asyncio.sleep(1.)
await yield_(i)
这是我的 index.js
const { ApolloServer, gql, mergeSchemas, introspectSchema, makeRemoteExecutableSchema } = require('apollo-server')
const { HttpLink } = require('apollo-link-http')
const fetch = require('node-fetch')
const { WebSocketLink } = require('apollo-link-ws')
const { split } = require('apollo-link')
const { getMainDefinition } = require('apollo-utilities')
const { SubscriptionClient } = require('subscriptions-transport-ws')
const ws = require('ws')
const resolvers = {}
async function getRemoteSchema({ uri, subscriptionsUri }) {
const httpLink = new HttpLink({ uri, fetch })
const client = new SubscriptionClient(subscriptionsUri, { reconnect: true }, ws)
const wsLink = new WebSocketLink(client)
const link = split(
operation => {
const definition = getMainDefinition(operation.query)
console.log(definition)
return (
definition.kind === 'OperationDefinition' &&
definition.operation === 'subscription'
)
},
wsLink, // <-- Use this if above function returns true
httpLink // <-- Use this if above function returns false
)
const schema = await introspectSchema(link)
const executableSchema = makeRemoteExecutableSchema({ schema, link })
return executableSchema
}
async function run() {
//
const remoteSchema1 = await getRemoteSchema({ uri: 'http://127.0.0.1:8000',
subscriptionsUri: 'ws://localhost:8005/subscriptions'
})
const remoteSchema3 = await getRemoteSchema({
uri: 'http://localhost:8005/graphql',
subscriptionsUri: 'ws://localhost:8005/subscriptions'
})
const resolvers = {}
const schema = mergeSchemas({
schemas: [
remoteSchema1,
remoteSchema3,
],
})
const server = new ApolloServer({ schema })
server.listen().then(({ url, subscriptionsUrl }) => {
console.log(` Server ready at ${url}`);
console.log(` Subscriptions ready at ${subscriptionsUrl}`);
})
}
run().catch(console.log)