Comments (10)
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.
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.
Any news on this @syrusakbary?
from graphql-core-legacy.
Probably it's because @asyncio.coroutine
... let me take a better look later today!
from graphql-core-legacy.
Hey @AlecAivazis this should be solved now... let me know if everything works perfectly!
from graphql-core-legacy.
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.
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.
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.
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.
@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)
- ☂️GraphQL-core v3 HOT 3
- Add support for __str__ in ExecutionResult
- RxPY v3.0 requires Python 3.6
- AST nodes implements __hash__ by id(self) while __eq__ is based on value HOT 2
- Improve log errors
- Unable to return Null/None in a custom Scalar type HOT 3
- Executing a GraphQL query containing types crashes HOT 1
- serialization error HOT 1
- Bug: Using a variable in multiple queries only works for first query HOT 1
- mypy reports invalid syntax with validators/rules/overlapping_fields_can_be_merged.py:344 HOT 5
- Package incompatibility with graphql-core v3 HOT 5
- Pickle support for schema objects
- Add support for custom validators in default GraphQL backend
- mypy errors with AsyncioExecutor(loop=asyncio.get_event_loop())
- Updating mypy to 0.770 raises new errors HOT 2
- Plans for a release >2.3.1 HOT 1
- Exceptions in subscriptions is not logging HOT 2
- Some troubles using AsyncioExecutor and gunicorn
- Parser raises an error for null Boolean Type
- Quiver source
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from graphql-core-legacy.