Description
This is a little edge casey!
the tl;dr is that when using requires+interface, a field that looks like this may not receive __typename
or any of the inline spreads and just receive only the base interface object.
Consider an interface Foo
, which has implementers Bar
and Baz
:
@strawberry.field(
directives=[
Requires(
fields="child { __typename foo ... on Bar { bar } ... on Baz { baz } }"
)
]
)
def qux(self) -> str:
# Developer wants to read `self.child`` here to discern if it's a Bar or Baz
# But this only prints `Foo(foo='foo')`
print(self.child)
return "..."
Full repro
I have this as a self-contained runnable repro that you can download and run here:
I'll write it out here too for posterity:
Consider two subgraphs, foo
and bar
:
foo
import typing
import strawberry
from strawberry.federation.schema_directives import Shareable
@strawberry.interface
class Foo:
foo: str
@strawberry.type(directives=[Shareable()])
class Bar(Foo):
bar: str
@strawberry.type(directives=[Shareable()])
class Baz(Foo):
baz: str
@strawberry.federation.type(keys=["id"])
class HelloWorld:
id: strawberry.ID
child: Foo
@strawberry.type
class Query:
@strawberry.field
def helloWorld(self, bar_or_baz: str) -> HelloWorld:
if bar_or_baz == "bar":
child = (Bar(foo="foo", bar="bar!"),)
elif bar_or_baz == "baz":
child = Baz(foo="foo", baz="baz!")
else:
raise ValueError("with_foo_or_bar may only be foo or bar.")
return HelloWorld(id=1, child=child)
schema = strawberry.federation.Schema(
query=Query, types=[HelloWorld, Foo, Bar, Baz], enable_federation_2=True
)
bar
import typing
import strawberry
from strawberry.federation.schema_directives import Shareable, External, Requires
@strawberry.interface
class Foo:
foo: str
@strawberry.type(directives=[Shareable()])
class Bar(Foo):
bar: str
@strawberry.type(directives=[Shareable()])
class Baz(Foo):
baz: str
@strawberry.federation.type(keys=["id"])
class HelloWorld:
id: strawberry.ID
child: Foo = strawberry.field(directives=[External()])
@strawberry.field(
directives=[
Requires(
fields="child { __typename foo ... on Bar { bar } ... on Baz { baz } }"
)
]
)
def qux(self) -> str:
# Developer wants to read `self.child`` here to discern if it's a Bar or Baz
print(self.child)
return "hello from bar"
# ⬇️ Uncomment this to fix!
# @classmethod
# def resolve_reference(cls, id: strawberry.ID, child: Foo) -> "HelloWorld":
# return HelloWorld(id=id, child=child)
@strawberry.type
class Query:
_hi: str = strawberry.field(resolver=lambda: "Hello World!")
schema = strawberry.federation.Schema(
query=Query, types=[HelloWorld, Bar, Baz], enable_federation_2=True
)
Query:
{
helloWorld(barOrBaz: "baz") {
qux
}
}
Thoughts
Unclear if this is expected behaviour or not, but this caught out one of our devs who expected this to work as written (apparently they tried the same thing with @apollo/server and it does function as they expected)
Adding the explicit resolve reference changes .child
from the dataclass to a raw dict it seems.
What are your thoughts on how this should work? Maybe this could be clarified with docs? Thanks!