Add GraphQL for users and groups

This commit is contained in:
jeremystretch
2021-06-29 13:15:10 -04:00
parent ef3cb9544a
commit 05ba54b6d3
6 changed files with 56 additions and 2 deletions

View File

View File

@@ -0,0 +1,12 @@
import graphene
from netbox.graphql.fields import ObjectField, ObjectListField
from .types import *
class UsersQuery(graphene.ObjectType):
group = ObjectField(GroupType)
groups = ObjectListField(GroupType)
user = ObjectField(UserType)
users = ObjectListField(UserType)

View File

@@ -0,0 +1,37 @@
from django.contrib.auth.models import Group, User
from graphene_django import DjangoObjectType
from users import filtersets
from utilities.querysets import RestrictedQuerySet
__all__ = (
'GroupType',
'UserType',
)
class GroupType(DjangoObjectType):
class Meta:
model = Group
fields = ('id', 'name')
filterset_class = filtersets.GroupFilterSet
@classmethod
def get_queryset(cls, queryset, info):
return RestrictedQuerySet(model=Group)
class UserType(DjangoObjectType):
class Meta:
model = User
fields = (
'id', 'username', 'password', 'first_name', 'last_name', 'email', 'is_staff', 'is_active', 'date_joined',
'groups',
)
filterset_class = filtersets.UserFilterSet
@classmethod
def get_queryset(cls, queryset, info):
return RestrictedQuerySet(model=User)

View File

@@ -17,7 +17,7 @@ class AppTest(APITestCase):
self.assertEqual(response.status_code, 200)
class UserTest(APIViewTestCases.APIViewTestCase):
class UserTest(APIViewTestCases.GraphQLTestCase, APIViewTestCases.APIViewTestCase):
model = User
view_namespace = 'users'
brief_fields = ['display', 'id', 'url', 'username']
@@ -48,7 +48,7 @@ class UserTest(APIViewTestCases.APIViewTestCase):
User.objects.bulk_create(users)
class GroupTest(APIViewTestCases.APIViewTestCase):
class GroupTest(APIViewTestCases.GraphQLTestCase, APIViewTestCases.APIViewTestCase):
model = Group
view_namespace = 'users'
brief_fields = ['display', 'id', 'name', 'url']