Skip to content

Commit

Permalink
Add input type
Browse files Browse the repository at this point in the history
  • Loading branch information
rafalp committed Oct 9, 2024
1 parent 9866dab commit 45b3ac6
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
8 changes: 6 additions & 2 deletions example/mutations/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
from typing import Any

from . import compare_roles
from . import dates_delta
from . import (
calc,
compare_roles,
dates_delta,
)

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

from ariadne_graphql_modules import GraphQLInput, GraphQLObject
from graphql import GraphQLResolveInfo

from ..scalars.date import DateScalar


class CalcOperation(StrEnum):
ADD = "add"
SUB = "sub"
MUL = "MUL"


class CalcInput(GraphQLInput):
a: int
b: int
op: CalcOperation


class Mutation(GraphQLObject):
@GraphQLObject.field(name="calc")
@staticmethod
def resolve_calc(obj, info: GraphQLResolveInfo, *, input: CalcInput) -> int:
if input.op == CalcOperation.ADD:
return input.a + input.b
if input.op == CalcOperation.SUB:
return input.a - input.b
if input.op == CalcOperation.MUL:
return input.a * input.b

return 0
12 changes: 12 additions & 0 deletions tests/test_mutation.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
import pytest


@pytest.mark.asyncio
async def test_query_calc_mutation(exec_query):
result = await exec_query(
"""
mutation {
calc(input: {a: 4, b: 3, op: MUL})
}
"""
)
assert result.data == {"calc": 12}


@pytest.mark.asyncio
async def test_query_compare_roles_mutation(exec_query):
result = await exec_query(
Expand Down

0 comments on commit 45b3ac6

Please sign in to comment.