Skip to content

Commit 83d9540

Browse files
committed
Keep Federation support types out of SDL
Amp-Thread-ID: https://ampcode.com/threads/T-01a04d3e-60f6-7768-ba15-4f10d54eea17
1 parent 6e321f5 commit 83d9540

17 files changed

Lines changed: 50 additions & 57 deletions

strawberry/federation/schema.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
if TYPE_CHECKING:
3434
from graphql import ExecutionContext as GraphQLExecutionContext
35+
from graphql import GraphQLNamedType
3536

3637
from strawberry.extensions import SchemaExtension
3738
from strawberry.federation.schema_directives import ComposeDirective
@@ -44,6 +45,14 @@
4445
FederationAny = NewType("FederationAny", object)
4546
"""Represents the _Any scalar type used in federation entity resolution."""
4647

48+
_PRIVATE_FEDERATION_TYPES = frozenset(
49+
{
50+
"_FieldSet",
51+
"link__Import",
52+
"link__Purpose",
53+
}
54+
)
55+
4756

4857
class Schema(BaseSchema):
4958
def __init__( # noqa: PLR0917
@@ -370,6 +379,9 @@ def _warn_for_federation_directives(self) -> None:
370379
def _should_register_schema_directive(self, directive: object) -> bool:
371380
return True
372381

382+
def _should_include_type_in_sdl(self, graphql_type: "GraphQLNamedType") -> bool:
383+
return graphql_type.name not in _PRIVATE_FEDERATION_TYPES
384+
373385

374386
def _get_entity_type(
375387
query: type[WithStrawberryObjectDefinition] | None,

strawberry/printer/printer.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,7 @@ def print_schema(schema: BaseSchema) -> str:
615615
type_
616616
for type_name in sorted(type_map)
617617
if is_defined_type(type_ := type_map[type_name])
618+
and schema._should_include_type_in_sdl(type_)
618619
]
619620

620621
types_printed = [_print_type(type_, schema, extras=extras) for type_ in types]
@@ -649,6 +650,9 @@ def _print_extra_types() -> Iterable[str]:
649650
"GraphQLNamedType", schema.schema_converter.from_type(type_)
650651
)
651652

653+
if not schema._should_include_type_in_sdl(graphql_type):
654+
continue
655+
652656
# Skip types that are already part of the schema's type map, otherwise
653657
# they'd be printed twice (e.g. an enum used both as a regular type and
654658
# as a schema directive field), producing invalid SDL.

strawberry/schema/base.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
if TYPE_CHECKING:
1010
from collections.abc import Iterable
1111

12-
from graphql import GraphQLError
12+
from graphql import GraphQLError, GraphQLNamedType
1313

1414
from strawberry.directive import StrawberryDirective
1515
from strawberry.schema.exception_handlers import ExceptionHandler
@@ -111,6 +111,10 @@ def get_directive_by_name(self, graphql_name: str) -> StrawberryDirective | None
111111
def as_str(self) -> str:
112112
raise NotImplementedError
113113

114+
@abstractmethod
115+
def _should_include_type_in_sdl(self, graphql_type: GraphQLNamedType) -> bool:
116+
raise NotImplementedError
117+
114118
@staticmethod
115119
def remove_field_suggestion(error: GraphQLError) -> None:
116120
if (

strawberry/schema/schema.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1427,6 +1427,9 @@ def as_str(self) -> str:
14271427

14281428
__str__ = as_str
14291429

1430+
def _should_include_type_in_sdl(self, graphql_type: GraphQLNamedType) -> bool:
1431+
return True
1432+
14301433
def introspect(self) -> dict[str, Any]:
14311434
"""Return the introspection query result for the current schema.
14321435

tests/federation/printer/test_additional_directives.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ class Query:
4343
4444
union _Entity = FederatedType
4545
46-
scalar _FieldSet
47-
4846
type _Service {
4947
sdl: String!
5048
}
@@ -101,8 +99,6 @@ class Query:
10199
102100
union _Entity = FederatedType
103101
104-
scalar _FieldSet
105-
106102
type _Service {
107103
sdl: String!
108104
}

tests/federation/printer/test_compose_directive.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ class Query:
5555
5656
union _Entity = FederatedType
5757
58-
scalar _FieldSet
59-
6058
type _Service {
6159
sdl: String!
6260
}
@@ -121,8 +119,6 @@ class Query:
121119
122120
union _Entity = FederatedType
123121
124-
scalar _FieldSet
125-
126122
type _Service {
127123
sdl: String!
128124
}

tests/federation/printer/test_entities.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,6 @@ def top_products(self, first: int) -> list[Product]: # pragma: no cover
124124
125125
union _Entity = Product
126126
127-
scalar _FieldSet
128-
129127
type _Service {
130128
sdl: String!
131129
}

tests/federation/printer/test_inaccessible.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@ def top_products(
8181
8282
union _Entity = Product
8383
84-
scalar _FieldSet
85-
8684
type _Service {
8785
sdl: String!
8886
}

tests/federation/printer/test_interface.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ def top_products(self, first: int) -> list[Product]: # pragma: no cover
4444
4545
union _Entity = Product
4646
47-
scalar _FieldSet
48-
4947
type _Service {
5048
sdl: String!
5149
}

tests/federation/printer/test_interface_object.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ class SomeInterface:
2828
2929
union _Entity = SomeInterface
3030
31-
scalar _FieldSet
32-
3331
type _Service {
3432
sdl: String!
3533
}

0 commit comments

Comments
 (0)