py-graphql-client icon indicating copy to clipboard operation
py-graphql-client copied to clipboard

context manager bug

Open bennnym opened this issue 3 years ago • 3 comments

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__

bennnym avatar Oct 02 '21 01:10 bennnym

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.

ecthiender avatar Oct 02 '21 11:10 ecthiender

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

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

bennnym avatar Oct 02 '21 19:10 bennnym

@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'}

ecthiender avatar May 27 '23 13:05 ecthiender