Python graphql.graphql_sync() Examples

The following are 30 code examples of graphql.graphql_sync(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module graphql , or try the search function .
Example #1
Source File: test_queries.py    From ariadne with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_default_resolver_resolves_value_from_object_attr():
    type_defs = """
        type Query {
            test: Custom
        }

        type Custom {
            node: String
        }
    """

    query = QueryType()
    query.set_field("test", lambda *_: Mock(node="custom"))

    schema = make_executable_schema(type_defs, query)

    result = graphql_sync(schema, "{ test { node } }")
    assert result.errors is None
    assert result.data == {"test": {"node": "custom"}} 
Example #2
Source File: test_objects.py    From ariadne with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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"}] 
Example #3
Source File: test_modularization.py    From ariadne with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_multiple_bindables_can_be_passed_as_separate_args():
    type_defs = """
        type Query {
            user: User
        }

        type User {
            username: String
        }
    """

    query = QueryType()
    query.set_field("user", lambda *_: Mock(first_name="Joe"))

    user = ObjectType("User")
    user.set_alias("username", "first_name")

    schema = make_executable_schema(type_defs, query, user)

    result = graphql_sync(schema, "{ user { username } }")
    assert result.errors is None
    assert result.data == {"user": {"username": "Joe"}} 
Example #4
Source File: test_objects.py    From ariadne with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_reference_resolver_can_be_set_using_decorator(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"}] 
Example #5
Source File: test_modularization.py    From ariadne with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_different_types_resolver_maps_are_merged_into_executable_schema():
    type_defs = """
        type Query {
            user: User
        }

        type User {
            username: String
        }
    """

    query = QueryType()
    query.set_field("user", lambda *_: Mock(first_name="Joe"))

    user = ObjectType("User")
    user.set_alias("username", "first_name")

    schema = make_executable_schema(type_defs, [query, user])

    result = graphql_sync(schema, "{ user { username } }")
    assert result.errors is None
    assert result.data == {"user": {"username": "Joe"}} 
Example #6
Source File: test_modularization.py    From ariadne with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_same_type_resolver_maps_are_merged_into_executable_schema():
    type_defs = """
        type Query {
            hello: String
            test(data: Int): Boolean
        }
    """

    query = QueryType()
    query.set_field("hello", lambda *_: "World!")

    extending_query = QueryType()

    @extending_query.field("test")
    def resolve_test(*_, data):  # pylint: disable=unused-variable
        assert data == 4
        return True

    schema = make_executable_schema(type_defs, [query, extending_query])

    result = graphql_sync(schema, "{ hello test(data: 4) }")
    assert result.errors is None
    assert result.data == {"hello": "World!", "test": True} 
Example #7
Source File: test_directives.py    From ariadne with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_multiple_field_definition_directives_replace_field_resolver_with_chainable_resolvers():
    type_defs = """
        directive @upper on FIELD_DEFINITION
        directive @reverse on FIELD_DEFINITION

        type Query {
            hello: String @upper @reverse
        }
    """

    query = QueryType()
    query.set_field("hello", lambda *_: "hello world")

    schema = make_executable_schema(
        type_defs,
        [query],
        directives={"upper": UpperDirective, "reverse": ReverseDirective},
    )

    result = graphql_sync(schema, "{ hello }")
    assert result.errors is None
    assert result.data == {"hello": "DLROW OLLEH"} 
Example #8
Source File: test_directives.py    From ariadne with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_directive_can_have_optional_argument():
    type_defs = """
        directive @test(arg: String) on FIELD_DEFINITION

        type Query {
            hello: String @test
        }
    """

    query = QueryType()
    query.set_field("hello", lambda *_: "hello world")

    schema = make_executable_schema(
        type_defs, [query], directives={"test": ReturnValueDirective}
    )

    result = graphql_sync(schema, "{ hello }")
    assert result.errors is None
    assert result.data == {"hello": None} 
Example #9
Source File: test_directives.py    From ariadne with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_directive_can_have_required_argument():
    type_defs = """
        directive @test(arg: String) on FIELD_DEFINITION

        type Query {
            hello: String @test(arg: "OK!")
        }
    """

    query = QueryType()
    query.set_field("hello", lambda *_: "hello world")

    schema = make_executable_schema(
        type_defs, [query], directives={"test": ReturnValueDirective}
    )

    result = graphql_sync(schema, "{ hello }")
    assert result.errors is None
    assert result.data == {"hello": "OK!"} 
Example #10
Source File: test_queries.py    From ariadne with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_default_resolver_resolves_value_from_dict_item():
    type_defs = """
        type Query {
            test: Custom
        }

        type Custom {
            node: String
        }
    """

    query = QueryType()
    query.set_field("test", lambda *_: {"node": "custom"})

    schema = make_executable_schema(type_defs, query)

    result = graphql_sync(schema, "{ test { node } }")
    assert result.errors is None
    assert result.data == {"test": {"node": "custom"}} 
Example #11
Source File: test_queries.py    From ariadne with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_custom_and_default_resolvers_are_combined_to_resolve_custom_type_fields():
    type_defs = """
        type Query {
            test: Custom
        }

        type Custom {
            node: String
            default: String
        }
    """

    query = QueryType()
    query.set_field("test", lambda *_: {"node": "custom", "default": "ok"})

    custom = ObjectType("Custom")
    custom.set_field("node", lambda *_: "deep")

    schema = make_executable_schema(type_defs, [query, custom])

    result = graphql_sync(schema, "{ test { node default } }")
    assert result.errors is None
    assert result.data == {"test": {"node": "deep", "default": "ok"}} 
Example #12
Source File: test_queries.py    From ariadne with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_custom_resolver_is_called_with_arguments_passed_with_query():
    type_defs = """
        type Query {
            test(returnValue: Int!): Int
        }
    """

    query = QueryType()

    @query.field("test")
    def resolve_test(*_, returnValue):  # pylint: disable=unused-variable
        assert returnValue == 4
        return "42"

    schema = make_executable_schema(type_defs, query)

    result = graphql_sync(schema, "{ test(returnValue: 4) }")
    assert result.errors is None
    assert result.data == {"test": 42} 
Example #13
Source File: test_queries.py    From ariadne with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_custom_resolver_is_called_with_input_type_value_as_dict():
    type_defs = """
        type Query {
            test(data: TestInput): Int
        }

        input TestInput {
            value: Int
        }
    """

    query = QueryType()

    @query.field("test")
    def resolve_test(*_, data):  # pylint: disable=unused-variable
        assert data == {"value": 4}
        return "42"

    schema = make_executable_schema(type_defs, query)

    result = graphql_sync(schema, "{ test(data: { value: 4 }) }")
    assert result.errors is None
    assert result.data == {"test": 42} 
Example #14
Source File: test_mutations.py    From ariadne with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_executing_mutation_takes_scalar_args_and_returns_scalar_sum():
    type_defs = """
        type Query {
            _: String
        }

        type Mutation {
            sum(a: Int, b: Int): Int
        }
    """

    mutation = MutationType()
    mutation.set_field("sum", lambda *_, a, b: a + b)

    schema = make_executable_schema(type_defs, mutation)

    result = graphql_sync(schema, "mutation { sum(a: 1, b: 2) }")
    assert result.errors is None
    assert result.data == {"sum": 3} 
Example #15
Source File: test_node_custom.py    From graphene with MIT License 5 votes vote down vote up
def test_gets_the_correct_name_for_users():
    query = """
      {
        node(id: "1") {
          id
          ... on User {
            name
          }
        }
      }
    """
    expected = {"node": {"id": "1", "name": "John Doe"}}
    result = graphql_sync(graphql_schema, query)
    assert not result.errors
    assert result.data == expected 
Example #16
Source File: test_unions.py    From ariadne with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_union_type_resolver_may_be_set_using_decorator(query_with_user_item):
    union = UnionType("FeedItem")

    @union.type_resolver
    def resolve_result_type(*_):  # pylint: disable=unused-variable
        return "User"

    schema = make_executable_schema(type_defs, [query_with_user_item, union])
    result = graphql_sync(schema, "{ item { __typename } }")
    assert result.data == {"item": {"__typename": "User"}} 
Example #17
Source File: test_unions.py    From ariadne with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_union_type_resolver_may_be_set_using_setter(query_with_user_item):
    def resolve_result_type(*_):  # pylint: disable=unused-variable
        return "User"

    union = UnionType("FeedItem")
    union.set_type_resolver(resolve_result_type)

    schema = make_executable_schema(type_defs, [query_with_user_item, union])
    result = graphql_sync(schema, "{ item { __typename } }")
    assert result.data == {"item": {"__typename": "User"}} 
Example #18
Source File: test_unions.py    From ariadne with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_result_is_username_if_union_resolves_type_to_user(query_with_user_item):
    union = UnionType("FeedItem", type_resolver=resolve_result_type)
    schema = make_executable_schema(type_defs, [query_with_user_item, union])
    result = graphql_sync(schema, test_query)
    assert result.data == {"item": {"__typename": "User", "username": User.username}} 
Example #19
Source File: test_node_custom.py    From graphene with MIT License 5 votes vote down vote up
def test_gets_the_correct_id_for_users():
    query = """
      {
        node(id: "1") {
          id
        }
      }
    """
    expected = {"node": {"id": "1"}}
    result = graphql_sync(graphql_schema, query)
    assert not result.errors
    assert result.data == expected 
Example #20
Source File: test_node_custom.py    From graphene with MIT License 5 votes vote down vote up
def test_gets_the_correct_id_for_photos():
    query = """
      {
        node(id: "4") {
          id
        }
      }
    """
    expected = {"node": {"id": "4"}}
    result = graphql_sync(graphql_schema, query)
    assert not result.errors
    assert result.data == expected 
Example #21
Source File: test_schema.py    From ariadne with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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 
Example #22
Source File: test_objects.py    From ariadne with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_set_alias_method_creates_resolver_for_specified_attribute(schema):
    query = ObjectType("Query")
    query.set_alias("hello", "test")
    query.bind_to_schema(schema)

    result = graphql_sync(schema, "{ hello }", root_value={"test": "World"})
    assert result.errors is None
    assert result.data == {"hello": "World"} 
Example #23
Source File: test_objects.py    From ariadne with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_field_resolver_can_be_set_using_setter(schema):
    query = ObjectType("Query")
    query.set_field("hello", lambda *_: "World")
    query.bind_to_schema(schema)

    result = graphql_sync(schema, "{ hello }")
    assert result.errors is None
    assert result.data == {"hello": "World"} 
Example #24
Source File: test_objects.py    From ariadne with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_field_resolver_can_be_set_using_decorator(schema):
    query = ObjectType("Query")
    query.field("hello")(lambda *_: "World")
    query.bind_to_schema(schema)

    result = graphql_sync(schema, "{ hello }")
    assert result.errors is None
    assert result.data == {"hello": "World"} 
Example #25
Source File: test_error_formatting.py    From ariadne with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_default_formatter_is_not_extending_plain_graphql_error(schema):
    result = graphql_sync(schema, "{ error }")
    error = format_error(result.errors[0], debug=True)
    assert error["extensions"]["exception"] is None 
Example #26
Source File: test_error_formatting.py    From ariadne with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_default_formatter_fills_context_with_reprs_of_python_context(
    schema, erroring_resolvers, failing_repr_mock
):
    result = graphql_sync(schema, "{ hello }")
    error = format_error(result.errors[0], debug=True)
    context = error["extensions"]["exception"]["context"]

    assert context["test_int"] == repr(123)
    assert context["test_str"] == repr("test")
    assert context["test_dict"] == repr({"test": "dict"})
    assert context["test_failing_repr"] == repr(failing_repr_mock)
    assert context["test_obj"] == repr(erroring_resolvers) 
Example #27
Source File: test_error_formatting.py    From ariadne with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_default_formatter_extends_error_with_stacktrace(schema):
    result = graphql_sync(schema, "{ hello }")
    error = format_error(result.errors[0], debug=True)
    assert error["extensions"]["exception"]["stacktrace"] 
Example #28
Source File: test_error_formatting.py    From ariadne with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_default_formatter_is_not_extending_error_by_default(schema):
    result = graphql_sync(schema, "{ hello }")
    error = format_error(result.errors[0])
    assert not error.get("extensions") 
Example #29
Source File: test_fallback_resolvers.py    From ariadne with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_snake_case_fallback_is_not_replacing_already_set_resolvers(schema):
    resolvers_map = ObjectType("Query")
    resolvers_map.set_field("hello", lambda *_: False)
    resolvers_map.set_field("Camel", lambda *_: False)
    resolvers_map.bind_to_schema(schema)
    snake_case_fallback_resolvers.bind_to_schema(schema)
    query_root = {"hello": True, "snake_case": True, "camel": True, "camel_case": True}
    result = graphql_sync(schema, query, root_value=query_root)
    assert result.data == {
        "hello": False,
        "snake_case": True,
        "Camel": False,
        "camelCase": True,
    } 
Example #30
Source File: test_fallback_resolvers.py    From ariadne with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_snake_case_fallback_is_not_resolving_fields_by_exact_names(schema):
    snake_case_fallback_resolvers.bind_to_schema(schema)
    query_root = {"hello": True, "snake_case": True, "Camel": True, "camelCase": True}
    result = graphql_sync(schema, query, root_value=query_root)
    assert result.data == {
        "hello": True,
        "snake_case": True,
        "Camel": None,
        "camelCase": None,
    }