0
votes

GraphQL newbie here. So I was trying to write a schema in Django using graphene which will take country parameter as a String & time parameter as Character. The query works fine with the "time" parameter, but whenever I add the "country" param. it throws the following error "Unknown argument \"country\" on field \"allNews\" of type \"Query\".",. Here is my Schema and Type:

Type:

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)

Schema:

    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

But whenever I enter my query, it returns the following error:

Query:

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

}

output:

{
  "errors": [
    {
      "message": "Unknown argument \"country\" on field \"allNews\" of type \"Query\".",
      "locations": [
        {
          "line": 2,
          "column": 23
        }
      ]
    }
  ]
}
1
You forgot to add 'country' to your all_news = DjangoFilterConnectionField( NewsType, filterset_class=NewsTypeFilter, time=graphene.String())engin_ipek
Adding the country didn't solve it either.Proteeti Prova

1 Answers

0
votes

First of all, I stripped the NewsTypeFilter from my schema, because it's returning the same as my NewsType. Then I added country with the other fields on my NewsType. That pretty much solved the problem.

I guess adding the filterType created the problem at first.