ariadne-graphql-modules icon indicating copy to clipboard operation
ariadne-graphql-modules copied to clipboard

Backwards compat with previous version

Open rafalp opened this issue 1 year ago • 0 comments

We need to provide people using previous version of library with a way to move to new version without having to rewrite whole GraphQL API in single commit or change.

I don't like the idea of having the new make_executable_schema support both new and old GraphQL types. I'll rather have a separate compat layer, like a function that would take old types as *args and return a list of those types wrapped in a custom LegacyGraphQLType() instances which in turn would implement __get_graphql_name__, __get_graphql_types__ and __get_graphql_model__ logic that would produce updated API types.

For example:

from typing import List, Type

from ..bases import BindableType
from .base import GraphQLType


def wrap_legacy_types(*bindable_types: Type[BindableType]) -> List[Type["LegacyGraphQLType"]]:
    # Wrap all legacy types in `LegacyGraphQLType` types
    return [
        type(f"Legacy{t.__name__}", (LegacyGraphQLType,) {"__base__": t})
        for t in bindable_types
    ]


class LegacyGraphQLType(GraphQLType):
    __base__: Type[BindableType]

    @classmethod
    def __get_graphql_model__(cls, metadata: GraphQLMetadata) -> "GraphQLModel":
        if issubclass(cls.__base__, ObjectType):
            ... # Construct a model for GraphQLObject

        if issubclass(cls.__base__, InterfaceType):
            ... # Construct a model for GraphQLInterface

Etc. ect. After a grace period of a year or two we could then remove the compat layer logic, remove old API and move contents of next namespace under root namespace.

rafalp avatar Apr 25 '24 10:04 rafalp