python - 将 `app.add_route()` 用于 GraphQL 时,如何在 FastAPI 中获取用户身份验证信息?

标签 python oauth graphql fastapi starlette

我正在使用 FastAPI,现在想使用 graphene 添加 GraphQL。

我正在使用:

  • fastapi-user
  • starlette.graphql.GraphQLApp

这是 GraphQL 的路由以及我如何使用 fastapi-user 包。

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))
...

schema 中,我需要获取用户信息并控制基于角色的身份验证。

如何或如何将 get_current_active_user() 方法与 GraphQL 架构一起使用?

最佳答案

我假设 my_schema 继承了 graphene.ObjectType 并且第一个参数是 info,如以下示例所示:

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 包含带有您的用户信息的 session 对象。

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

关于python - 将 `app.add_route()` 用于 GraphQL 时,如何在 FastAPI 中获取用户身份验证信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61930695/

相关文章:

php - Facebook TokenResponseException 与 Laravel 4.1 和 oauth-4-laravel

java - GraphQL-SPQR 中返回错误的正确方法

graphql - GraphQL 和 SPARQL 有什么区别?

graphql 使用嵌套查询上的嵌套查询参数或嵌套查询上的父参数

python - Scrapy:pip使用python 3.6在Windows 10上安装scrapy并遇到Missing MSVCP140.dll文件

python - 在设置值时和构造函数中验证值 - Python 3.3

python - 从实例中获取模型名称

python - 为什么我要将代码放在 __init__.py 文件中?

php - 适用于 Windows 的 OAuth PHP?

ios - 有没有办法在显示 SFAuthenticationSession 提示之前知道是否有可用的 cookie