Skip to content

Instantly share code, notes, and snippets.

@salwator
Last active November 19, 2019 13:01
Show Gist options
  • Save salwator/615079c6ca0baf3dacc524c176a1f7d6 to your computer and use it in GitHub Desktop.
Save salwator/615079c6ca0baf3dacc524c176a1f7d6 to your computer and use it in GitHub Desktop.
Ariadne service implementation that extends saleor with fake reviews
from dataclasses import dataclass
from typing import List
from ariadne import ObjectType, MutationType, QueryType, snake_case_fallback_resolvers
from ariadne_extensions import federation
from ariadne.asgi import GraphQL
query = QueryType()
manager = federation.FederatedManager(query=query, schema_sdl_file="schema.graphql")
user_type = federation.FederatedObjectType("User")
review_type = ObjectType("Review")
mutation = MutationType()
@dataclass
class Review:
id: str
title: str
author: "User"
__typename: str = "Review"
@dataclass
class User:
id: str
reviews: List[dict] = list
typename: str = "User"
REVIEWS = [
Review(id="1", title="How to kill an admin?", author=User(id="VXNlcjoyMQ==")),
Review(id="2", title="Funny names", author=User(id="VXNlcjo4")),
]
@user_type.resolve_reference
def resolve_user_reference(representation):
user_id = representation.get("id")
return User(id=user_id)
@query.field("reviews")
def resolve_reviews(obj, *_):
return REVIEWS
@user_type.field("reviews")
def resolve_user_reviews(obj, *_):
return [review for review in REVIEWS if review.author.id == obj.id]
@review_type.field("author")
def resolve_author(obj, *_):
return {"id": obj.author.id}
manager.add_types(user_type, review_type, mutation)
manager.add_types(snake_case_fallback_resolvers)
schema = manager.get_schema()
app = GraphQL(schema, debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment