def test_reference_resolver_can_be_set_using_setter(schema): def resolve_result_type(*_): # pylint: disable=unused-variable return "Wine" interface = FederatedInterfaceType("Product") interface.set_type_resolver(resolve_result_type) interface.reference_resolver(lambda *_: {"name": "Malbec"}) interface.bind_to_schema(schema) obj = FederatedObjectType("Wine") obj.bind_to_schema(schema) result = graphql_sync( schema, """ query GetEntities($representations: [_Any!]!) { _entities(representations: $representations) { ... on Wine { name } } } """, variable_values={"representations": [{"__typename": "Wine", "upc": 1}]}, ) assert result.errors is None assert result.data["_entities"] == [{"name": "Malbec"}]
def test_federated_schema_not_mark_type_with_no_keys(): type_defs = """ type Query type Product { upc: String! name: String price: Int } """ product = FederatedObjectType("Product") schema = make_federated_schema(type_defs, product) assert sic(print_object(schema.get_type("Product"))) == sic( """ type Product { upc: String! name: String price: Int } """ ) assert schema.get_type("_Entity") is None
def test_federated_schema_mark_type_with_key_split_type_defs(): query_type_defs = """ type Query """ product_type_defs = """ type Product @key(fields: "upc") { upc: String! name: String price: Int } """ product = FederatedObjectType("Product") schema = make_federated_schema( [query_type_defs, product_type_defs], product, ) assert sic(print_object(schema.get_type("Product"))) == sic(""" type Product { upc: String! name: String price: Int } """) assert sic(print_union(schema.get_type("_Entity"))) == sic(""" union _Entity = Product """)
def test_federated_schema_mark_type_with_multiple_keys(): type_defs = """ type Query type Product @key(fields: "upc sku") { upc: String! sku: String! name: String price: Int } """ product = FederatedObjectType("Product") schema = make_federated_schema(type_defs, product) assert sic(print_object(schema.get_type("Product"))) == sic(""" type Product { upc: String! sku: String! name: String price: Int } """) assert sic(print_union(schema.get_type("_Entity"))) == sic(""" union _Entity = Product """)
def test_reference_resolver_can_be_set_using_setter(schema): obj = FederatedObjectType("Product") obj.reference_resolver(lambda *_: {"name": "Malbec"}) obj.bind_to_schema(schema) result = graphql_sync( schema, """ query GetEntities($representations: [_Any!]!) { _entities(representations: $representations) { ... on Product { name } } } """, variable_values={ "representations": [{ "__typename": "Product", "upc": 1 }] }, ) assert result.errors is None assert result.data["_entities"] == [{"name": "Malbec"}]
def test_federated_schema_execute_reference_resolver_that_returns_none(): type_defs = """ type Query { rootField: String } type Product @key(fields: "upc") { upc: Int name: String } """ product = FederatedObjectType("Product") @product.reference_resolver() def product_reference_resolver(_obj, _info, reference): assert reference["upc"] == 1 # return None schema = make_federated_schema(type_defs, product) result = graphql_sync( schema, """ query GetEntities($representations: [_Any!]!) { _entities(representations: $representations) { ... on Product { __typename name } } } """, variable_values={ "representations": [ { "__typename": "Product", "upc": 1, }, ], }, ) assert result.errors is None assert result.data["_entities"][0] is None
def test_federated_schema_type_with_multiple_keys(): type_defs = """ type Query type Product @key(fields: "upc") @key(fields: "sku") { upc: String! sku: String! price: String } """ product = FederatedObjectType("Product") schema = make_federated_schema(type_defs) assert sic(print_object(schema.get_type("Product"))) == sic(""" type Product { upc: String! sku: String! price: String } """)
async def test_federated_schema_execute_async_reference_resolver(): type_defs = """ type Query { rootField: String } type Product @key(fields: "upc") { upc: Int name: String } type User @key(fields: "id") { firstName: String } """ product = FederatedObjectType("Product") @product.reference_resolver() async def product_reference_resolver(_obj, _info, reference): assert reference["upc"] == 1 return {"name": "Apollo Gateway"} user = FederatedObjectType("User") @user.reference_resolver() async def user_reference_resolver(_obj, _info, reference): assert reference["id"] == 1 return Mock(firstName="James") schema = make_federated_schema(type_defs, [product, user]) result = await graphql( schema, """ query GetEntities($representations: [_Any!]!) { _entities(representations: $representations) { ... on Product { __typename name } ... on User { __typename firstName } } } """, variable_values={ "representations": [ { "__typename": "Product", "upc": 1, }, { "__typename": "User", "id": 1, }, ], }, ) assert result.errors is None assert result.data["_entities"][0]["__typename"] == "Product" assert result.data["_entities"][0]["name"] == "Apollo Gateway" assert result.data["_entities"][1]["__typename"] == "User" assert result.data["_entities"][1]["firstName"] == "James"
from ariadne.contrib.federation import FederatedObjectType from doctors.models import Doctor doctor_federated_object = FederatedObjectType("Doctor") @doctor_federated_object.reference_resolver def get_doctor_by_uid(_, _info, representation): return Doctor.objects.get(uid=representation.get("uid")) @doctor_federated_object.field("uid") def resolve_id(obj, *_): return obj.uid @doctor_federated_object.field("indexingId") def resolve_indexing_id(obj, *_): return obj.indexing_id @doctor_federated_object.field("first_name") def resolve_first_name(obj, *_): return obj.first_name @doctor_federated_object.field("last_name") def resolve_last_name(obj, *_): return obj.last_name
import logging from ariadne.contrib.federation import FederatedObjectType from diagnosis.models import Diagnosis patient_federated_type = FederatedObjectType("Patient") class Patient: def __init__(self, uid): self.uid = uid @patient_federated_type.reference_resolver def get_patient_by_uid(_, _info, representation): try: uid = representation.get("uid") return Patient(uid) except Exception as error: logging.error(error) return None @patient_federated_type.field("uid") def resolve_uid(obj, *_): try: if isinstance(obj, dict): return obj.get("uid") if isinstance(obj, Patient): return obj.uid except Exception as error:
PermitEmailType, RefundEmailType, send_permit_email, send_refund_email, ) from .services.traficom import Traficom from .talpa.order import TalpaOrderManager logger = logging.getLogger("db") helsinki_profile_query = load_schema_from_path( BASE_DIR / "parking_permits" / "schema" / "helsinki_profile.graphql") query = QueryType() mutation = MutationType() address_node = FederatedObjectType("AddressNode") profile_node = FederatedObjectType("ProfileNode") schema_bindables = [ query, mutation, address_node, snake_case_fallback_resolvers ] ACTIVE_PERMIT_STATUSES = [ ParkingPermitStatus.DRAFT, ParkingPermitStatus.VALID, ] @query.field("getPermits") @is_authenticated @convert_kwargs_to_snake_case
from ariadne.contrib.federation import FederatedObjectType from diagnosis.models import Diagnosis diagnosis_federated_object = FederatedObjectType("Diagnosis") patient_federated_object = FederatedObjectType("Patient") doctor_federated_object = FederatedObjectType("Doctor") @diagnosis_federated_object.resolve_reference def get_diagnosis_by_uid(representation): return Diagnosis.objects.get(id=representation.get("id")) @diagnosis_federated_object.field("uid") def resolve_id(obj, *_): return obj.uid @diagnosis_federated_object.field("indexingId") def resolve_indexing_id(obj, *_): return obj.indexing_id @diagnosis_federated_object.field("diagnosis") def resolve_diagnosis(obj, *_): return obj.diagnosis @diagnosis_federated_object.field("consulting_doctor") def resolve_consulting_doctor(obj, *_):
product: Product @provides(fields: "upc") } type User @key(fields: "email") @extends { email: String! @external reviews: [Review] } type Product @key(fields: "upc") @extends { upc: String! @external reviews: [Review] } """ query = QueryType() review = FederatedObjectType("Review") user = FederatedObjectType("User") product = FederatedObjectType("Product") @review.reference_resolver def resolve_reviews_reference(_, _info, representation): return get_review_by_id(representation["id"]) @review.field("author") def resolve_review_author(review, *_): return {"__typename": "User", "email": review["user"]["email"]} @review.field("product")
from ariadne.asgi import GraphQL from ariadne import (load_schema_from_path, make_executable_schema, MutationType, QueryType) from ariadne.contrib.federation import ( FederatedObjectType, make_federated_schema) from . import resolvers # load schema file type_defs = load_schema_from_path("riderequests/schema.graphql") mutation = MutationType() query = QueryType() riderequest = FederatedObjectType("RideRequest") mutation.set_field("requestRide", resolvers.resolve_request_ride) query.set_field("allRideRequests", resolvers.resolve_all_ride_request) query.set_field("request", resolvers.resolve_ride_request) riderequest.set_field("passenger", resolvers.resolve_riderequest_passenger) riderequest.set_field("ride", resolvers.resolve_riderequest_ride) # bind schema(queries and mutation) to the respective resolvers schema = make_federated_schema(type_defs, [riderequest, query, mutation]) application = GraphQL(schema, debug=True)
from ariadne.contrib.federation import FederatedObjectType, make_federated_schema query_type = QueryType() class User: id = "45AOE5U6AU452E5A8" userId = "UHOH45" email = "*****@*****.**" firstName = "Brent" lastName = "Weeks" @query_type.field('me') def resolve_me(*_): return User() user_type = FederatedObjectType('User') @user_type.reference_resolver def resolve_reference(_, _info, representation): return User() type_defs = load_schema_from_path('schema.graphql') schema = make_federated_schema(type_defs, query_type, user_type) app = GraphQL(schema, debug=True)
from ariadne.contrib.federation import FederatedObjectType from diagnosis.models import Diagnosis diagnosis_federated_object = FederatedObjectType("Diagnosis") @diagnosis_federated_object.reference_resolver def get_diagnosis_by_uid(_, _info, representation): return Diagnosis.objects.get(uid=representation.get("uid")) @diagnosis_federated_object.field("uid") def resolve_id(obj, *_): return obj.uid @diagnosis_federated_object.field("indexingId") def resolve_indexing_id(obj, *_): return obj.indexing_id @diagnosis_federated_object.field("diagnosis") def resolve_diagnosis(obj, *_): return obj.diagnosis @diagnosis_federated_object.field("consulting_doctor") def resolve_consulting_doctor(obj, *_): return {"uid": obj.consulting_doctor}
from ariadne.contrib.federation import FederatedObjectType from patient.models import Patient patient_federated_object = FederatedObjectType("Patient") @patient_federated_object.reference_resolver def get_patient_by_uid(_, _info, representation): return Patient.objects.get(uid=representation.get("uid")) @patient_federated_object.field("uid") def resolve_id(obj, *_): return obj.uid @patient_federated_object.field("indexingId") def resolve_indexing_id(obj, *_): return obj.indexing_id @patient_federated_object.field("first_name") def resolve_first_name(obj, *_): return obj.first_name @patient_federated_object.field("last_name") def resolve_last_name(obj, *_): return obj.last_name
from ariadne.contrib.federation import FederatedObjectType from user.models.user import User user = FederatedObjectType("User") @user.reference_resolver def resolve_user_reference(_, _info, representation): print(representation) return User()
import logging from ariadne.contrib.federation import FederatedObjectType from diagnosis.models import Diagnosis doctor_federated_type = FederatedObjectType("Doctor") class Doctor: def __init__(self, uid): self.uid = uid @doctor_federated_type.reference_resolver def get_doctor_by_uid(_, _info, representation): try: uid = representation.get("uid") return Doctor(uid) except Exception as error: logging.error(error) return None @doctor_federated_type.field("uid") def resolve_uid(obj, *_): try: if isinstance(obj, dict): return obj.get("uid") if isinstance(obj, Doctor): return obj.uid except Exception as error:
from ariadne import QueryType from ariadne.asgi import GraphQL from ariadne.contrib.federation import FederatedObjectType, make_federated_schema type_defs = """ type Query { me: Book } type Book @key(fields: "title") { id: ID! title: String author: String! } """ query = QueryType() user = FederatedObjectType("Book") # @user.reference_resolver # def resolve_user_reference(_, _info, representation): # return get_user_by_email(representation.get("email")) schema = make_federated_schema(type_defs, [query, user]) application = GraphQL(schema)
def test_bind_federated_object_type_to_undefined_type_raises_error(schema): obj = FederatedObjectType("Test") with pytest.raises(ValueError): obj.bind_to_schema(schema)
def test_bind_federated_object_type_to_invalid_type_raises_error(schema): obj = FederatedObjectType("Date") with pytest.raises(ValueError): obj.bind_to_schema(schema)
from ariadne.contrib.federation import FederatedObjectType from product.models.product import Product product = FederatedObjectType("Product") @product.reference_resolver def resolve_product_reference(*_, representation): print(representation) return Product(upc="id", name="product", price=10, weight=12)
from random import choice from string import ascii_letters from ariadne.contrib.federation import FederatedObjectType from review.models.review import Review review = FederatedObjectType("Review") @review.reference_resolver def resolve_reviews_reference(_, _info, representation): print(representation) return Review(id=representation["id"], body=choice(ascii_letters))