| 66 | |
| 67 | @dataclasses.dataclass(slots=True) |
| 68 | class Field: |
| 69 | type_name: str |
| 70 | name: str |
| 71 | args: dict[str, Any] |
| 72 | children: dict[str, "Field"] = dataclasses.field(default_factory=dict) |
| 73 | # When set, children are wrapped in an inline fragment on this type: |
| 74 | # field(args) { ... on inline_type { children } } |
| 75 | inline_type: str | None = None |
| 76 | |
| 77 | def to_dsl(self, schema: DSLSchema) -> DSLField: |
| 78 | type_: DSLType = getattr(schema, self.type_name) |
| 79 | field_ = getattr(type_, self.name)(**self.args) |
| 80 | if self.children: |
| 81 | child_fields = { |
| 82 | name: child.to_dsl(schema) for name, child in self.children.items() |
| 83 | } |
| 84 | if self.inline_type is not None: |
| 85 | frag_type: DSLType = getattr(schema, self.inline_type) |
| 86 | inline = DSLInlineFragment().on(frag_type).select(**child_fields) |
| 87 | field_ = field_.select(inline) |
| 88 | else: |
| 89 | field_ = field_.select(**child_fields) |
| 90 | return field_ |
| 91 | |
| 92 | def add_child(self, child: "Field") -> "Field": |
| 93 | return dataclasses.replace(self, children={child.name: child}) |
| 94 | |
| 95 | |
| 96 | @dataclasses.dataclass(slots=True) |