Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add-compatibility-layer #44

Merged
merged 2 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion ariadne_graphql_modules/bases.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from typing import List, Type, Union

from graphql import DefinitionNode, GraphQLSchema, ObjectTypeDefinitionNode
from graphql import (
DefinitionNode,
GraphQLSchema,
ObjectTypeDefinitionNode,
TypeSystemDefinitionNode,
)

from .dependencies import Dependencies
from .types import RequirementsDict
Expand Down Expand Up @@ -31,6 +36,7 @@ class DefinitionType(BaseType):

graphql_name: str
graphql_type: Type[DefinitionNode]
graphql_def: TypeSystemDefinitionNode

@classmethod
def __get_requirements__(cls) -> RequirementsDict:
Expand Down
12 changes: 7 additions & 5 deletions ariadne_graphql_modules/enum_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,19 @@ def __init_subclass__(cls) -> None:

cls.__abstract__ = False

graphql_def = cls.__validate_schema__(
cls.graphql_def = cls.__validate_schema__(
parse_definition(cls.__name__, cls.__schema__)
)

cls.graphql_name = graphql_def.name.value
cls.graphql_type = type(graphql_def)
cls.graphql_name = cls.graphql_def.name.value
cls.graphql_type = type(cls.graphql_def)

requirements = cls.__get_requirements__()
cls.__validate_requirements_contain_extended_type__(graphql_def, requirements)
cls.__validate_requirements_contain_extended_type__(
cls.graphql_def, requirements
)

values = cls.__get_values__(graphql_def)
values = cls.__get_values__(cls.graphql_def)
cls.__validate_values__(values)

@classmethod
Expand Down
15 changes: 9 additions & 6 deletions ariadne_graphql_modules/input_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class InputType(BindableType):
__args__: Optional[Union[Args, Callable[..., Args]]] = None

graphql_fields: InputFieldsDict
graphql_def: InputNodeType

def __init_subclass__(cls) -> None:
super().__init_subclass__()
Expand All @@ -29,13 +30,13 @@ def __init_subclass__(cls) -> None:

cls.__abstract__ = False

graphql_def = cls.__validate_schema__(
cls.graphql_def = cls.__validate_schema__(
parse_definition(cls.__name__, cls.__schema__)
)

cls.graphql_name = graphql_def.name.value
cls.graphql_type = type(graphql_def)
cls.graphql_fields = cls.__get_fields__(graphql_def)
cls.graphql_name = cls.graphql_def.name.value
cls.graphql_type = type(cls.graphql_def)
cls.graphql_fields = cls.__get_fields__(cls.graphql_def)

if callable(cls.__args__):
# pylint: disable=not-callable
Expand All @@ -44,9 +45,11 @@ def __init_subclass__(cls) -> None:
cls.__validate_args__()

requirements = cls.__get_requirements__()
cls.__validate_requirements_contain_extended_type__(graphql_def, requirements)
cls.__validate_requirements_contain_extended_type__(
cls.graphql_def, requirements
)

dependencies = cls.__get_dependencies__(graphql_def)
dependencies = cls.__get_dependencies__(cls.graphql_def)
cls.__validate_requirements__(requirements, dependencies)

@classmethod
Expand Down
14 changes: 8 additions & 6 deletions ariadne_graphql_modules/interface_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,20 @@ def __init_subclass__(cls) -> None:

cls.__abstract__ = False

graphql_def = cls.__validate_schema__(
cls.graphql_def = cls.__validate_schema__(
parse_definition(cls.__name__, cls.__schema__)
)

cls.graphql_name = graphql_def.name.value
cls.graphql_type = type(graphql_def)
cls.graphql_fields = cls.__get_fields__(graphql_def)
cls.graphql_name = cls.graphql_def.name.value
cls.graphql_type = type(cls.graphql_def)
cls.graphql_fields = cls.__get_fields__(cls.graphql_def)

requirements = cls.__get_requirements__()
cls.__validate_requirements_contain_extended_type__(graphql_def, requirements)
cls.__validate_requirements_contain_extended_type__(
cls.graphql_def, requirements
)

dependencies = cls.__get_dependencies__(graphql_def)
dependencies = cls.__get_dependencies__(cls.graphql_def)
cls.__validate_requirements__(requirements, dependencies)

if callable(cls.__fields_args__):
Expand Down
14 changes: 8 additions & 6 deletions ariadne_graphql_modules/mutation_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,22 @@ def __init_subclass__(cls) -> None:

cls.__abstract__ = False

graphql_def = cls.__validate_schema__(
cls.graphql_def = cls.__validate_schema__(
parse_definition(cls.__name__, cls.__schema__)
)

cls.graphql_name = graphql_def.name.value
cls.graphql_type = type(graphql_def)
cls.graphql_name = cls.graphql_def.name.value
cls.graphql_type = type(cls.graphql_def)

field = cls.__get_field__(graphql_def)
field = cls.__get_field__(cls.graphql_def)
cls.mutation_name = field.name.value

requirements = cls.__get_requirements__()
cls.__validate_requirements_contain_extended_type__(graphql_def, requirements)
cls.__validate_requirements_contain_extended_type__(
cls.graphql_def, requirements
)

dependencies = cls.__get_dependencies__(graphql_def)
dependencies = cls.__get_dependencies__(cls.graphql_def)
cls.__validate_requirements__(requirements, dependencies)

if callable(cls.__args__):
Expand Down
168 changes: 168 additions & 0 deletions ariadne_graphql_modules/next/compatibility_layer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
from enum import Enum
from inspect import isclass
from typing import Any, Dict, List, Type, Union, cast

from graphql import (
EnumTypeDefinitionNode,
InputObjectTypeDefinitionNode,
InterfaceTypeDefinitionNode,
ObjectTypeDefinitionNode,
ScalarTypeDefinitionNode,
TypeExtensionNode,
UnionTypeDefinitionNode,
)

from ..executable_schema import get_all_types

from ..directive_type import DirectiveType
from ..enum_type import EnumType
from ..input_type import InputType
from ..interface_type import InterfaceType
from ..mutation_type import MutationType
from ..scalar_type import ScalarType
from ..subscription_type import SubscriptionType
from ..union_type import UnionType

from ..object_type import ObjectType

from ..bases import BindableType
from .base import GraphQLModel, GraphQLType
from . import (
GraphQLObjectModel,
GraphQLEnumModel,
GraphQLInputModel,
GraphQLScalarModel,
GraphQLInterfaceModel,
GraphQLSubscriptionModel,
GraphQLUnionModel,
)


def wrap_legacy_types(
*bindable_types: Type[BindableType],
) -> List[Type["LegacyGraphQLType"]]:
all_types = get_all_types(bindable_types)

return [
type(f"Legacy{t.__name__}", (LegacyGraphQLType,), {"__base_type__": t})
for t in all_types
]


class LegacyGraphQLType(GraphQLType):
__base_type__: Type[BindableType]
__abstract__: bool = False

@classmethod
def __get_graphql_model__(cls, *_) -> GraphQLModel:
if issubclass(cls.__base_type__.graphql_type, TypeExtensionNode):
pass
if issubclass(cls.__base_type__, ObjectType):
return cls.construct_object_model(cls.__base_type__)
if issubclass(cls.__base_type__, EnumType):
return cls.construct_enum_model(cls.__base_type__)
if issubclass(cls.__base_type__, InputType):
return cls.construct_input_model(cls.__base_type__)
if issubclass(cls.__base_type__, InterfaceType):
return cls.construct_interface_model(cls.__base_type__)
if issubclass(cls.__base_type__, MutationType):
return cls.construct_object_model(cls.__base_type__)
if issubclass(cls.__base_type__, ScalarType):
return cls.construct_scalar_model(cls.__base_type__)
if issubclass(cls.__base_type__, SubscriptionType):
return cls.construct_subscription_model(cls.__base_type__)
if issubclass(cls.__base_type__, UnionType):
return cls.construct_union_model(cls.__base_type__)
raise ValueError(f"Unsupported base_type {cls.__base_type__}")

@classmethod
def construct_object_model(
cls, base_type: Type[Union[ObjectType, MutationType]]
) -> GraphQLObjectModel:
return GraphQLObjectModel(
name=base_type.graphql_name,
ast_type=ObjectTypeDefinitionNode,
ast=cast(ObjectTypeDefinitionNode, base_type.graphql_def),
resolvers=base_type.resolvers, # type: ignore
aliases=base_type.__aliases__ or {}, # type: ignore
out_names={},
)

@classmethod
def construct_enum_model(cls, base_type: Type[EnumType]) -> GraphQLEnumModel:
members = base_type.__enum__ or {}
members_values: Dict[str, Any] = {}

if isinstance(members, dict):
members_values = dict(members.items())
elif isclass(members) and issubclass(members, Enum):
members_values = {member.name: member for member in members}

return GraphQLEnumModel(
name=base_type.graphql_name,
members=members_values,
ast_type=EnumTypeDefinitionNode,
ast=cast(EnumTypeDefinitionNode, base_type.graphql_def),
)

@classmethod
def construct_directive_model(cls, base_type: Type[DirectiveType]):
"""TODO: https://github.com/mirumee/ariadne-graphql-modules/issues/29"""

@classmethod
def construct_input_model(cls, base_type: Type[InputType]) -> GraphQLInputModel:
return GraphQLInputModel(
name=base_type.graphql_name,
ast_type=InputObjectTypeDefinitionNode,
ast=cast(InputObjectTypeDefinitionNode, base_type.graphql_def),
out_type=base_type.graphql_type,
out_names={},
)

@classmethod
def construct_interface_model(
cls, base_type: Type[InterfaceType]
) -> GraphQLInterfaceModel:
return GraphQLInterfaceModel(
name=base_type.graphql_name,
ast_type=InterfaceTypeDefinitionNode,
ast=cast(InterfaceTypeDefinitionNode, base_type.graphql_def),
resolve_type=base_type.resolve_type,
resolvers=base_type.resolvers,
out_names={},
aliases=base_type.__aliases__ or {}, # type: ignore
)

@classmethod
def construct_scalar_model(cls, base_type: Type[ScalarType]) -> GraphQLScalarModel:
return GraphQLScalarModel(
name=base_type.graphql_name,
ast_type=ScalarTypeDefinitionNode,
ast=cast(ScalarTypeDefinitionNode, base_type.graphql_def),
serialize=base_type.serialize,
parse_value=base_type.parse_value,
parse_literal=base_type.parse_literal,
)

@classmethod
def construct_subscription_model(
cls, base_type: Type[SubscriptionType]
) -> GraphQLSubscriptionModel:
return GraphQLSubscriptionModel(
name=base_type.graphql_name,
ast_type=ObjectTypeDefinitionNode,
ast=cast(ObjectTypeDefinitionNode, base_type.graphql_def),
resolvers=base_type.resolvers,
aliases=base_type.__aliases__ or {}, # type: ignore
out_names={},
subscribers=base_type.subscribers,
)

@classmethod
def construct_union_model(cls, base_type: Type[UnionType]) -> GraphQLUnionModel:
return GraphQLUnionModel(
name=base_type.graphql_name,
ast_type=UnionTypeDefinitionNode,
ast=cast(UnionTypeDefinitionNode, base_type.graphql_def),
resolve_type=base_type.resolve_type,
)
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
from dataclasses import dataclass
from typing import Any, Callable, Dict, cast
from typing import Dict, cast

from ariadne import InterfaceType
from ariadne.types import Resolver
from graphql import GraphQLField, GraphQLObjectType, GraphQLSchema
from graphql import GraphQLField, GraphQLObjectType, GraphQLSchema, GraphQLTypeResolver

from ..base import GraphQLModel


@dataclass(frozen=True)
class GraphQLInterfaceModel(GraphQLModel):
resolvers: Dict[str, Resolver]
resolve_type: Callable[[Any], Any]
resolve_type: GraphQLTypeResolver
out_names: Dict[str, Dict[str, str]]
aliases: Dict[str, str]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def __get_graphql_model_without_schema__(

@staticmethod
def resolve_type(obj: Any, *_) -> str:
if isinstance(obj, GraphQLInterface):
if isinstance(obj, GraphQLObject):
return obj.__get_graphql_name__()

raise ValueError(
Expand Down
15 changes: 10 additions & 5 deletions ariadne_graphql_modules/next/graphql_scalar/scalar_model.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
from dataclasses import dataclass
from typing import Any, Callable, Dict, Optional
from typing import Optional

from graphql import GraphQLSchema, ValueNode
from graphql import (
GraphQLScalarLiteralParser,
GraphQLScalarSerializer,
GraphQLScalarValueParser,
GraphQLSchema,
)
from ariadne import ScalarType as ScalarTypeBindable
from ..base import GraphQLModel


@dataclass(frozen=True)
class GraphQLScalarModel(GraphQLModel):
serialize: Callable[[Any], Any]
parse_value: Callable[[Any], Any]
parse_literal: Callable[[ValueNode, Optional[Dict[str, Any]]], Any]
serialize: Optional[GraphQLScalarSerializer]
parse_value: Optional[GraphQLScalarValueParser]
parse_literal: Optional[GraphQLScalarLiteralParser]

def bind_to_schema(self, schema: GraphQLSchema):
bindable = ScalarTypeBindable(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from dataclasses import dataclass
from typing import Any, Callable, Dict, cast
from typing import Dict, cast

from ariadne import SubscriptionType
from ariadne.types import Resolver, Subscriber
Expand All @@ -11,7 +11,6 @@
@dataclass(frozen=True)
class GraphQLSubscriptionModel(GraphQLModel):
resolvers: Dict[str, Resolver]
resolve_type: Callable[[Any], Any]
out_names: Dict[str, Dict[str, str]]
aliases: Dict[str, str]
subscribers: Dict[str, Subscriber]
Expand Down
Loading
Loading