Graphql consumption

32 views
Skip to first unread message

Richard D

unread,
Jul 29, 2020, 9:57:33 AM7/29/20
to web2py-users
Dear Web2py friends,

I am trying to cosume a graphql endpoint using https://github.com/graphql-python/gql#HTTP-async-transport but get an error.

Applied code:

def qlquery():
    from gql import gql, Client, AIOHTTPTransport

    # Select your transport with a defined url endpoint
    transport = AIOHTTPTransport(url="https://countries.trevorblades.com/")

    # Create a GraphQL client using the defined transport
    client = Client(transport=transport, fetch_schema_from_transport=True)

    # Provide a GraphQL query
    query = gql(
        """
        query getContinents {
        continents {
            code
            name
            }
        }
    """
    )

    # Execute the query on the transport
    result = client.execute(query)
    print(result)

    return locals()


Gives:

class 'RuntimeError'> There is no current event loop in thread 'Thread-13'.

Versie

web2py™ Version 2.20.4-stable+timestamp.2020.05.03.05.18.50
Python Python 3.8.2: C:\Program Files\Python38\python.exe (prefix: C:\Program Files\Python38)

Traceback

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
Traceback (most recent call last):
File "E:\web2py_src\web2py\gluon\restricted.py", line 219, in restricted
exec(ccode, environment)
File "E:/web2py_src/web2py/applications/insp/controllers/qlquery.py", line 27, in <module>
File "E:\web2py_src\web2py\gluon\globals.py", line 430, in <lambda>
self._caller = lambda f: f()
File "E:/web2py_src/web2py/applications/insp/controllers/qlquery.py", line 23, in qlquery
result = client.execute(query)
File "C:\Users\richa\AppData\Roaming\Python\Python38\site-packages\gql\client.py", line 113, in execute
loop = asyncio.get_event_loop()
File "C:\Program Files\Python38\lib\asyncio\events.py", line 639, in get_event_loop
raise RuntimeError('There is no current event loop in thread %r.'
RuntimeError: There is no current event loop in thread 'Thread-13'.

This looks like a asyncio issue as mentioned in https://github.com/graphql-python/gql#HTTP-async-transport
Here they suggest to use :

from gql import gql, AIOHTTPTransport, Client import asyncio async def main(): transport = AIOHTTPTransport(url='https://countries.trevorblades.com/graphql') # Using `async with` on the client will start a connection on the transport # and provide a `session` variable to execute queries on this connection async with Client( transport=transport, fetch_schema_from_transport=True, ) as session: # Execute single query query = gql(''' query getContinents { continents { code name } } ''') result = await session.execute(query) print(result) asyncio.run(main())

But this 'hangs'

Does anybody has some experience with this?

Kind regards,
Richard

Richard D

unread,
Jul 29, 2020, 3:26:57 PM7/29/20
to web2py-users
Looking at Jupyter Notebook use give me the hint for the solution.

    from gql.transport.requests import RequestsHTTPTransport

    _transport = RequestsHTTPTransport(
        use_json=True,
    )

Issue solved :)

Richard
Reply all
Reply to author
Forward
0 new messages