0

GraphQL 新手在这里。所以我试图在 Django 中使用石墨烯编写一个模式,它将国家参数作为字符串和时间参数作为字符。该查询与“时间”参数一起工作正常,但每当我添加“国家”参数时。它引发以下错误"Unknown argument \"country\" on field \"allNews\" of type \"Query\".",。这是我的架构和类型:

类型:

class NewsType(DjangoObjectType):
    class Meta:
        model = News
        fields = ('id', 'site_name', 'title', 'link', 'content', 'thumbnail', 'status', 'upvote', 'downvote', 'published_date',
                  'paper', 'created_date')
        filter_fields = ['site_name', 'status', 'country']
        interfaces = (CustomNewsNode, )

    vote_status = DjangoFilterConnectionField(UserVoteNewsType)
    def resolve_vote_status(self, info, user_id):

        return self.uservotenews_set.filter(user_id=user_id)

架构:

    class NewsTypeFilter(django_filters.FilterSet):
        class Meta:
            model = News
            fields = ['id', 'site_name', 'title', 'link', 'content', 'thumbnail', 'status', 'upvote', 'downvote', 'published_date',
                      'paper', 'created_date']

class Query(graphene.ObjectType):
    all_news = DjangoFilterConnectionField(
        NewsType,  filterset_class=NewsTypeFilter, time=graphene.String())
     def resolve_all_news(self, info, **kwargs):
        print(kwargs['country'])
        env = environ.Env()
        environ.Env.read_env()
        HOURS_AFTER_NEWS_VISIBLE = env('HOURS_AFTER_NEWS_VISIBLE')
        timeDiff = datetime.now() - timedelta(hours=int(HOURS_AFTER_NEWS_VISIBLE))

        if kwargs['time']:  # if user clicks on sort by button
            if(kwargs['time'] == 'd'):
                time = 1
            elif (kwargs['time'] == 'w'):
                time = 7
            elif (kwargs['time'] == 'm'):
                time = 30
            elif (kwargs['time'] == 'y'):
                time = 365
            else:
                time = 1000
            end_of_give_time_period = datetime.now(
            ) - timedelta(days=int(HOURS_AFTER_NEWS_VISIBLE))
            start_of_given_time_period = end_of_give_time_period - \
                timedelta(days=time)
            allNews = News.objects.order_by('created_date').filter(status=True,
                                                                   created_date__lte=end_of_give_time_period, created_date__gte=start_of_given_time_period).order_by('-upvote')
        else:
            allNews = News.objects.order_by(
                '-created_date').filter(status=True).filter(created_date__lte=timeDiff)

        if 'country' in kwargs:
            countryId = Country.objects.filter(
                name=kwargs['country'][0]).values_list('pk', flat=True)
            allNews = allNews.filter(country__in=[countryId[0]])

        return allNews

但是每当我输入查询时,它都会返回以下错误:

询问:

query{
  allNews (time :"w", country: "Bangladesh") {
    edges{
      node{
        title
        upvote
        createdDate
      }
}
  }

}

输出:

{
  "errors": [
    {
      "message": "Unknown argument \"country\" on field \"allNews\" of type \"Query\".",
      "locations": [
        {
          "line": 2,
          "column": 23
        }
      ]
    }
  ]
}
4

1 回答 1

0

首先,我NewsTypeFilter从架构中删除了 ,因为它返回的结果与我的NewsType. 然后我country在我的NewsType. 这几乎解决了这个问题。

我猜首先添加 filterType 会产生问题。

于 2020-02-09T05:47:06.113 回答