From 017fe5a8d246fd3e5ffba5f01cea78be04e5b6b3 Mon Sep 17 00:00:00 2001 From: rafalp Date: Mon, 7 Oct 2024 22:01:28 +0200 Subject: [PATCH] Add mutation for enum arg and result --- example/enums/role.py | 9 ++++++ example/fixture.py | 4 +++ example/models/user.py | 1 + example/mutations/__init__.py | 2 ++ example/mutations/compare_roles.py | 30 +++++++++++++++++++ example/mutations/dates_delta.py | 1 + example/types/user.py | 2 ++ ...tes_delta_mutation.py => test_mutation.py} | 14 ++++++++- tests/test_query.py | 9 ++++-- 9 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 example/enums/role.py create mode 100644 example/mutations/compare_roles.py rename tests/{test_dates_delta_mutation.py => test_mutation.py} (53%) diff --git a/example/enums/role.py b/example/enums/role.py new file mode 100644 index 0000000..247c77a --- /dev/null +++ b/example/enums/role.py @@ -0,0 +1,9 @@ +from ariadne_graphql_modules import GraphQLEnum + + +class RoleEnum(GraphQLEnum): + __members__ = [ + "ADMIN", + "MEMBER", + "GUEST", + ] diff --git a/example/fixture.py b/example/fixture.py index 001534d..daf796f 100644 --- a/example/fixture.py +++ b/example/fixture.py @@ -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": { diff --git a/example/models/user.py b/example/models/user.py index 5bcfbf4..4660c5e 100644 --- a/example/models/user.py +++ b/example/models/user.py @@ -6,3 +6,4 @@ class User: id: int username: str group_id: int + role: str diff --git a/example/mutations/__init__.py b/example/mutations/__init__.py index a445ad8..ab61dac 100644 --- a/example/mutations/__init__.py +++ b/example/mutations/__init__.py @@ -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, ] diff --git a/example/mutations/compare_roles.py b/example/mutations/compare_roles.py new file mode 100644 index 0000000..aa959ae --- /dev/null +++ b/example/mutations/compare_roles.py @@ -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 diff --git a/example/mutations/dates_delta.py b/example/mutations/dates_delta.py index e93c783..05141f2 100644 --- a/example/mutations/dates_delta.py +++ b/example/mutations/dates_delta.py @@ -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: diff --git a/example/types/user.py b/example/types/user.py index 0976d42..10b5db2 100644 --- a/example/types/user.py +++ b/example/types/user.py @@ -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 @@ -17,6 +18,7 @@ class UserType(GraphQLObject): id: GraphQLID username: str group: Annotated["GroupType", deferred(".group")] + role: RoleEnum posts: list[PostType] @GraphQLObject.resolver("group") diff --git a/tests/test_dates_delta_mutation.py b/tests/test_mutation.py similarity index 53% rename from tests/test_dates_delta_mutation.py rename to tests/test_mutation.py index 2eb1153..b410a22 100644 --- a/tests/test_dates_delta_mutation.py +++ b/tests/test_mutation.py @@ -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 { diff --git a/tests/test_query.py b/tests/test_query.py index d348f34..e28c2be 100644 --- a/tests/test_query.py +++ b/tests/test_query.py @@ -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", }, @@ -83,12 +84,13 @@ 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", }, @@ -96,6 +98,7 @@ async def test_query_users(exec_query): { "id": "2", "username": "Alice", + "role": "ADMIN", "group": { "name": "Admins", }, @@ -103,6 +106,7 @@ async def test_query_users(exec_query): { "id": "3", "username": "Bob", + "role": "MEMBER", "group": { "name": "Members", }, @@ -110,6 +114,7 @@ async def test_query_users(exec_query): { "id": "4", "username": "Mia", + "role": "GUEST", "group": { "name": "Members", },