21

我使用 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:从 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
        }
      ]
    }
  ]
}

我错过了什么?或者我做错了什么?

4

3 回答 3

21

我找到了一个解决方案,感谢: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 )。DjangoFilterConnectionField 使用 Django-filter。

现在我可以这样做了:

query {
  allApplications(name: "Foo") {
    edges {
      node {
        name
      }
    }
  }
}

响应将是:

{
  "data": {
    "allApplications": {
      "edges": [
        {
          "node": {
            "name": "Foo"
          }
        }
      ]
    }
  }
}
于 2016-11-03T14:02:54.640 回答
5

如果您是我的情况并且不想使用 Relay,您还可以使用 Django orm 过滤直接在解析器中处理过滤。此处的示例:在 django 中过滤 graphql 查询

于 2020-03-26T15:21:35.953 回答
3

对 Adrien Answer 的补充很少。如果您想在过滤时执行不同的操作,例如包含和完全匹配,请编辑您的schema.py

class ApplicationNode(DjangoObjectType):
    class Meta:
        model = Application
        # Provide more complex lookup types
        filter_fields = {
            'name': ['exact', 'icontains', 'istartswith']
        }
        interfaces = (relay.Node, )

你可以这样写查询

  query {
  allApplications(name_Icontains: "test") {
    edges {
      node {
        id,
        name
      }
    }
  }
}
于 2021-02-14T17:46:27.717 回答