Skip to content
Snippets Groups Projects
Commit 5dc9dfeb authored by jan.bednarik's avatar jan.bednarik
Browse files

Authors field in schema.

parent 677c2168
No related branches found
No related tags found
No related merge requests found
......@@ -9,6 +9,13 @@ from .. import search
from ..models import User
class AuthorsConnection(relay.Connection):
total_count = graphene.Int()
class Meta:
node = types.Author
class SearchReportsConnection(relay.Connection):
total_count = graphene.Int()
......@@ -26,6 +33,7 @@ class Query:
' Default: false')
node = relay.Node.Field()
authors = relay.ConnectionField(AuthorsConnection)
search_reports = relay.ConnectionField(
SearchReportsConnection,
description='Fulltext search in Reports.',
......@@ -38,6 +46,22 @@ class Query:
description='Shortcuts for login. Use with LoginByShortcut mutation.',
)
def resolve_authors(self, info, **kwargs):
paginator = Paginator(**kwargs)
total = User.objects.filter(is_author=True).count()
authors = User.objects.filter(is_author=True)[paginator.slice_from:paginator.slice_to]
page_info = paginator.get_page_info(total)
edges = []
for i, author in enumerate(authors):
cursor = paginator.get_edge_cursor(i + 1)
node = types.Author.from_db(author)
edges.append(AuthorsConnection.Edge(node=node, cursor=cursor))
return AuthorsConnection(page_info=page_info, edges=edges, total_count=total)
def resolve_search_reports(self, info, **kwargs):
paginator = Paginator(**kwargs)
query = kwargs.get('query', '')
......
......@@ -5,6 +5,7 @@ from django.contrib.postgres.fields import JSONField
class User(AbstractUser):
# TODO remove username, set different login field
openid_uid = models.CharField(max_length=255, unique=True, db_index=True)
extra = JSONField(null=True, blank=True)
is_author = models.BooleanField(default=False)
......
......@@ -16,3 +16,5 @@ snapshots['test_node__login_shortcut 1'] = b'{"data":{"node":{"id":"TG9naW5TaG9y
snapshots['test_node__author 1'] = b'{"data":{"node":{"id":"QXV0aG9yOjU=","firstName":"Winston","lastName":"Wolfe","openidUid":"TheWolf","extra":"{\\"x\\": 1}"}}}'
snapshots['test_node__author__only_if_is_author 1'] = b'{"data":{"node":null}}'
snapshots['test_authors 1'] = b'{"data":{"authors":{"totalCount":1,"edges":[{"node":{"id":"QXV0aG9yOjU=","firstName":"Winston","lastName":"Wolfe","openidUid":"TheWolf","extra":"{\\"x\\": 1}"}}]}}}'
......@@ -88,3 +88,34 @@ def test_node__author__only_if_is_author(client, snapshot):
}}
""".format(id=to_global_id('Author', 7))})
snapshot.assert_match(res.content)
@pytest.mark.django_db
def test_authors(client, snapshot):
User.objects.create(
id=5,
username='a',
is_author=True,
openid_uid='TheWolf',
first_name='Winston',
last_name='Wolfe',
extra={'x': 1},
)
User.objects.create(id=7, is_author=False, username='b')
res = client.post('/graphql', {'query': """
query {
authors {
totalCount
edges {
node {
id
firstName
lastName
openidUid
extra
}
}
}
}
"""})
snapshot.assert_match(res.content)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment