Description
Describe the bug
The Client has the option to fetch the GraphQL schem from the transport. This is done when the session is initialized and the program enters the context manager, i.e. when doing the following:
client = Client(transport=AIOHTTPTransport(url), fetch_schema_from_transport=True)
with client as session:
# do something with the session
However, it can happen that fetching the schema fails. This throws an exception but since the context manager was not completely entered yet, the transport is not closed as it would be when leaving the context manager. When trying to open a new session, this fails with an TransportAlreadyConnected
Error.
This only happens when re-using the client object, e.g.,
client = Client(transport=AIOHTTPTransport(url), fetch_schema_from_transport=True)
with client as session:
# session is not established because the server is not available
...
with client as session:
# session can now be established
Maybe this is not intended? Should we always create a new client object after an error?
I think this can be easily fixed by catching the error in the __aenter__
method and close the transport. I will add a PR.
To Reproduce
Steps to reproduce the behavior:
- Open a client session to a server that is not yet available
- Wait until the server is available
- Try again to open a client session to the server
Expected behavior
The following should be possible without errors:
client = Client(transport=AIOHTTPTransport(url), fetch_schema_from_transport=True)
try:
with client as session:
# session is not established because the server is not available
except Exception:
pass
sleep(30)
with client as session:
# session can now be established
System info (please complete the following information):
- OS: Linux
- Python version: 3.10
- gql version: 3.0.0rc0
- graphql-core version: 3.1.7