- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我使用 graphen-django 构建 GraphQL API。我已成功创建此 API,但无法传递参数来过滤我的响应。
这是我的 models.py:
from django.db import models
class Application(models.Model):
name = models.CharField("nom", unique=True, max_length=255)
sonarQube_URL = models.CharField("Url SonarQube", max_length=255, blank=True, null=True)
def __unicode__(self):
return self.name
这是我的schema.py: 进口 Graphite 烯 从 graphene_django 导入 DjangoObjectType 从模型导入应用程序
class Applications(DjangoObjectType):
class Meta:
model = Application
class Query(graphene.ObjectType):
applications = graphene.List(Applications)
@graphene.resolve_only_args
def resolve_applications(self):
return Application.objects.all()
schema = graphene.Schema(query=Query)
我的urls.py:
urlpatterns = [
url(r'^', include(router.urls)),
url(r'^admin/', admin.site.urls),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
url(r'^api-token-auth/', authviews.obtain_auth_token),
url(r'^graphql', GraphQLView.as_view(graphiql=True)),
]
如您所见,我还有一个 REST API。
我的 settings.py 包含这个:
GRAPHENE = {
'SCHEMA': 'tibco.schema.schema'
}
我遵循这个:https://github.com/graphql-python/graphene-django
当我发送这个请求时:
{
applications {
name
}
}
我得到了这样的回复:
{
"data": {
"applications": [
{
"name": "foo"
},
{
"name": "bar"
}
]
}
}
所以,成功了!
但是当我尝试传递这样的参数时:
{
applications(name: "foo") {
name
id
}
}
我有这样的回应:
{
"errors": [
{
"message": "Unknown argument \"name\" on field \"applications\" of type \"Query\".",
"locations": [
{
"column": 16,
"line": 2
}
]
}
]
}
我错过了什么?还是我做错了什么?
最佳答案
我找到了解决方案,感谢:https://docs.graphene-python.org/projects/django/en/latest/
这是我的答案。我已经编辑了我的 schema.py:
import graphene
from graphene import relay, AbstractType, ObjectType
from graphene_django import DjangoObjectType
from graphene_django.filter import DjangoFilterConnectionField
from models import Application
class ApplicationNode(DjangoObjectType):
class Meta:
model = Application
filter_fields = ['name', 'sonarQube_URL']
interfaces = (relay.Node, )
class Query(ObjectType):
application = relay.Node.Field(ApplicationNode)
all_applications = DjangoFilterConnectionField(ApplicationNode)
schema = graphene.Schema(query=Query)
然后,它缺少一个包:django-filter ( https://github.com/carltongibson/django-filter/tree/master )。Django-filter 由 DjangoFilterConnectionField 使用。
现在我可以这样做了:
query {
allApplications(name: "Foo") {
edges {
node {
name
}
}
}
}
响应将是:
{
"data": {
"allApplications": {
"edges": [
{
"node": {
"name": "Foo"
}
}
]
}
}
}
关于python - graphene-django - 如何过滤?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40381998/
我尝试了解 Django 中的 Graphql 并使用 graphene和 graphene_django . 我的前端可能会使用 Vuejs 和 Apollo 客户端构建。 互联网上的所有教程都是关
我已经实现了 graphql 并且我正在迁移到中继。我已经为每个表创建了一个 uuid,名为“id”。我找到了我的应用程序 this github thread谈到可能会改变规范,但感觉就像一个兔子洞
不确定我设置错了什么,但是当我使用 uvicorn mysite.asgi:application 在 uvicorn 中运行时,我没有得到 graphiql 界面: [32mINFO[0m:
我想将状态字段添加到错误响应中,而不是这样: { "errors": [ { "message": "Authentication credentials were not p
我的 Django 模型中有一个图像字段,我正在尝试从 Graphene 获取图像字段输出的绝对路径。我记得使用 HttpRequest.build_absolute_uri 获取文件/图像字段的绝对
如果这个问题在其他地方得到回答,那么我很抱歉,但是下类后两天,仍然没有雪茄...... 我有一个播放器模型: class Player(models.Model): name = models
我的 Django 模型中有一个图像字段,我正在尝试从 Graphene 获取图像字段输出的绝对路径。我记得使用 HttpRequest.build_absolute_uri 获取文件/图像字段的绝对
我使用 Django 作为后端,使用 graphene-django 作为前端。我是 django 和 graphene 的新手,所以我不确定在这个设置中没有代码重复的情况下实现字段级权限的最佳方法是
我的应用程序有几个多对多关系 带直通型 像这样: class Person(models.Model): name = models.CharField() class Group(model
我想知道如何正确创建用于创建此 Django 模型的突变: class Company(models.Model): class Meta: db_table = 'compa
我使用 django 和 django graphene 来制作 graphql API。 在我的应用程序中,我使用reactJS和react-bootstrap-table 。 React-boot
我目前正在使用 Python-Graphene 为我的 Django 应用程序创建一个 GraphQL 接口(interface)。虽然查询效果很好,但突变 - 不完全是。 成分的模型: class
假设一个类似于此的 Django 模型: class Profile(models.Model): user = models.OneToOneField(User, on_delete=
我使用 graphen-django 构建 GraphQL API。我已成功创建此 API,但无法传递参数来过滤我的响应。 这是我的 models.py: from django.db import
我在 Django 对象类型定义中遇到 get_node 方法的问题。在我的案例中似乎没有调用该方法。 我什至尝试通过在 get_node 方法中暂停执行来使用 pdb 进行调试,但也没有用。 这是我
到目前为止,我可以在不需要 DjangoObjectType 的情况下使用 Graphene。我尽量避免它,因为我不打算离我的 Django 模型类太近。但是我在使用 Graphene 实现 Rela
说我有, class PersonNode(DjangoObjectType): class Meta: model = Person fields = ('f
所以我的模型看起来像 class Abcd(models.Model): name = models.CharField(max_length=30, default=False) d
目前使用graphene-python 和graphene-django(和graphene-django-optimizer)。 收到GraphQL查询后,数据库查询在几分之一秒内成功完成;然而,
每当引发异常时,它们都会记录在控制台中(如果使用了 Sentry,则记录在 Sentry 中)。 许多这些异常(exception)仅旨在向用户显示。例如, django-graphql-jwt ra
我是一名优秀的程序员,十分优秀!