Skip to content

convert_names_case on default resolvers doesn't work well with directives #1234

Open
@zwangBLP

Description

@zwangBLP

For example in the current test

def test_field_definition_directive_replaces_field_resolver_with_custom_one():

For simplicity if I change the test case to have both convert_names_case=True and @upper it will fail

def test_field_definition_directive_replaces_field_resolver_with_custom_one():
    type_defs = """
        directive @upper on FIELD_DEFINITION

        type Query {
            test: Custom
        }

        type Custom {
            nodeField: String @upper
        }
    """

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

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

    result = graphql_sync(schema, "{ test { nodeField }}")
    assert result.errors is None
    assert result.data == {"test": {"nodeField": "CUSTOM"}}
python -m pytest tests/test_directives.py::test_field_definition_directive_replaces_field_resolver_with_custom_one
FAILED tests/test_directives.py::test_field_definition_directive_replaces_field_resolver_with_custom_one - assert [GraphQLError("'NoneType' object has no attribute 'upper'", locations=[SourceLocation(line=1, column=10)], path=['test', 'nodeField'])] is None

But if I change the test case to have only convert_names_case=True it will succeed

def test_field_definition_directive_replaces_field_resolver_with_custom_one():
    type_defs = """
        type Query {
            test: Custom
        }

        type Custom {
            nodeField: String
        }
    """

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

    schema = make_executable_schema(
        type_defs,
        [query],
        convert_names_case=True,
    )

    result = graphql_sync(schema, "{ test { nodeField }}")
    assert result.errors is None
    assert result.data == {"test": {"nodeField": "custom"}}
python -m pytest tests/test_directives.py::test_field_definition_directive_replaces_field_resolver_with_custom_one
1 passed, 2 warnings in 0.04s

It will also succeed if I explicitly provide a resolver when having both convert_names_case=True and @upper

def test_field_definition_directive_replaces_field_resolver_with_custom_one():
    type_defs = """
        directive @upper on FIELD_DEFINITION

        type Query {
            test: Custom
        }

        type Custom {
            nodeField: String @upper
        }
    """

    query = QueryType()
    test =  ObjectType('Custom')
    query.set_field("test", lambda *_: {"node_field": "custom"})
    test.set_field("nodeField", lambda obj, *_: obj["node_field"])

    schema = make_executable_schema(
        type_defs,
        [query, test],
        directives={"upper": UpperDirective},
        convert_names_case=True,
    )

    result = graphql_sync(schema, "{ test { nodeField }}")
    assert result.errors is None
    assert result.data == {"test": {"nodeField": "CUSTOM"}}
python -m pytest tests/test_directives.py::test_field_definition_directive_replaces_field_resolver_with_custom_one
1 passed, 2 warnings in 0.05s

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions