Graphene-Django文档注意到,如果您不想使用GraphiQL API浏览器graphiql=False
,GraphQLView
则可以在实例化时通过。但是,我想保持GraphiQL API浏览器可用,而仅限制谁可以访问它。那怎么办?
例如,如何使只有“职员”用户(可以访问管理站点)才有权访问GraphiQL浏览器?
您可以扩展Graphene-DjangoGraphQLView
并覆盖其can_display_graphiql
方法(在此处定义)以添加这种逻辑。
from graphene_django.views import GraphQLView as BaseGraphQLView
class GraphQLView(BaseGraphQLView):
@classmethod
def can_display_graphiql(cls, request, data):
# Only allow staff users to access the GraphiQL interface
if not request.user or not request.user.is_staff:
return False
return super().can_display_graphiql(request, data)
然后在urls.py文件中,使用新的GraphQLView
而不是默认的:
# import the GraphQLView defined above
urlpatterns = [
# ...
path("graphql", GraphQLView.as_view(graphiql=True)),
]
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句