Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

context manager bug #33

Open
bennnym opened this issue Oct 2, 2021 · 3 comments
Open

context manager bug #33

bennnym opened this issue Oct 2, 2021 · 3 comments

Comments

@bennnym
Copy link

bennnym commented Oct 2, 2021

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
Copy link
Owner

ecthiender commented Oct 2, 2021

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
Copy link
Author

bennnym commented Oct 2, 2021

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

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

@ecthiender
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants