Example #1
0
def test_reference_resolver_can_be_set_on_both_interface_and_type(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.reference_resolver()(lambda *_: {"name": "Pinot"})
    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": "Pinot"}]
Example #2
0
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"}]