Aiohttp с graphql: строка не может представлять значение

#python #graphql #aiohttp

#python #graphql #aiohttp

Вопрос:

Я создаю API aiohttp с graphql (aiohttp_graphql) и создаю эту проблему. Почему?

версия aiohttp: aiohttp==3.7.3 -e git https://github.com/graphql-python/aiohttp-graphql.git@5f7580310761dd7de33b44bc92f30b2695f2d523#egg=aiohttp_graphql

Это мой код:

 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)
 

Когда я запускаю GraphiQL:

 query {
  hello
}
 

Результат GraphiQL:

 {
  "errors": [
    {
      "message": "String cannot represent value: <coroutine resolve_hello>",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "hello"
      ]
    }
  ],
  "data": {
    "hello": null
  }
}
 

Возврат терминала: предупреждение о времени выполнения: сопрограмма ‘resolve_hello’ никогда не ожидалась

Ответ №1:

Похоже, что она aiohttp_graphql была объединена в graphql_server проект

Итак, после установки

 > pip install graphql-server[aiohttp] jinja2
 

похоже, это работает

 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)