Ariadne Codegen 项目教程
1. 项目目录结构及介绍
Ariadne Codegen 项目的目录结构如下:
ariadne-codegen/
├── ariadne_codegen/
│ ├── client_generators/
│ ├── dependencies/
│ ├── plugins/
│ ├── __init__.py
│ ├── cli.py
│ ├── config.py
│ ├── main.py
│ └── utils.py
├── tests/
│ ├── test_client_generators/
│ ├── test_dependencies/
│ ├── test_plugins/
│ ├── __init__.py
│ └── test_main.py
├── .gitignore
├── CHANGELOG.md
├── CONTRIBUTING.md
├── EXAMPLE.md
├── LICENSE
├── PLUGINS.md
├── README.md
└── pyproject.toml
目录结构介绍
-
ariadne_codegen/: 项目的主要代码目录,包含了生成代码的核心逻辑。
- client_generators/: 包含生成客户端代码的模块。
- dependencies/: 包含项目依赖的模块。
- plugins/: 包含插件相关的模块。
- init.py: 模块初始化文件。
- cli.py: 命令行接口的实现。
- config.py: 配置文件处理模块。
- main.py: 项目的主入口文件。
- utils.py: 工具函数模块。
-
tests/: 包含项目的测试代码。
- test_client_generators/: 测试客户端生成器的模块。
- test_dependencies/: 测试依赖模块的模块。
- test_plugins/: 测试插件模块的模块。
- init.py: 测试模块初始化文件。
- test_main.py: 主测试文件。
-
.gitignore: Git 忽略文件配置。
-
CHANGELOG.md: 项目变更日志。
-
CONTRIBUTING.md: 贡献指南。
-
EXAMPLE.md: 项目示例文档。
-
LICENSE: 项目许可证。
-
PLUGINS.md: 插件文档。
-
README.md: 项目介绍文档。
-
pyproject.toml: 项目配置文件。
2. 项目的启动文件介绍
项目的启动文件是 ariadne_codegen/main.py
。这个文件包含了项目的核心逻辑,负责读取配置、生成代码并输出结果。
主要功能
- 读取配置: 从
pyproject.toml
文件中读取配置信息。 - 生成代码: 根据配置生成 Python 客户端代码。
- 输出结果: 将生成的代码输出到指定目录。
3. 项目的配置文件介绍
项目的配置文件是 pyproject.toml
。这个文件包含了项目的所有配置信息,包括 GraphQL 模式路径、查询路径、生成的包名等。
配置文件示例
[tool.ariadne-codegen]
schema_path = "schema.graphql"
queries_path = "queries.graphql"
target_package_name = "graphql_client"
target_package_path = "./generated"
client_name = "Client"
client_file_name = "client"
base_client_name = "AsyncBaseClient"
base_client_file_path = "ariadne_codegen/client_generators/dependencies/async_base_client.py"
enums_module_name = "enums"
input_types_module_name = "input_types"
include_comments = true
convert_to_snake_case = true
async_client = true
files_to_include = []
plugins = []
配置项介绍
- schema_path: GraphQL 模式文件路径。
- queries_path: GraphQL 查询文件路径。
- target_package_name: 生成的包名。
- target_package_path: 生成包的输出路径。
- client_name: 生成的客户端类名。
- client_file_name: 生成的客户端文件名。
- base_client_name: 基础客户端类名。
- base_client_file_path: 基础客户端类文件路径。
- enums_module_name: 生成的枚举模块名。
- input_types_module_name: 生成的输入类型模块名。
- include_comments: 是否在生成的文件中包含注释。
- convert_to_snake_case: 是否将字段和参数名转换为蛇形命名法。
- async_client: 是否生成异步客户端。
- files_to_include: 需要包含到生成包中的文件列表。
- plugins: 使用的插件列表。
通过这些配置项,用户可以灵活地定制生成的 Python 客户端代码。