rscarrera27 / Sanic-JWT-Extended

⚡️An open source Sanic extension that provides "extended" JWT support
https://sanic-jwt-extended.seonghyeon.dev
MIT License
35 stars 8 forks source link

Including AuthInfoField in a relay connection type #34

Open ShivanshJ opened 3 years ago

ShivanshJ commented 3 years ago

I wanted to add Pagination with relay into my query.

The query fetches posts from user. The following is the working of graphene.Union - to include Authentication as given in the documentation.

class PostObject(SQLAlchemyObjectType):
    class Meta:
        model = Post
        interfaces = (graphene.relay.Node, )

class ProtectedPost(graphene.Union):
    class Meta:
        types = (PostObject, AuthInfoField)

And the query is as follows:

 class Query(graphene.ObjectType):
    node = graphene.relay.Node.Field()
    my_posts = graphene.List(ProtectedPost)

However to add Pagination support, I have to add relay connection. So instead of using graphene.List , I have to use SQLAlchemyConnectionField. my_posts = SQLAlchemyConnectionField(ProtectedPost).

But this gives the following error on starting the application : SQLALchemyConnectionField only accepts SQLAlchemyObjectType types, not ProtectedPost

What to do if we want to include Relay ?

ShivanshJ commented 3 years ago

I tried the following -

class PostConnection(graphene.relay.Connection):
    class Meta:
        node = ProtectedPost

And the query becomes

class Query(graphene.ObjectType):
    node = graphene.relay.Node.Field()
    my_posts = graphene.relay.ConnectionField(ProtectedPost)

But then when auth Token is incorrect, the error message is :

"errors": [
        {
            "message": "Resolved value from the connection field have to be iterable or instance of PostConnection. Received \"<flask_graphql_auth.fields.AuthInfoField object at 0x1077adfa0>\"",
            .
            .
            .
ShivanshJ commented 3 years ago

Can we simply not include GraphQLError in this library instead of using AuthInfoField. i.e,

from graphql.error.base import GraphQLError

func () {
.
return GraphQLError("Unauthenticated")
}

Can I raise a PR to replace all AuthInfoFields ?