标签: graphene-python

在 graphene-sqlalchemy 查询中按 id 过滤

如何设置 graphene-sqlalchemy 按 id 过滤对象?

我想运行查询:

{
  marker(markerId: 1) {
    markerId
    title
  }
}
Run Code Online (Sandbox Code Playgroud)

我希望获得一个 MarkerId 为 1 的 Marker 对象,但收到错误““Query”类型的“marker”字段上的未知参数“markerId””。

我有两个文件:

模式.py

{
  marker(markerId: 1) {
    markerId
    title
  }
}
Run Code Online (Sandbox Code Playgroud)

模型.py

import graphene
from graphene_sqlalchemy import SQLAlchemyObjectType
from model import db_session, Marker as MarkerModel

class Marker(SQLAlchemyObjectType):
    class Meta:
        model = MarkerModel

class Query(graphene.ObjectType):
    marker = graphene.Field(Marker)
    markers = graphene.List(Marker)

    def resolve_markers(self, args, context, info):
        return db_session.query(MarkerModel).all()

    def resolve_marker(self, args, context, info):
        return db_session.query(MarkerModel).first()

schema = graphene.Schema(query=Query)
Run Code Online (Sandbox Code Playgroud)

感谢您的帮助!

sqlalchemy graphql graphene-python

5
推荐指数
1
解决办法
4700
查看次数

石墨烯模式中的 Django http 请求

如何将 http-request-session 对象获取到石墨烯模式中?

我已在请求会话中存储了一些值,我需要通过这些值进行访问。一种可能的解决方案是将 session-id 发送到前端,然后将其传递到 post 请求中,但这似乎不是一个好的解决方案。

石墨烯有一个 context_value 但我不明白我是如何工作的。

我将其放入我的 Django 视图中:

schema = graphene.Schema()
schema.execute('{ viewer }', context_value={'session': request.session})
Run Code Online (Sandbox Code Playgroud)

在我的石墨烯模式中,如果我尝试按照教程中描述的方式进行操作(https://github.com/graphql-python/graphene/blob/master/docs/execution/execute.rst),它会说

“WSGIRequest”对象没有属性“get”

class Query(graphene.ObjectType):
  viewer = graphene.Field(Viewer)

  def resolve_viewer(self, info):
    info.context.get('session')
    print(info.context.session.keys()) #an empty array
    return Viewer()
Run Code Online (Sandbox Code Playgroud)

django schema request django-sessions graphene-python

5
推荐指数
1
解决办法
3389
查看次数

将 AWS AppSync (+ Cognito) 架构与 lambda 上的 Graphene django 集成

我有一个带有一些 DynamoDB 表的 AWS AppSync GraphQL 终端节点,并且它与 Cognito 集成。

我还开发了一个带有 graphql 端点(graphene)和 cognito 集成的 django 应用程序,该应用程序与 zappa 一起部署到 lambda 函数。

组合这两个 graphql 端点以便我可以从一个端点查询它们的最佳方法是什么?1-Appsync 使用解析器将请求转发给 lambda?(有例子吗?) 2- 拦截来自 api 网关的调用并将其转发到 appsync?(不太可能)

谢谢,阿尔普

amazon-web-services amazon-cognito aws-lambda graphene-python aws-appsync

5
推荐指数
0
解决办法
1414
查看次数

默认解析器的身份验证/授权

我阅读了有关如何将@login_required和其他装饰器与解析器一起使用的描述。然而,如果不使用显式解析器(而是使用默认解析器),如何实施类似的访问控制?

就我而言,我将石墨烯与 Django 用户模型结合使用。我有以下内容:

class UserNode(DjangoObjectType):
    class Meta:
        model = User
        filter_fields = ['first_name', 'last_name', 'id', 'email']
        interfaces = (Node, )

class Query(object):
    userNode = relay.Node.Field(UserNode)
    all_users = DjangoConnectionField(UserNode)
Run Code Online (Sandbox Code Playgroud)

如果我显式定义“resolve_all_users”方法并在其上使用 @login_required 装饰器,则它可以正常工作。但我的架构中的这个(和其他对象)依赖于默认解析器。如何在无需显式定义解析器的情况下保护它们?

我承认自己是使用石墨烯/graphql 的新手……非常感谢任何为我指明正确方向的帮助。

来源: https: //github.com/flavors/django-graphql-jwt/issues/36

django graphql graphene-python

5
推荐指数
1
解决办法
828
查看次数

如何查看 graphene-django 调试日志

DEBUG我在使用 Graphene 和 Django 查看级别日志时遇到问题。我已设置以下内容settings.py

\n\n
\nLOGGING = {\n \'version\': 1,\n \'disable_existing_loggers\': False,\n \'handlers\': {\n \'console\': {\n \'class\': \'logging.StreamHandler\',\n },\n },\n \'loggers\': {\n \'django\': {\n \'handlers\': [\'console\'], \n \'level\': \'DEBUG\'\n },\n \'django.request\': {\n \'handlers\': [\'console\'],\n \'level\ ': \'调试\'\n },\n },\n}\n
\n\n

然而,当我尝试查看 Django 服务器的日志时,我看到的只是:

\n\n
\n \xe2\x9d\xaf\xe2\x9d\xaf\xe2\x9d\xaf kubectl messages -f server-6b65f48895-bmp6w server\n要执行的操作:\n 应用所有迁移:admin、auth、contenttypes、django_celery_beat、django_celery_results 、服务器、会话、social_django\n运行迁移:\n 没有要应用的迁移。\n正在执行系统检查...\n\n系统检查未发现任何问题(0 已静音)。\n2018 年 10 月 8 日 - 23:59:00\nDjango版本 2.0.6,使用设置 \'backend.settings\'\n在 http://0.0.0.0:8000/\n启动开发服务器\n使用 CONTROL-C 退出服务器。\n"POST /graphql HTTP/1.1" 400 113 \n“POST /graphql HTTP/1.1”400 113\n“POST /graphql …

django graphql graphene-python

5
推荐指数
1
解决办法
3878
查看次数

如何使用 django_graphene 解析 django 模型的自定义字段?

看看 graphene_django,我发现他们有一堆解析器拾取 django 模型字段,将它们映射到石墨烯类型。

我有一个JSONField的子类,我也希望被选中。

:

# models
class Recipe(models.Model):
    name = models.CharField(max_length=100)
    instructions = models.TextField()
    ingredients = models.ManyToManyField(
        Ingredient, related_name='recipes'
    )
    custom_field = JSONFieldSubclass(....)


# schema
class RecipeType(DjangoObjectType):
    class Meta:
        model = Recipe

    custom_field = ???
Run Code Online (Sandbox Code Playgroud)

我知道我可以为查询编写一个单独的字段和解析器对,但我更希望将其作为该模型架构的一部分。

我意识到我可以做什么:

class RecipeQuery:
    custom_field = graphene.JSONString(id=graphene.ID(required=True))

    def resolve_custom_field(self, info, **kwargs):
       id = kwargs.get('id')
       instance = get_item_by_id(id)
       return instance.custom_field.to_json()
Run Code Online (Sandbox Code Playgroud)

但是——这意味着一个单独的往返,获取 id,然后获取该项目的 custom_field,对吧?

有没有办法将其视为 RecipeType 架构的一部分?

python django graphql graphene-python

5
推荐指数
1
解决办法
4461
查看次数

AssertionError:在架构中找到具有相同名称的不同类型

我的模型中有两个类:Products 和 SalableProducts(SalableProducts 继承自 Products,因此它具有数据库的每个字段)。下面是我的架构

我尝试包含“exclude_fields”属性,但这不起作用

产品架构.py:

class Product(SQLAlchemyObjectType):
 class Meta:
  model = ProductModel
  interfaces = (relay.Node, )

class ProductConnections(relay.Connection):
 class Meta:
  node = Product
Run Code Online (Sandbox Code Playgroud)

Salable_product_schema.py:

class SalableProduct(SQLAlchemyObjectType):
 class Meta:
  model = SalableProductModel
  interfaces = (relay.Node, )

class SalableProductConnections(relay.Connection):
 class Meta:
  node = SalableProduct

Run Code Online (Sandbox Code Playgroud)

架构.py:

class Query(graphene.ObjectType):
 node = relay.Node.Field()
 all_products = SQLAlchemyConnectionField(ProductConnections)
 all_salable_products = 
  SQLAlchemyConnectionField(SalableProductConnections)
Run Code Online (Sandbox Code Playgroud)

结果是这个错误:

AssertionError:在架构中发现具有相同名称的不同类型:product_status、product_status。

(product_status是两个类通过继承共享的属性)

python graphql graphene-python graphene-sqlalchemy

5
推荐指数
1
解决办法
3127
查看次数

如何过滤/后处理 Python-Graphene 结果

我目前正在使用 Python-Graphene 编写 GraphQL API。

一个解析器提供了一个条目列表,我想在这里进行一些后处理/过滤。然而,子条目的属性此时尚未解析,当然稍后再解析。

我能做些什么?尝试返回一个 Promise 并绑定到它的“then”方法,或者编写一个中间件。它全部给出了“未处理的”子条目,即没有解析的数据。做了一些研究,如果我可以在某些隐藏的数据结构中找到数据,但 pysnooper 证明此时根本没有计算数据。

请注意:我在这个项目中没有使用 Django。这是我尝试做的一个最小示例,请注意 TODO 行

""" Test some graphene features"""

import graphene
from collections import namedtuple
from datetime import date
from json import dumps

DBUser = namedtuple('DBUser', ('name', 'year_of_birth'))

# Our, ehm, professional database ;-)
DATABASE = [
    DBUser('John', 1970),
    DBUser('Mary', 1980),
    DBUser('Sandra', 1990),
    DBUser('Michael', 2000),
]


class User(graphene.ObjectType):

    name = graphene.String()
    age = graphene.Int()

    def resolve_name(self, info):
        return self.name

    def resolve_age(self, info):
        return date.today().year - self.year_of_birth


class …
Run Code Online (Sandbox Code Playgroud)

python graphql graphene-python

5
推荐指数
1
解决办法
741
查看次数

石墨烯:枚举参数似乎不起作用

我目前在突变 enum 上很难Argument

以下是我的代码Mutation

class CreatePerson(graphene.Mutation):
    foo = graphene.String()

    def mutate(self, info, **kwargs):
        return CreatePerson(foo='foo')


    class Arguments:
        enum_arg = graphene.Argument(graphene.Enum.from_enum(EnumArg))
Run Code Online (Sandbox Code Playgroud)

枚举类:

from enum import Enum

class EnumArg(Enum):
    Baz = 0
    Bar = 1
    Spam = 2
    Egg = 3
Run Code Online (Sandbox Code Playgroud)

使用POSTMAN命令:

{
    "query": "mutation": {createPerson(enumArg=1) { foo }}
}
Run Code Online (Sandbox Code Playgroud)

但我最终得到了这个错误信息:

"message": "Argument \"enumArg\" has invalid value 1.
            Expected type \"EnumArg\", found 1.",
Run Code Online (Sandbox Code Playgroud)

我也试过给人enumArg=\"Bar\"createPerson突变和错误仍然存在。

python graphql graphene-python

5
推荐指数
1
解决办法
3426
查看次数

以列表作为输入的石墨烯突变

我有这样的石墨烯突变:


class User(ObjectType):
    username = String()

class ImportUsers(Mutation):
    class Arguments:
        users = List(User)
    Output = List(User)

    @staticmethod
    def mutation(root, info, users):
        ...
Run Code Online (Sandbox Code Playgroud)

但石墨烯给了我以下错误:AssertionError: Mutations.importUsers(users:) argument type must be Input Type but got: [User].

如何在石墨烯中进行突变以接受对象列表?

graphql graphene-python

5
推荐指数
1
解决办法
5027
查看次数