Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get user auth info in FastAPI, when using `app.add_route()` for GraphQL?

I'm using FastAPI and now want to add GraphQL using graphene.

I'm using:

  • fastapi-user
  • starlette.graphql.GraphQLApp

Here's the routing for GraphQL and how I used fastapi-user package.

import fastapi_users
from starlette.graphql import GraphQLApp
from mypackage.schema import my_schema
...

app.include_router(fastapi_users.router, prefix="/users", tags=["users"])
app.include_router(google_oauth_router, prefix="/google-oauth", tags=["users"])
app.add_route("/", GraphQLApp(schema=my_schema))
...

In the schema, I need to get user information and control the role based auth.

How to, or how can I use get_current_active_user() method with GraphQL schema?

like image 396
Renz Serrano Avatar asked Dec 08 '25 08:12

Renz Serrano


1 Answers

I'm assuming that my_schema inherits graphene.ObjectType and that the first argument is info as shown in the example at:

https://fastapi.tiangolo.com/advanced/graphql/

from graphene import ObjectType, String

class Query(ObjectType):
    hello = String(name=String(default_value="stranger"))

    def resolve_hello(self, info, name):
        return "Hello " + name

info contains the session object with your user information.

info.context['request'].session['user']

like image 188
APerlman Avatar answered Dec 09 '25 21:12

APerlman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!