Add more examples to enums doc
@@ -3,11 +3,11 @@ Enumeration types
Ariadne supports `enumeration types <https://graphql.org/learn/schema/#enumeration-types>`_, which are represented as strings in Python logic::
- from db import get_users_ids
+ from db import get_users
type_defs = """
type Query{
- users(status: UserStatus): [ID!]!
+ users(status: UserStatus): [User]!
}
enum UserStatus{
@@ -20,11 +20,11 @@ Ariadne supports `enumeration types <https://graphql.org/learn/schema/#enumerati
def resolve_users(*_, status):
if status == "ACTIVE":
- return get_users_ids(is_active=True)
+ return get_users(is_active=True)
if status == "INACTIVE":
- return get_users_ids(is_active=False)
+ return get_users(is_active=False)
if status == "BANNED":
- return get_users_ids(is_banned=True)
+ return get_users(is_banned=True)
resolvers = {
@@ -32,3 +32,30 @@ Ariadne supports `enumeration types <https://graphql.org/learn/schema/#enumerati
"users": resolve_users,
}
}
+
+Above example defines resolver that returns list of users based on user status, defined using ``UserStatus`` enumerable from schema.
+
+Implementing logic validating if ``status`` value is allowed is not required - this is done on GraphQL level. This query will produce error::
+
+ {
+ users(status: TEST)
+ }
+
+GraphQL failed to find ``TEST`` in ``UserStatus``, and returned error without calling ``resolve_users``::
+
+ {
+ "error": {
+ "errors": [
+ {
+ "message": "Argument \"status\" has invalid value TEST.\nExpected type \"UserStatus\", found TEST.",
+ "locations": [
+ {
+ "line": 2,
+ "column": 14
+ }
+ ]
+ }
+ ]
+ }
+ }
+
\ No newline at end of file
