Skip to content

refactor(graphql): tighten graphql-core usage#79

Open
allmonday wants to merge 1 commit into
masterfrom
refactor/graphql-core-usage
Open

refactor(graphql): tighten graphql-core usage#79
allmonday wants to merge 1 commit into
masterfrom
refactor/graphql-core-usage

Conversation

@allmonday

Copy link
Copy Markdown
Collaborator

Summary

graphql-core 在项目里只承担 parser + AST 库 的角色(schema 构建、执行、校验、introspection 都是自研)。本 PR 在保持这一定位的前提下,清理掉围绕 graphql-core 长出来的冗余代码。

  • 删死代码:移除 handler._is_introspection_query / _execute_introspection / IntrospectionGenerator.is_introspection_query 及其自证测试。__schema / __type 已在 QueryExecutor.execute_query:96-104 内联处理,老的分发函数无生产调用方。
  • 消除双 parsehandler.execute 改为 parse(query) 一次后传 DocumentNode 给 executor;QueryParser 新增 parse_document(document) 走共享 AST 路径,旧 parse(query) 签名保留以兼容 use_case/selection.py。executor 不再第二次 parse(之前仅为读 operation_type)。
  • AST value 转换统一query_parser._value_node_to_pythonargument_builder._extract_value 都改用 graphql-core 的 value_from_ast_untypedintrospection.py 早已用此 API)。build_arguments 删掉手写 VariableNode 分支。顺手修了 _value_node_to_pythonVariableNode 静默返回 None 的雷。

净效果:−89 行,每条 query 少一次 lex+parse,AST value 转换路径从 3 套收到 1 套。

Test plan

  • uv run ruff check src/ — clean
  • uv run pytest tests/ — 940 passed / 6 skipped / 0 failed
  • ./scripts/check-ci.sh — green (ruff + pytest + skill template compile)
  • tests/test_argument_types.py 17 个 value-conversion 测试全过(int/float/str/bool/null/list/object/enum/datetime/variable)
  • tests/test_query_parser.py 8 个 value-type 测试全过
  • tests/test_query_executor.py 13 个 executor 调用点已迁移到新 execute_query(document, ...) 签名

🤖 Generated with Claude Code

graphql-core stays scoped as parser + AST library; this PR removes
redundant work that had grown around it.

- Drop dead introspection dispatch (_is_introspection_query /
  _execute_introspection / IntrospectionGenerator.is_introspection_query)
  and its self-justifying test — __schema/__type already inlined in
  QueryExecutor.execute_query.
- Eliminate double parse: handler now parses the query once and passes
  the DocumentNode to executor.execute_query (was query: str, which
  re-parsed internally just to read operation_type). QueryParser gains
  parse_document(document) for the shared-AST path; parse(query) keeps
  the old signature for callers that only need FieldSelections.
- Unify AST value→Python conversion on graphql-core's
  value_from_ast_untyped (introspection.py already used it). Replaces
  query_parser._value_node_to_python and argument_builder._extract_value
  — ~50 lines of duplicated isinstance / __class__.__name__ branches
  collapse to a one-line delegation. VariableNode handling in
  build_arguments goes away too; value_from_ast_untyped resolves
  variables when given a variables dict.
- Side fix: query_parser._value_node_to_python used to return None
  silently for VariableNode; now goes through value_from_ast_untyped.

Net: -89 lines, one parse per request, single value-conversion path.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant