'Aiohttp with graphql: String cannot represent value
I am creating an aiohttp api with graphql (aiohttp_graphql) and is giving this problem. Why?
aiohttp version: aiohttp==3.7.3 -e git+https://github.com/graphql-python/aiohttp-graphql.git@5f7580310761dd7de33b44bc92f30b2695f2d523#egg=aiohttp_graphql
This is my Code:
import asyncio
from aiohttp import web
from aiohttp_graphql import GraphQLView
from graphql.execution.executors.asyncio import AsyncioExecutor
from graphql import (graphql, GraphQLSchema, GraphQLObjectType, GraphQLField, GraphQLString)
async def resolve_hello(root, info):
await asyncio.sleep(3)
return 'World!'
Schema = GraphQLSchema(
query=GraphQLObjectType(
name='HelloQuery',
fields={
'hello': GraphQLField(
GraphQLString,
resolve=resolve_hello),
},
))
app = web.Application()
GraphQLView.attach(
app,
route_path='/graphql',
schema=Schema,
graphiql=True,
executor=AsyncioExecutor())
if __name__ == '__main__':
web.run_app(app)
When i run GraphiQL:
query {
hello
}
GraphiQL result:
{
"errors": [
{
"message": "String cannot represent value: <coroutine resolve_hello>",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"hello"
]
}
],
"data": {
"hello": null
}
}
Return of terminal: RuntimeWarning: coroutine 'resolve_hello' was never awaited
Solution 1:[1]
It looks like aiohttp_graphql got merged into graphql_server project
So after installation
> pip install graphql-server[aiohttp] jinja2
this one seems to be working
import asyncio
from aiohttp import web
from graphql import (GraphQLField,
GraphQLObjectType,
GraphQLSchema,
GraphQLString)
from graphql_server.aiohttp import GraphQLView
async def resolve_hello(root, info):
await asyncio.sleep(3)
return 'World!'
Schema = GraphQLSchema(
query=GraphQLObjectType(
name='HelloQuery',
fields={
'hello': GraphQLField(
GraphQLString,
resolve=resolve_hello),
},
))
app = web.Application()
GraphQLView.attach(app,
route_path='/graphql',
schema=Schema,
graphiql=True,
# without this parameter coroutines as resolvers won't work
enable_async=True)
if __name__ == '__main__':
web.run_app(app)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
