GraphQL-"Unknown argument \"country\" on field \"allNews\" of type \"Query\".",
GraphQL-"Unknown argument \"country\" on field \"allNews\" of type \"Query\".",
这里是 GraphQL 新手。 所以我尝试使用石墨烯在 Django 中编写一个模式,它将国家参数作为字符串,将时间参数作为字符。该查询使用 "time" 参数可以正常工作,但每当我添加 "country" 参数时。它抛出以下错误 "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
但是每当我输入查询时,它 returns 出现以下错误:
查询:
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
}
]
}
]
}
首先,我从我的架构中删除了 NewsTypeFilter
,因为它返回的与我的 NewsType
相同。然后我在 NewsType
上添加了 country
和其他字段。这几乎解决了问题。
我想添加 filterType 一开始就造成了问题。
这里是 GraphQL 新手。 所以我尝试使用石墨烯在 Django 中编写一个模式,它将国家参数作为字符串,将时间参数作为字符。该查询使用 "time" 参数可以正常工作,但每当我添加 "country" 参数时。它抛出以下错误 "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
但是每当我输入查询时,它 returns 出现以下错误:
查询:
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
}
]
}
]
}
首先,我从我的架构中删除了 NewsTypeFilter
,因为它返回的与我的 NewsType
相同。然后我在 NewsType
上添加了 country
和其他字段。这几乎解决了问题。
我想添加 filterType 一开始就造成了问题。