mirumee/ariadne

SchemaDirectiveVisitor not called when extending objects.

Open

#344 opened on Mar 28, 2020

 (5 comments) (0 reactions) (0 assignees)Python (194 forks)batch import
bughelp wantedto do

Repository metrics

Stars
 (2,342 stars)
PR merge metrics
 (Avg merge 12d) (3 merged PRs in 30d)

Description

It seems like custom directives do not work on extend type Object, this might be by design but I could not find anywhere in the Ariadne docs or GraphQL specs something that confirms this.

Here is a simple test case:

from ariadne import QueryType, gql, make_executable_schema, SchemaDirectiveVisitor
from ariadne.asgi import GraphQL

class TestDirective(SchemaDirectiveVisitor):
    def visit_object(self, object_):
        print(f"Visiting object {object_}")
        return object_

type_defs = gql("""
    directive @test on OBJECT

    type Query {
        foo: String!
    }

    extend type Query @test {
        bar: String!
    }
""")

query = QueryType()
schema = make_executable_schema(type_defs, query, directives={"test": TestDirective})
app = GraphQL(schema, debug=True)

Testing using uvicorn --debug test:app.

I expect Visiting object Query to be printed but it looks like the SchemaDirectiveVisitor is not called when extending objects. (But it works as expected when doing type Query @test.)

If this is not a bug, it would make sense to at least output a warning or error that using directives on extended objects is not supported.

Looking at the code (which I do not know well), I suspect that build_and_extend_schema(ast_document) is called before SchemaDirectiveVisitor.visit_schema_directives(schema, directives) and that it somehow loses the directives in the process of extending?

To be honest I expected the directives to be applied before the schema is extended, but that's just my gut feeling.

Contributor guide