Python:Ariadne(服务端)
graphql-github-io-zh-Hans生态项目与库
Python:Ariadne(服务端)
| 属性 | 值 |
|---|---|
| 名称 | Ariadne |
| 描述 | Ariadne 是一个使用 schema-first 方法实现 GraphQL 服务器的 Python 库。它同时支持同步和异步查询执行,内置了针对常见 GraphQL 服务器问题(如查询成本校验、性能追踪)的解决方案,并提供了易于扩展或替换的简洁 API。 |
| 官网 | https://ariadnegraphql.org |
| GitHub | mirumee/ariadne |
可以通过 pip 安装 Ariadne:
bash
pip install ariadne
它内置了许多 GraphQL 服务器实现,便于快速进行实验:
python
from ariadne import ObjectType, QueryType, gql, make_executable_schema
from ariadne.asgi import GraphQL
# Define types using Schema Definition Language (https://graphql.org/learn/schema/)
# Wrapping string in gql function provides validation and better error traceback
type_defs = gql("""
type Query {
hello: String!
}
""")
# Bind resolver functions to Query's fields using QueryType
query_type = QueryType()
# Resolvers are simple python functions
@query_type.field("hello")
def resolve_hello(*_):
return "Hello world!"
# Create executable GraphQL schema
schema = make_executable_schema(type_defs, query_type)
# Create an ASGI app using the schema, running in debug mode
app = GraphQL(schema, debug=True)
上述服务器可以通过 uvicorn 运行:
pip install uvicorn
uvicorn example:app
帮助我们改进文档
发现翻译问题或内容错误?请告诉我们。
