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_interface_with_multiple_keys(): type_defs = """ type Query interface Product @key(fields: "upc sku") { upc: String! sku: String! name: String price: Int } """ product = FederatedInterfaceType("Product") schema = make_federated_schema(type_defs, product) assert sic(print_interface(schema.get_type("Product"))) == sic(""" interface Product { upc: String! sku: String! name: String price: Int } """) assert schema.get_type("_Entity") is None
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_federated_schema_query_service_provide_federation_directives(): type_defs = """ type Query { rootField: String } type Review @key(fields: "id") { id: ID! body: String author: User @provides(fields: "email") 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] } """ schema = make_federated_schema(type_defs) result = graphql_sync( schema, """ query GetServiceDetails { _service { sdl } } """, ) assert result.errors is None assert sic(result.data["_service"]["sdl"]) == sic(""" type Query { rootField: String } type Review @key(fields: "id") { id: ID! body: String author: User @provides(fields: "email") 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] } """)
def test_includes_directive(): type_defs = """ directive @custom on INPUT_OBJECT type Query { rootField: String } input Input @custom { id: ID! } type Review { id: ID! body: String author: User product: Product } type User @key(fields: "email") @extends { email: String @external } type Product @key(fields: "upc") @extends { upc: ID! @external name: String } """ schema = make_federated_schema(type_defs) assert not includes_directive(schema.get_type("Input"), "custom") assert not includes_directive(schema.get_type("Review"), "key") assert includes_directive(schema.get_type("User"), "key") assert includes_directive(schema.get_type("Product"), "key")
def test_get_entity_types_with_key_directive(): type_defs = """ type Query { rootField: String } type Review { id: ID! body: String author: User product: Product } type User @key(fields: "email") @extends { email: String @external } type Product @key(fields: "upc") @extends { upc: ID! @external name: String } """ schema = make_federated_schema(type_defs) entity_types_with_key_directive = get_entity_types(schema) assert len(entity_types_with_key_directive) == 2 assert schema.get_type("User") in entity_types_with_key_directive assert schema.get_type("Product") in entity_types_with_key_directive
def test_federated_schema_raises_error_on_missing_type(): type_defs = """ type Query { rootField: String } type Product @key(fields: "upc") { upc: String! @external name: String } """ schema = make_federated_schema(type_defs) result = graphql_sync( schema, """ query GetEntities($representations: [_Any!]!) { _entities(representations: $representations) { ... on Product { upc } } } """, variable_values={ "representations": [{"__typename": "ProductWrongSpelling", "id": 1,},], }, ) assert result.errors is not None
def test_federated_schema_execute_default_reference_resolver(): type_defs = """ type Query { rootField: String } type Product @key(fields: "upc") { upc: Int name: String } """ schema = make_federated_schema(type_defs) result = graphql_sync( schema, """ query GetEntities($representations: [_Any!]!) { _entities(representations: $representations) { ... on Product { upc name } } } """, variable_values={ "representations": [ {"__typename": "Product", "upc": 1, "name": "Apollo Gateway",}, ], }, ) assert result.errors is None assert result.data["_entities"][0]["name"] == "Apollo Gateway"
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 generate_schema(): type_defs = load_typedef_from_schema() query = bind_query_type_to_resolvers() mutation = bind_mutation_type_to_resolvers() schema = make_federated_schema(type_defs, [query, mutation, patient_federated_object]) return schema
def schema(): return make_federated_schema(""" type Query { hello: String } scalar Date type Product @key(fields: "upc") { upc: Int name: String } """)
def test_federated_schema_augment_root_query_with_no_keys(): type_defs = """ type Query { rootField: String } """ schema = make_federated_schema(type_defs) assert sic(print_object(schema.get_type("Query"))) == sic(""" type Query { rootField: String _service: _Service! } """)
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 } """)
def test_federated_schema_augment_root_query_with_interface_key(): type_defs = """ type Query { rootField: String } interface Product @key(fields: "upc") { upc: ID! } """ schema = make_federated_schema(type_defs) assert sic(print_object(schema.get_type("Query"))) == sic(""" type Query { rootField: String _service: _Service! } """)
def test_federated_schema_query_service_with_multiple_keys(): type_defs = """ type Query { rootField: String } type Product @key(fields: "upc sku") { upc: String! sku: String! name: String price: Int } """ schema = make_federated_schema(type_defs) result = graphql_sync( schema, """ query GetServiceDetails { _service { sdl } } """, ) assert result.errors is None assert sic(result.data["_service"]["sdl"]) == sic( """ type Query { rootField: String } type Product @key(fields: "upc sku") { upc: String! sku: String! name: String price: Int } """ )
def test_gather_directives(): type_defs = """ type Query { rootField: String } type Product @key(fields: "upc") { upc: String! @external name: String } extend type Product @extends """ schema = make_federated_schema(type_defs) product = schema.get_type("Product") directives = gather_directives(product) assert len(directives) == 2 assert {d.name.value for d in directives} == {"key", "extends"}
def test_federated_schema_augment_root_query_with_type_key(): type_defs = """ type Query { rootField: String } type Product @key(fields: "upc") { upc: ID! } """ schema = make_federated_schema(type_defs) assert sic(print_object(schema.get_type("Query"))) == sic(""" type Query { rootField: String _service: _Service! _entities(representations: [_Any!]!): [_Entity]! } """)
def schema(): return make_federated_schema( """ type Query { hello: String } scalar Date interface Product @key(fields: "upc") { upc: Int name: String } type Wine implements Product @key(fields: "upc") { upc: Int name: String content: Float } """ )
def test_federated_schema_query_service_ignore_custom_directives(): type_defs = """ directive @custom on FIELD type Query { rootField: String } type User @key(fields: "email") @extends { email: String @external } """ schema = make_federated_schema(type_defs) result = graphql_sync( schema, """ query GetServiceDetails { _service { sdl } } """, ) assert result.errors is None assert sic(result.data["_service"]["sdl"]) == sic( """ type Query { rootField: String } type User @key(fields: "email") @extends { email: String @external } """ )
def test_federated_schema_without_query_is_valid(): type_defs = """ type Product @key(fields: "upc") { upc: String! name: String price: Int weight: Int } """ schema = make_federated_schema(type_defs) result = graphql_sync( schema, """ query GetServiceDetails { _service { sdl } } """, ) assert result.errors is None assert sic(result.data["_service"]["sdl"]) == sic(type_defs)
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 import QueryType, gql, make_executable_schema from ariadne.asgi import GraphQL from ariadne.contrib.federation import make_federated_schema type_defs = gql(""" type Query { hello2: String! } """) query = QueryType() @query.field("hello2") def resolve_hello2(_, info): return "Hello2 fabien !" schema = make_federated_schema(type_defs, query) app = GraphQL(schema)
#!/usr/bin/env python3 from ariadne import QueryType, graphql_sync, load_schema_from_path from ariadne.constants import PLAYGROUND_HTML from ariadne.contrib.federation import (FederatedObjectType, make_federated_schema) from flask import Flask, jsonify, request app = Flask(__name__) type_defs = load_schema_from_path("schema.graphql") query = QueryType() product = FederatedObjectType("Product") schema = make_federated_schema(type_defs, [query, product]) @app.route("/", methods=["GET"]) def graphql_playground(): return PLAYGROUND_HTML, 200 @app.route("/", methods=["POST"]) def graphql_server(): data = request.get_json() success, result = graphql_sync(schema, data, context_value=request, debug=app.debug) status_code = 200 if success else 400 return jsonify(result), status_code @query.field("topProducts")
@review.field("product") def resolve_review_product(review, *_): return {"__typename": "Product", "upc": review["product"]["upc"]} @user.field("reviews") def resolve_user_reviews(representation, *_): return get_user_reviews(representation["email"]) @product.field("reviews") def resolve_product_reviews(representation, *_): return get_product_reviews(representation["upc"]) schema = make_federated_schema(type_defs, [query, user, review, product]) application = GraphQL(schema) reviews = [ { "id": "1", "user": { "email": "*****@*****.**" }, "product": { "upc": "1" }, "body": "Love it!", }, { "id": "2",
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)
query = QueryType() user = FederatedObjectType("User") @query.field("me") def resolve_me(_, info): return users[0] @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) users = [ { "id": 1, "name": "Ada Lovelace", "email": "*****@*****.**" }, { "id": 2, "name": "Alan Turing", "email": "*****@*****.**" }, ]
import uvicorn from ariadne.asgi import GraphQL from ariadne.contrib.federation import make_federated_schema from user.schema import type_defs, query, mutation, user schema = make_federated_schema(type_defs, query, mutation, user) app = GraphQL(schema, debug=True) if __name__ == "__main__": uvicorn.run(app, port=5001)
from ariadne import load_schema_from_path from ariadne.contrib.django.views import GraphQLView from ariadne.contrib.federation import make_federated_schema from parking_permits import admin_resolvers, resolvers from parking_permits.error_formatter import error_formatter from project.settings import BASE_DIR type_defs = load_schema_from_path(BASE_DIR / "parking_permits" / "schema" / "parking_permit.graphql") schema = make_federated_schema(type_defs, resolvers.schema_bindables) view = GraphQLView.as_view(schema=schema, error_formatter=error_formatter) admin_type_defs = load_schema_from_path( BASE_DIR / "parking_permits" / "schema" / "parking_permit_admin.graphql") schema = make_federated_schema(admin_type_defs, admin_resolvers.schema_bindables) admin_view = GraphQLView.as_view(schema=schema, error_formatter=error_formatter)
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)