当我使用GraphQL偏移量查询作者表时,出现错误:
[GraphQLError('字段“作者”类型为“查询”的未知参数“偏移”
我预订了一些网站,它通常说只要使用offset参数,它就可以工作。你能帮我找到原因吗?
测试环境:django2.1.7 + python3.6 + win7
我的模型。py:
class BaseTimeStamp(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
abstract = True
class BaseStatus(models.Model):
STATUS_ENABLED = 0
STAUS_DISABLED = 1
STATUS_CHOICE = (
(STATUS_ENABLED, 'enabled'),
(STAUS_DISABLED, 'disabled')
)
status =
models.SmallIntegerField(choices=STATUS_CHOICE,default=STATUS_ENABLED)
class Meta:
abstract = True
class ModelFiltered(object):
@staticmethod
def get_model_field_names(cls, excludes=[], includes=[]):
fields = list(map(lambda model_field: model_field.name , cls._meta.fields))
filtered = list(filter(lambda field_name: field_name not in excludes, fields))
filtered.extend(includes)
return filtered
class Author(BaseTimeStamp, BaseStatus):
name = models.CharField(max_length=128, db_index=True)
country = models.CharField(max_length=128)
@staticmethod
def get_serialier_fields():
return ModelFiltered.get_model_field_names(Author)
@staticmethod
def get_admin_display_fields():
return ModelFiltered.get_model_field_names(Author)
class Meta:
ordering = ('created_at', )
我的schema.py代码:
class CountableConnectionBase(relay.Connection):
class Meta:
abstract = True
total_count = graphene.Int()
def resolve_total_count(self, info, **kwargs):
return self.iterable.count()
class AuthorType(DjangoObjectType):
class Meta:
model = Author
interfaces = (relay.Node,)
connection_class = CountableConnectionBase
class AuthorInput(graphene.InputObjectType):
name = graphene.String(required=True)
id = graphene.String(required=False)
class CreateAuthor(graphene.Mutation):
class Arguments:
author_data = AuthorInput(required=True)
ok = graphene.Boolean()
author = graphene.Field(AuthorType)
def mutate(self, info, author_data):
author = Author.objects.create(name=author_data['name'])
ok = True
return CreateAuthor(author=author, ok=ok)
class Query(graphene.ObjectType):
authors = DjangoConnectionField(AuthorType,
id=graphene.String(),
name=graphene.String(),
status=graphene.Int(),
orderby=graphene.String())
def resolve_authors(self, info, **kwargs):
orderby = kwargs.get('orderby', 'created_at')
name = kwargs.get("name")
if name is None:
return Author.objects.all()
else:
results = Author.objects.filter(Q(name=name)).order_by(orderby)
return results
class Mutations(graphene.ObjectType):
create_author = CreateAuthor.Field()
schema = graphene.Schema(query=Query, mutation=Mutations)
我的query_template:
query_template_str_author = '''query allauthors {{
authors({}) {{
totalCount
edges {{
node {{
id,
name
}}
cursor
}}
pageInfo {{
endCursor
startCursor
hasNextPage
hasPreviousPage
}}
}}
}}'''
我的测试代码:
params = 'first:3, offset:1'
query_str = query_template_str_author.format(params)
result = schema.execute(query_str)
self.assertEqual(result.data is not None, True)