【问题标题】: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'

【问题讨论】:

    标签: python graphql aiohttp


    【解决方案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"
    

    【讨论】:

      猜你喜欢
      • 2021-01-19
      • 1970-01-01
      • 1970-01-01
      • 2012-05-07
      • 1970-01-01
      • 2021-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多