ecthiender / py-graphql-client

Dead-simple GraphQL client with subscriptions over websockets
https://pypi.org/project/py-graphql-client/
Other
37 stars 11 forks source link

context manager bug #33

Open bennnym opened 3 years ago

bennnym commented 3 years ago

running the simple example on the homepage

from graphql_client import GraphQLClient

def callback(_id, data):
  print("got new data..")
  print(f"msg id: {_id}. data: {data}")

with GraphQLClient(endpoint) as client:
  sub_id = client.subscribe(subscription,
                            # variables={'limit': 10},
                            headers=new_header,
                            callback=callback)

  client.stop_subscribe(sub_id)

returns the following:

raceback (most recent call last):
  File "dro_auth.py", line 91, in <module>
    with GraphQLClient(endpoint) as client:
AttributeError: __enter__
ecthiender commented 3 years ago

Hi @bennnym , thanks for the bug report.

I think the error happens because there is no support for context manager yet. Would you mind opening a PR to support it?

Otherwise I can take a look at this in few days.

bennnym commented 3 years ago

Hi @ecthiender, I didn't realise it wasn't supported.

Why is it in the examples on the readme if it isn't?

ecthiender commented 1 year ago

@bennnym sorry I'm dumb. It is supported.

I tried this code (same as your code, adding missing variables) and it seems to work:

import time
from graphql_client import GraphQLClient

endpoint = 'ws://localhost:8080/graphql'
new_header = {
    'authorization': 'secret'
}

subscription = """
  subscription {
    notifications {
      id
      title
      content
    }
  }
"""

def callback(_id, data):
  print("got new data..")
  print(f"msg id: {_id}. data: {data}")

with GraphQLClient(endpoint) as client:
  sub_id = client.subscribe(subscription,
                            # variables={'limit': 10},
                            headers=new_header,
                            callback=callback)

  print('got sub id', sub_id)
  time.sleep(2)
  client.stop_subscribe(sub_id)

I get this output:

got sub id b48cfc9393ed46e099d7e7252155a965
got new data..
msg id: b48cfc9393ed46e099d7e7252155a965. data: {'type': 'error', 'id': 'b48cfc9393ed46e099d7e7252155a965', 'payload': {'errors': [{'extensions': {'code': 'validation-failed', 'path': '$.selectionSet.notifications'}, 'message': "field 'notifications' not found in type: 'subscription_root'"}]}}
got new data..
msg id: b48cfc9393ed46e099d7e7252155a965. data: {'type': 'complete', 'id': 'b48cfc9393ed46e099d7e7252155a965'}