Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass dict to context argument from SchemaDirectiveVisitor.__init__ #1019

Open
rafalp opened this issue Feb 3, 2023 · 2 comments
Open

Pass dict to context argument from SchemaDirectiveVisitor.__init__ #1019

rafalp opened this issue Feb 3, 2023 · 2 comments
Labels
enhancement New feature or request to do

Comments

@rafalp
Copy link
Contributor

rafalp commented Feb 3, 2023

SchemaDirectiveVisitor accept's context as one of constructors arguments, but it's not passed to directives at any point, and doesn't even exist when directive is initiated, making this arg's value always be None.

We could initialize empty dict in visit_schema_directives and then pass it to directive instances to let them share state between fields without having to resort to type attributes which are leaky abstraction in multi-schema projects.

@rafalp rafalp added enhancement New feature or request roadmap Feature that we want to have included labels Feb 3, 2023
@yaliansymphonie
Copy link

+1 I had the None issue trying to implement an Auth Directive 😔

@rafalp
Copy link
Contributor Author

rafalp commented Jan 17, 2024

Auth directive should wrap resolver in custom one that has access to info.context, eg:

from typing import Union

from ariadne import SchemaDirectiveVisitor
from graphql import (
    GraphQLField,
    GraphQLInterfaceType,
    GraphQLObjectType,
    default_field_resolver,
)


class AuthDirective(SchemaDirectiveVisitor):
    def visit_field_definition(
        self,
        field: GraphQLField,
        object_type: Union[GraphQLObjectType, GraphQLInterfaceType],
    ) -> GraphQLField:
        resolver = field.resolve or default_field_resolver

        def auth_resolver(obj, info, **kwargs):
            if not info.context["has_valid_auth"]:
                raise Exception("Auth is required!")

            return resolver(obj, info, **kwargs)

        field.resolve = auth_resolver
        return field

@TMuszczekk TMuszczekk added to do and removed roadmap Feature that we want to have included labels Mar 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request to do
Projects
None yet
Development

No branches or pull requests

3 participants