Skip to content

Commit

Permalink
Add mutation for enum arg and result
Browse files Browse the repository at this point in the history
  • Loading branch information
rafalp committed Oct 7, 2024
1 parent 573bba3 commit 017fe5a
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 3 deletions.
9 changes: 9 additions & 0 deletions example/enums/role.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from ariadne_graphql_modules import GraphQLEnum


class RoleEnum(GraphQLEnum):
__members__ = [
"ADMIN",
"MEMBER",
"GUEST",
]
4 changes: 4 additions & 0 deletions example/fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,25 @@ def get_data() -> dict[str, dict[int, Any]]:
id=1,
username="JohnDoe",
group_id=1,
role="ADMIN",
),
2: User(
id=2,
username="Alice",
group_id=1,
role="ADMIN",
),
3: User(
id=3,
username="Bob",
group_id=2,
role="MEMBER",
),
4: User(
id=4,
username="Mia",
group_id=2,
role="GUEST",
),
},
"categories": {
Expand Down
1 change: 1 addition & 0 deletions example/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ class User:
id: int
username: str
group_id: int
role: str
2 changes: 2 additions & 0 deletions example/mutations/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from typing import Any

from . import compare_roles
from . import dates_delta

mutations: list[Any] = [
compare_roles.Mutation,
dates_delta.Mutation,
]
30 changes: 30 additions & 0 deletions example/mutations/compare_roles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from enum import IntEnum

from ariadne_graphql_modules import GraphQLObject
from graphql import GraphQLResolveInfo

from ..enums.role import RoleEnum


class RoleDiff(IntEnum):
EQUAL = 0
A_GREATER = 1
A_LOWER = 2


class Mutation(GraphQLObject):
@GraphQLObject.field(name="compareRoles")
@staticmethod
def resolveCompareRoles(
obj, info: GraphQLResolveInfo, *, a: RoleEnum, b: RoleEnum
) -> RoleDiff:
index_a = RoleEnum.__members__.index(a)
index_b = RoleEnum.__members__.index(b)

if index_a == index_b:
return RoleDiff.EQUAL

if index_a < index_b:
return RoleDiff.A_GREATER

return RoleDiff.A_LOWER
1 change: 1 addition & 0 deletions example/mutations/dates_delta.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class DatesDeltaType(GraphQLObject):

class Mutation(GraphQLObject):
@GraphQLObject.field(name="datesDelta")
@staticmethod
def resolve_dates_delta(
obj, info: GraphQLResolveInfo, *, a: DateScalar, b: DateScalar
) -> DatesDeltaType:
Expand Down
2 changes: 2 additions & 0 deletions example/types/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from graphql import GraphQLResolveInfo

from ..database import db
from ..enums.role import RoleEnum
from ..models.group import Group
from ..models.post import Post
from ..models.user import User
Expand All @@ -17,6 +18,7 @@ class UserType(GraphQLObject):
id: GraphQLID
username: str
group: Annotated["GroupType", deferred(".group")]
role: RoleEnum
posts: list[PostType]

@GraphQLObject.resolver("group")
Expand Down
14 changes: 13 additions & 1 deletion tests/test_dates_delta_mutation.py → tests/test_mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,19 @@


@pytest.mark.asyncio
async def test_query_hello_field(exec_query):
async def test_query_compare_roles_mutation(exec_query):
result = await exec_query(
"""
mutation {
compareRoles(a: ADMIN, b: MEMBER)
}
"""
)
assert result.data == {"compareRoles": "A_GREATER"}


@pytest.mark.asyncio
async def test_query_dates_delta_mutation(exec_query):
result = await exec_query(
"""
mutation {
Expand Down
9 changes: 7 additions & 2 deletions tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,12 @@ async def test_query_groups_field_member_arg(exec_query):

@pytest.mark.asyncio
async def test_query_user(exec_query):
result = await exec_query('{ user(id: "2") { id username group { name } } }')
result = await exec_query('{ user(id: "2") { id username role group { name } } }')
assert result.data == {
"user": {
"id": "2",
"username": "Alice",
"role": "ADMIN",
"group": {
"name": "Admins",
},
Expand All @@ -83,33 +84,37 @@ async def test_query_user(exec_query):

@pytest.mark.asyncio
async def test_query_users(exec_query):
result = await exec_query("{ users { id username group { name } } }")
result = await exec_query("{ users { id username role group { name } } }")
assert result.data == {
"users": [
{
"id": "1",
"username": "JohnDoe",
"role": "ADMIN",
"group": {
"name": "Admins",
},
},
{
"id": "2",
"username": "Alice",
"role": "ADMIN",
"group": {
"name": "Admins",
},
},
{
"id": "3",
"username": "Bob",
"role": "MEMBER",
"group": {
"name": "Members",
},
},
{
"id": "4",
"username": "Mia",
"role": "GUEST",
"group": {
"name": "Members",
},
Expand Down

0 comments on commit 017fe5a

Please sign in to comment.