Giter Club home page Giter Club logo

Comments (10)

syrusakbary avatar syrusakbary commented on June 10, 2024

I've added the loop option in AsyncioExecutor, now you can pass your event loop if any :)
Related commit: graphql-python/graphql-core@3258d29

Closing the issue. Please reopen if you run into any other problems with this.

from graphql-core-legacy.

AlecAivazis avatar AlecAivazis commented on June 10, 2024

So it seems the particular bug has been fixed, unfortunately I'm still unable to successfully execute a query with the asyncio executor. Here is my test case:

import asyncio
import graphene
from graphql.execution.executors.asyncio import AsyncioExecutor

# get the current event loop
loop = asyncio.get_event_loop()

class Query(graphene.ObjectType):
    hello = graphene.String()

    @asyncio.coroutine
    def resolve_hello(self, *args, **kwds):
        return 'world'

schema = graphene.Schema(
    query=Query,
    executor=AsyncioExecutor(loop=loop)
)


result = schema.execute("""
    query {
        hello
    }
 """)

print(result.data)

Which results in:

OrderedDict([('hello', '<generator object Query.resolve_hello at 0x10b3844c0>')])

I might be doing something wrong - if i'm understanding the source code correctly, wrapping the resolver in @asyncio.coroutine should be enough to have the future yielded, right? That doesn't seem to be happening...

from graphql-core-legacy.

AlecAivazis avatar AlecAivazis commented on June 10, 2024

Any news on this @syrusakbary?

from graphql-core-legacy.

syrusakbary avatar syrusakbary commented on June 10, 2024

Probably it's because @asyncio.coroutine... let me take a better look later today!

from graphql-core-legacy.

syrusakbary avatar syrusakbary commented on June 10, 2024

Hey @AlecAivazis this should be solved now... let me know if everything works perfectly!

from graphql-core-legacy.

steveklewis avatar steveklewis commented on June 10, 2024

I'm still seeing the behavior just returning the string. I'm using Python 3.5.2 with these libs:

asyncio (3.4.3)
Cython (0.24.1)
graphene (0.10.2)
graphql-core (1.0)
graphql-relay (0.4.4)
iso8601 (0.1.11)
pip (8.1.2)
promise (0.4.2)
setuptools (20.10.1)
six (1.10.0)

It looks like I have the latest code if I go into site-packages but maybe there's something dumb I'm doing. Just wanted to put in my $.02 cents since @AlecAivazis didn't reply that it was working for him, in case this helps you.

from graphql-core-legacy.

bcb avatar bcb commented on June 10, 2024

Seems to work for me @steveklewis

import asyncio
import graphene
from graphql.execution.executors.asyncio import AsyncioExecutor

class Query(graphene.ObjectType):
    hello = graphene.String(description='Hello')

    async def resolve_hello(self, *args, **kwds):
        return 'world'

schema = graphene.Schema(query=Query)
executor = AsyncioExecutor(loop=asyncio.get_event_loop())
result = schema.execute('{hello}', executor=executor)

print(result.data)

Results in

OrderedDict([('hello', 'world')])

from graphql-core-legacy.

japrogramer avatar japrogramer commented on June 10, 2024

I get an error

            result = schema.execute(
                          payload['query'],
                          variable_values=payload['variables'],
                          root_value=rx.Observable.create(stream).share(),
                          allow_subscriptions=True,
                          executor=AsyncioExecutor(loop=asyncio.get_event_loop()),
                          **{'context_value': message})
    async def resolve_sub_product(self, info, **input):
        await make_sub(info, input.get('product'))
        name = ProductType._meta.model.__class__.__name__
        subscribe = info.context['subscribe']
        if subscribe:
            subscribe(name)

        stream = info.root_value
        return stream.map(lambda message: self.next(message, info, **input))

    (Pdb) result.errors
           [RuntimeError('This event loop is already running',)]

from graphql-core-legacy.

japrogramer avatar japrogramer commented on June 10, 2024

I made some more changes and believe i am closer

            result = await graphql(schema,
                          payload['query'],
                          variable_values=payload['variables'],
                          root_value=rx.Observable.create(stream).share(),
                          allow_subscriptions=True,
                          executor=AsyncioExecutor(loop=asyncio.get_event_loop()),
                          **{'context_value': message})

[2018/06/10 03:20:35] HTTP POST /graphql 200 [0.04, 10.0.0.4:48010]
> /app/apple/graphquery/consumers.py(75)websocket_receive()
-> result = await graphql(schema,
(Pdb) c
> /app/apple/product/schema.py(179)resolve_sub_product()
-> await make_sub(info, input.get('product'))
(Pdb) n
> /app/apple/product/schema.py(180)resolve_sub_product()
-> name = ProductType._meta.model.__class__.__name__
(Pdb) n
> /app/apple/product/schema.py(182)resolve_sub_product()
-> stream = info.root_value
(Pdb) n
> /app/apple/product/schema.py(183)resolve_sub_product()
-> return stream.map(lambda message: self.next(message, info, **input))
(Pdb) n
2018-06-10 03:20:43,619 - ERROR - server - Exception inside application: object ExecutionResult can't be used in 'await' expression
  File "/usr/local/lib/python3.6/site-packages/channels/sessions.py", line 175, in __call__
    return await self.inner(receive, self.send)
  File "/usr/local/lib/python3.6/site-packages/channels/consumer.py", line 54, in __call__
    await await_many_dispatch([receive, self.channel_receive], self.dispatch)
  File "/usr/local/lib/python3.6/site-packages/channels/utils.py", line 50, in await_many_dispatch
    await dispatch(result)
  File "/usr/local/lib/python3.6/site-packages/channels/consumer.py", line 67, in dispatch
    await handler(message)
  File "/app/apple/graphquery/consumers.py", line 75, in websocket_receive
    result = await graphql(schema,
  object ExecutionResult can't be used in 'await' expression
[2018/06/10 03:20:43] WebSocket DISCONNECT /gql [10.255.0.2:52896]


from graphql-core-legacy.

blazewicz avatar blazewicz commented on June 10, 2024

@japrogramer you propably found solution already but I think this issue requires some conclusion

schema.execute() with default value of return_promise - a False - will fail in asyncio context (when run from a coroutine) with "Event loop already running" Exception. This is because execute internally calls wait_until_finished() which always tries to start the loop. Puropose of this function is to run coroutines asynchronously in synchronous context. This is why example shown by @bcb works.

Solution for your case - running AsyncioExecutor from asyncio context - is shown in #67. Simply add return_promise=True to call to execute and you'll receive a Promise which you can await in your coroutine.

from graphql-core-legacy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.