简体   繁体   English

graphene-django - 如何过滤?

[英]graphene-django - How to filter?

I use graphen-django for build a GraphQL API.我使用 graphen-django 来构建 GraphQL API。 I have succesfully create this API, but I can't pass a argument for filter my response.我已成功创建此 API,但无法传递参数来过滤我的响应。

This is my models.py :这是我的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

This is my schema.py : import graphene from graphene_django import DjangoObjectType from models import Application这是我的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)

My urls.py :我的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)),
]

As you can see, I also have a REST API.如您所见,我还有一个 REST API。

My settings.py contains this:我的settings.py包含这个:

GRAPHENE = {
    'SCHEMA': 'tibco.schema.schema'
}

I follow this: https://github.com/graphql-python/graphene-django我按照这个: https : //github.com/graphql-python/graphene-django

When I send this resquest:当我发送此请求时:

{
  applications {
    name
  }
}

I've got this response:我得到了这样的回应:

{
  "data": {
    "applications": [
      {
        "name": "foo"
      },
      {
        "name": "bar"
      }
    ]
   }
}

So, it's works!所以,它的作品!

But when I try to pass an argument like this:但是当我尝试传递这样的参数时:

{
  applications(name: "foo") {
    name
    id
  }
}

I have this response:我有这样的回应:

{
  "errors": [
   {
      "message": "Unknown argument \"name\" on field \"applications\" of type \"Query\".",
      "locations": [
        {
          "column": 16,
          "line": 2
        }
      ]
    }
  ]
}

What i have missed?我错过了什么? Or maybe I do something wrong?或者我做错了什么?

I've found a solution thanks to: https://docs.graphene-python.org/projects/django/en/latest/我找到了一个解决方案,感谢: https : //docs.graphene-python.org/projects/django/en/latest/

This is my answer.这是我的回答。 I have edit my schema.py :我已经编辑了我的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)

Then, it was missing a package: django-filter ( https://github.com/carltongibson/django-filter/tree/master ).然后,它缺少一个包:django-filter ( https://github.com/carltongibson/django-filter/tree/master )。 Django-filter is used by DjangoFilterConnectionField. Django-filter 由 DjangoFilterConnectionField 使用。

Now I can do this:现在我可以这样做:

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

and the response will be:响应将是:

{
  "data": {
    "allApplications": {
      "edges": [
        {
          "node": {
            "name": "Foo"
          }
        }
      ]
    }
  }
}

If you're in my case and don't want to use Relay, you can also handle filtering directly in you resolvers using Django orm filtering.如果您在我的情况下不想使用 Relay,您也可以使用 Django orm 过滤直接在解析器中处理过滤。 Example here: Filter graphql query in django此处示例:在 django 中过滤 graphql 查询

Little addition to Adrien Answer.对 Adrien Answer 的补充很少。 If you want to perform different operation while filtering like contains and exact match then edit your schema.py如果您想在过滤时执行不同的操作,例如包含和精确匹配,请编辑您的schema.py

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

and you can write the query like this你可以像这样编写查询

  query {
  allApplications(name_Icontains: "test") {
    edges {
      node {
        id,
        name
      }
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM