-
Hello, I have a question about this https://strawberry.rocks/docs/guides/federation: The federation2, as used in the documentation, is possible to request the books and add the reviews into the response, as resolvers. My app books: from typing import List
import strawberry
from strawberry.federation.schema_directives import Requires, External
# @strawberry.federation.type(keys=["id"])
# @strawberry.federation.interface(keys=["id"], directives=[External()])
@strawberry.federation.interface(keys=["id"],)
class Book:
id: strawberry.ID
title: str
@classmethod
def resolve_reference(cls, id: strawberry.ID, title: str) -> 'Book':
# here we could fetch the book from the database
# or even from an API
return Book(id=id, title=title)
def get_all_books() -> List[Book]:
return Book(id=strawberry.ID("1"), title="The Dark Tower")
@strawberry.type
class Query:
all_books: List[Book] = strawberry.field(resolver=get_all_books)
@strawberry.field
def book(self) -> Book:
return Book(id=strawberry.ID("1"), title='A title')
schema = strawberry.federation.Schema(
query=Query,
types=[Book],
enable_federation_2=True) My app reviews: from typing import List
import strawberry
from strawberry.federation.schema_directives import Requires, External
@strawberry.type
class Review:
id: int
body: str
book: 'Book'
def get_reviews(root: "Book") -> List[Review]:
return [
Review(id=id_, body=f"A review for {root.id}")
for id_ in range(root.reviews_count)
]
# @strawberry.federation.type(keys=["id"])
# @strawberry.federation.interface_object(keys=["id"], directives=[External()])
@strawberry.federation.interface_object(keys=["id"])
class Book:
id: strawberry.ID
reviews_count: int
reviews: List[Review] = strawberry.field(resolver=get_reviews)
@classmethod
def resolve_reference(cls, id: strawberry.ID, reviews_count: int) -> 'Book':
# here we could fetch the book from the database
# or even from an API
return Book(id=strawberry.ID("1"), reviews_count=3)
def get_all_reviews() -> List[Review]:
return [Review(
id=strawberry.ID("1"),
body="Blablabla",
book=Book(id=strawberry.ID("1"), reviews_count=3),
)]
@strawberry.type
class Query:
_hi: str = strawberry.field(resolver=lambda: "Hello World!")
all_reviews: List[Review] = strawberry.field(resolver=get_all_reviews)
schema = strawberry.federation.Schema(
query=Query, types=[Book, Review], enable_federation_2=True
) The federation is made with Apollo. Thank you :) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
@owl2 could you provide a repo for this? 😊 |
Beta Was this translation helpful? Give feedback.
Hi @owl2 thanks for the reproduction! I've made an example based on your repo that adds a
Reviewable
interface, which is used by the book and extended in the reviews service 😊Here's the PR for it: owl2/gql-strawberry-interface-test#1