Skip to content

print_schema creates multiple directives if a field is used in multiple schemas #3596

Open

Description

class UnauthorizedException(Exception):
    pass

class FeaturesRequired(BasePermission):
    message = "Keine Rechte"
    error_class = UnauthorizedException
    error_extensions = {"code": 401}

    def __init__(self, *features):
        self.features = features

    async def has_permission(self, source: Any, info: Info, **kwargs) -> bool:
        return 'test' in self.features

@strawberry.type
class MyType:

    @strawberry.field(extensions=[PermissionExtension(permissions=[FeaturesRequired('test')])])
    def hello(self) -> str:
        return 'world'
 
# schema_1.py

@strawberry.type
class Query1:
  @strawberry.field
  def my_type(self, info: Info) -> MyType:
    return MyType()

schema_1 = Schema(query=Query)

# schema_2.py:

@strawberry.type
class Query2:
  @strawberry.field
  def my_type(self, info: Info) -> MyType:
    return MyType()


schema_2 = Schema(query=Query)

generate the gql file:

from schema_1 import schema_1
from strawberry.printer import print_schema

with open('test.graphql', 'w') as f:
  f.write(print_schema(schema_1))


generates:

hello: String! @featuresRequired @featuresRequired

which leads to errors in vsc like The directive "@featuresRequired" can only be used once at this location.
Bug: This code is getting called twice, one time for schema_1 and one time for schema_2

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions