Skip to content

Instantly share code, notes, and snippets.

@nietzscheson
Created March 19, 2023 21:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nietzscheson/e2c3bf5b48a33f2da0adad69508562c2 to your computer and use it in GitHub Desktop.
Save nietzscheson/e2c3bf5b48a33f2da0adad69508562c2 to your computer and use it in GitHub Desktop.
@strawberry.federation.type(keys=["id"])
class UserType:
id: strawberry.ID = strawberry.federation.field
@classmethod
def resolve_reference(cls, id: strawberry.ID):
return UserType(id)
@strawberry.federation.type(keys=["id"], description="User Type definition")
class ProductType:
id: strawberry.ID
name: str
@strawberry.field
def created_by(self) -> UserType:
return UserType(id=self.created_by)
@strawberry.type
class Mutation:
@strawberry.mutation
def product_create(self, name: str, created_by: int) -> ProductType:
product = Product(name=name, created_by: int)
db.session.add(product)
db.session.commit()
return product
schema = strawberry.federation.Schema(query=Query, types=[UserType, ProductType], mutation=Mutation, enable_federation_2=True)
### User microservice
@strawberry.federation.type(keys=["id"])
class UserType:
id: strawberry.ID
name: str
@classmethod
def resolve_reference(cls, id: strawberry.ID) -> "UserType":
logger.error(id)
user = db.session.query(User).get(id)
return UserType(id=user.id, name=user.name)
@strawberry.type
class Mutation:
@strawberry.mutation
def user_create(self, info: strawberry.types.Info, name: str) -> UserType:
user = User(name=name)
db.session.add(user)
db.session.commit()
return user
schema = strawberry.federation.Schema(query=Query, types=[UserType], mutation=Mutation, enable_federation_2=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment