我正在关注这个关于 graphql 订阅的 twilio 博客,使用ariadine
. 但是,当尝试在操场上进行订阅时,我得到了错误:
{
"error": "Could not connect to websocket endpoint ws://localhost:3001/. Please check if the endpoint url is correct."
}
这是我要发送的订阅
subscription {
post {
error {
message
field
}
post {
postId
createdAt
caption
}
}
}
这是我的 typedef
schema {
query: Query
mutation: Mutation
subscription: Subscription
}
input PostInput {
caption: String!
}
type Post {
postId: String!
createdAt: String!
caption: String!
}
type ErrorType {
field: String!
message: String!
}
type PostResponse {
error: ErrorType
post: Post
}
type Mutation {
createPost(input: PostInput!): PostResponse!
}
type Subscription {
post: PostResponse
}
这是我的解析器。
- 突变
@mutation.field("createPost")
async def create_post_resolver(obj, info, input):
try:
post = Post(postId=uuid4(), caption=input["caption"])
db.session.add(post)
db.session.commit()
for queue in queues:
queue.put(post)
return{
"error": None,
"post": post
}
except Exception as e:
return{
"error": {"message":str(e), "field": "unknown"},
"post": None
}
- 订阅
@subscription.source("post")
async def posts_source(obj, info):
queue = asyncio.Queue(maxsize=0)
queues.append(queue)
try:
while True:
post = await queue.get()
queue.task_done()
payload = {
"post": post,
"error": None
}
yield payload
except asyncio.CancelledError:
queues.remove(queue)
raise
@subscription.field("post")
async def posts_resolver(payload, info):
return payload
这个错误的可能原因是什么。