-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
151a733
commit 92d44a9
Showing
24 changed files
with
176 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from dataclasses import dataclass | ||
|
||
from graphql import GraphQLSchema, TypeDefinitionNode | ||
|
||
|
||
@dataclass(frozen=True) | ||
class GraphQLModel: | ||
name: str | ||
ast: TypeDefinitionNode | ||
ast_type: type[TypeDefinitionNode] | ||
|
||
def bind_to_schema(self, schema: GraphQLSchema): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
from collections.abc import Iterable | ||
from enum import Enum | ||
from typing import Any, Optional, cast | ||
|
||
from graphql import EnumTypeDefinitionNode, EnumValueDefinitionNode, NameNode | ||
|
||
from ariadne_graphql_modules.description import get_description_node | ||
from ariadne_graphql_modules.enum_type.models import GraphQLEnumModel | ||
|
||
|
||
def create_graphql_enum_model( # noqa: C901 | ||
enum: type[Enum], | ||
*, | ||
name: Optional[str] = None, | ||
description: Optional[str] = None, | ||
members_descriptions: Optional[dict[str, str]] = None, | ||
members_include: Optional[Iterable[str]] = None, | ||
members_exclude: Optional[Iterable[str]] = None, | ||
) -> "GraphQLEnumModel": | ||
if members_include and members_exclude: | ||
raise ValueError( | ||
"'members_include' and 'members_exclude' options are mutually exclusive." | ||
) | ||
|
||
if hasattr(enum, "__get_graphql_model__"): | ||
return cast(GraphQLEnumModel, enum.__get_graphql_model__()) | ||
|
||
if not name: | ||
if hasattr(enum, "__get_graphql_name__"): | ||
name = cast("str", enum.__get_graphql_name__()) | ||
else: | ||
name = enum.__name__ | ||
|
||
members: dict[str, Any] = {i.name: i for i in enum} | ||
final_members: dict[str, Any] = {} | ||
|
||
if members_include: | ||
for key, value in members.items(): | ||
if key in members_include: | ||
final_members[key] = value | ||
elif members_exclude: | ||
for key, value in members.items(): | ||
if key not in members_exclude: | ||
final_members[key] = value | ||
else: | ||
final_members = members | ||
|
||
members_descriptions = members_descriptions or {} | ||
for member in members_descriptions: | ||
if member not in final_members: | ||
raise ValueError( | ||
f"Member description was specified for a member '{member}' " | ||
"not present in final GraphQL enum." | ||
) | ||
|
||
return GraphQLEnumModel( | ||
name=name, | ||
members=final_members, | ||
ast_type=EnumTypeDefinitionNode, | ||
ast=EnumTypeDefinitionNode( | ||
name=NameNode(value=name), | ||
description=get_description_node(description), | ||
values=tuple( | ||
EnumValueDefinitionNode( | ||
name=NameNode(value=value_name), | ||
description=get_description_node( | ||
members_descriptions.get(value_name) | ||
), | ||
) | ||
for value_name in final_members | ||
), | ||
), | ||
) | ||
|
||
|
||
def graphql_enum( | ||
cls=None, | ||
*, | ||
name: Optional[str] = None, | ||
description: Optional[str] = None, | ||
members_descriptions: Optional[dict[str, str]] = None, | ||
members_include: Optional[Iterable[str]] = None, | ||
members_exclude: Optional[Iterable[str]] = None, | ||
): | ||
def graphql_enum_decorator(cls): | ||
graphql_model = create_graphql_enum_model( | ||
cls, | ||
name=name, | ||
description=description, | ||
members_descriptions=members_descriptions, | ||
members_include=members_include, | ||
members_exclude=members_exclude, | ||
) | ||
|
||
def __get_graphql_model__(*_) -> GraphQLEnumModel: # noqa: N807 | ||
return graphql_model | ||
|
||
setattr(cls, "__get_graphql_model__", classmethod(__get_graphql_model__)) | ||
return cls | ||
|
||
if cls: | ||
return graphql_enum_decorator(cls) | ||
|
||
return graphql_enum_decorator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.