Circular dependencies seem to be a common issue with Strawberry.
I'm wondering if Strawberry would consider adding support for something like type-graphql's FieldResolver
s. That allows one to define fields for other types outside of the immediate class definition of that type.
For example, consider a schema with Person
and Pet
objects. A Person
might have a pets: [Pet!]!
field and each pet might have a owners: [Person!]!
field. This requires Person
and Pet
to "know" about each other.
With type-graphql, one can define
// person.ts
@ObjectType()
class Person {
@Field()
get name(): string {
// ...
}
// other fields
}
and
// pet.ts
import { Person } from "./person";
@ObjectType()
class Pet {
@Field()
name: string;
// other pet fields
@Field()
owners(): Person[] {
// ...
}
}
@Resolver(of => Person)
class PersonPetResolver {
@Field()
pets(
@Root() person: Person,
): Person[] {
// ...
}
}
In Strawberry land, this might look something like
# person.py
@strawberry.type
class Person:
name: str
# ...
and
# pet.py
from .person import Person
@strawberry.type
class Pet:
name: str
# ...
@strawberry.field
def owners(self) -> List[Person]:
...
# This could either be a staticmethod or just a decorator on a normal (non-method) function
@strawberry.resolver
@staticmethod
def person_pets(root: Person) -> List[Pet]:
...
Thoughts? Am I missing something?
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too