|
1 | 1 | import ast |
2 | 2 |
|
| 3 | +from graphql import ( |
| 4 | + FieldNode, |
| 5 | + FragmentDefinitionNode, |
| 6 | + FragmentSpreadNode, |
| 7 | + InlineFragmentNode, |
| 8 | + OperationDefinitionNode, |
| 9 | + SelectionNode, |
| 10 | + SelectionSetNode, |
| 11 | +) |
| 12 | + |
| 13 | + |
| 14 | +def strip_typename_from_selection_set(selection_set: SelectionSetNode | None) -> SelectionSetNode | None: |
| 15 | + """Recursively strip __typename fields from a SelectionSetNode. |
| 16 | +
|
| 17 | + The __typename meta-field is an introspection field that is not part of the schema's |
| 18 | + type definitions. When code generation tools like ariadne-codegen try to look up |
| 19 | + __typename in the schema, they fail because it's a reserved introspection field. |
| 20 | +
|
| 21 | + This function removes all __typename fields from the selection set, allowing |
| 22 | + code generation to proceed without errors. |
| 23 | + """ |
| 24 | + if selection_set is None: |
| 25 | + return None |
| 26 | + |
| 27 | + new_selections: list[SelectionNode] = [] |
| 28 | + for selection in selection_set.selections: |
| 29 | + if isinstance(selection, FieldNode): |
| 30 | + # Skip __typename fields |
| 31 | + if selection.name.value == "__typename": |
| 32 | + continue |
| 33 | + # Recursively process nested selection sets |
| 34 | + new_field = FieldNode( |
| 35 | + alias=selection.alias, |
| 36 | + name=selection.name, |
| 37 | + arguments=selection.arguments, |
| 38 | + directives=selection.directives, |
| 39 | + selection_set=strip_typename_from_selection_set(selection.selection_set), |
| 40 | + ) |
| 41 | + new_selections.append(new_field) |
| 42 | + elif isinstance(selection, InlineFragmentNode): |
| 43 | + # Process inline fragments |
| 44 | + new_inline = InlineFragmentNode( |
| 45 | + type_condition=selection.type_condition, |
| 46 | + directives=selection.directives, |
| 47 | + selection_set=strip_typename_from_selection_set(selection.selection_set), |
| 48 | + ) |
| 49 | + new_selections.append(new_inline) |
| 50 | + elif isinstance(selection, FragmentSpreadNode): |
| 51 | + # FragmentSpread references a named fragment - keep as-is |
| 52 | + new_selections.append(selection) |
| 53 | + else: |
| 54 | + raise TypeError(f"Unexpected GraphQL selection node type '{type(selection).__name__}'.") |
| 55 | + |
| 56 | + return SelectionSetNode(selections=tuple(new_selections)) |
| 57 | + |
| 58 | + |
| 59 | +def strip_typename_from_operation(operation: OperationDefinitionNode) -> OperationDefinitionNode: |
| 60 | + """Strip __typename fields from an operation definition. |
| 61 | +
|
| 62 | + Returns a new OperationDefinitionNode with all __typename fields removed |
| 63 | + from its selection set and any nested selection sets. |
| 64 | + """ |
| 65 | + return OperationDefinitionNode( |
| 66 | + operation=operation.operation, |
| 67 | + name=operation.name, |
| 68 | + variable_definitions=operation.variable_definitions, |
| 69 | + directives=operation.directives, |
| 70 | + selection_set=strip_typename_from_selection_set(operation.selection_set), |
| 71 | + ) |
| 72 | + |
| 73 | + |
| 74 | +def strip_typename_from_fragment(fragment: FragmentDefinitionNode) -> FragmentDefinitionNode: |
| 75 | + """Strip __typename fields from a fragment definition. |
| 76 | +
|
| 77 | + Returns a new FragmentDefinitionNode with all __typename fields removed |
| 78 | + from its selection set and any nested selection sets. |
| 79 | + """ |
| 80 | + return FragmentDefinitionNode( |
| 81 | + name=fragment.name, |
| 82 | + type_condition=fragment.type_condition, |
| 83 | + variable_definitions=fragment.variable_definitions, |
| 84 | + directives=fragment.directives, |
| 85 | + selection_set=strip_typename_from_selection_set(fragment.selection_set), |
| 86 | + ) |
| 87 | + |
3 | 88 |
|
4 | 89 | def get_class_def_index(module: ast.Module) -> int: |
5 | 90 | """Get the index of the first class definition in the module. |
|
0 commit comments