AIOHTTP with Graphql: TypeError: __init__() got an unexpected keyword argument 'resolve'。为啥会出现这个问题?

Posted

技术标签:

【中文标题】AIOHTTP with Graphql: TypeError: __init__() got an unexpected keyword argument \'resolve\'。为啥会出现这个问题?【英文标题】:AIOHTTP with Graphql: TypeError: __init__() got an unexpected keyword argument 'resolve'. Why this problem?AIOHTTP with Graphql: TypeError: __init__() got an unexpected keyword argument 'resolve'。为什么会出现这个问题? 【发布时间】:2021-03-06 17:07:33 【问题描述】:

我正在关注 GraphQL-core 3 文档。为什么会出现这个问题?

代码:

import asyncio
from graphql import (
    graphql, GraphQLSchema, GraphQLObjectType, GraphQLField, GraphQLString)


async def resolve_hello(obj, info):
    await asyncio.sleep(3)
    return 'world'

schema = GraphQLSchema(
    query=GraphQLObjectType(
        name='RootQueryType',
        fields=
            'hello': GraphQLField(
                GraphQLString,
                resolve=resolve_hello)
        ))


async def main():
    query = ' hello '
    print('Fetching the result...')
    result = await graphql(schema, query)
    print(result)


loop = asyncio.get_event_loop()
try:
    loop.run_until_complete(main())
finally:
    loop.close()

运行脚本后,终端返回如下错误

错误:

Traceback (most recent call last):
  File "server.py", line 16, in <module>
    resolve=resolve_hello)
TypeError: __init__() got an unexpected keyword argument 'resolve'

【问题讨论】:

【参考方案1】:

这是 graphql 版本的问题。例如在版本0.5.3 上,我遇到了和你一样的问题。

from graphql import (
    GraphQLSchema, GraphQLObjectType, GraphQLField, GraphQLString)

schema = GraphQLSchema(
    query=GraphQLObjectType(
        name='RootQueryType',
        fields=
            'hello': GraphQLField(
                GraphQLString,
                resolve=lambda obj, info: 'world')
        ))
>>> TypeError: __init__() got an unexpected keyword argument 'resolve'

刚刚更改为版本 3 或更高版本。

pip install "graphql-core>=3"

【讨论】:

以上是关于AIOHTTP with Graphql: TypeError: __init__() got an unexpected keyword argument 'resolve'。为啥会出现这个问题?的主要内容,如果未能解决你的问题,请参考以下文章

aiohttp浣跨敤鏂规硶

python 使用aiohttp通过https://justanr.github.io/getting-start-with-aiohttpweb-a-todo-tutorial与Postgres进行

python 使用aiohttp通过https://justanr.github.io/getting-start-with-aiohttpweb-a-todo-tutorial在内存中进行简单的待办

[GraphQL] Query a GraphQL API with graphql-request

[GraphQL] Reuse GraphQL Selection Sets with Fragments

[GraphQL] Reuse Query Fields with GraphQL Fragments