refactor(graphql): tighten graphql-core usage#79
Open
allmonday wants to merge 1 commit into
Open
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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内联处理,老的分发函数无生产调用方。handler.execute改为parse(query)一次后传DocumentNode给 executor;QueryParser新增parse_document(document)走共享 AST 路径,旧parse(query)签名保留以兼容use_case/selection.py。executor 不再第二次 parse(之前仅为读operation_type)。query_parser._value_node_to_python和argument_builder._extract_value都改用 graphql-core 的value_from_ast_untyped(introspection.py早已用此 API)。build_arguments删掉手写VariableNode分支。顺手修了_value_node_to_python对VariableNode静默返回 None 的雷。净效果:−89 行,每条 query 少一次 lex+parse,AST value 转换路径从 3 套收到 1 套。
Test plan
uv run ruff check src/— cleanuv run pytest tests/— 940 passed / 6 skipped / 0 failed./scripts/check-ci.sh— green (ruff + pytest + skill template compile)tests/test_argument_types.py17 个 value-conversion 测试全过(int/float/str/bool/null/list/object/enum/datetime/variable)tests/test_query_parser.py8 个 value-type 测试全过tests/test_query_executor.py13 个 executor 调用点已迁移到新execute_query(document, ...)签名🤖 Generated with Claude Code