I was following this twilio blog on graphql subscriptions using ariadine
. But when itry to make a subscription in the playground im getting the error:
{
"error": "Could not connect to websocket endpoint ws://localhost:3001/. Please check if the endpoint url is correct."
}
Here is the subscription that I'm sending
subscription {
post {
error {
message
field
}
post {
postId
createdAt
caption
}
}
}
here are my typedefs
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
}
Here is my resolvers.
- mutations
@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
}
- subscriptions
@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
What maybe possibly the cause of this error.