How to use the sgqlc.types.__init__.BaseType function in sgqlc

To help you get started, we’ve selected a few sgqlc examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github profusion / sgqlc / sgqlc / types / __init__.py View on Github external
def __init__(cls, name, bases, namespace):
        super(EnumMeta, cls).__init__(name, bases, namespace)
        if not cls.__choices__ and BaseType not in bases:
            raise ValueError(name + ': missing __choices__')

        if isinstance(cls.__choices__, str):
            cls.__choices__ = tuple(cls.__choices__.split())
        else:
            cls.__choices__ = tuple(cls.__choices__)

        for v in cls.__choices__:
            setattr(cls, v, v)
github profusion / sgqlc / sgqlc / types / __init__.py View on Github external
def __to_graphql__(cls, indent=0, indent_string='  '):
        s = [BaseMeta.__to_graphql__(cls, indent, indent_string)]
        prefix = indent_string * (indent + 1)
        for c in cls:
            s.append(prefix + str(c))
        s.append(indent_string * indent + '}')
        return '\n'.join(s)

    def __to_graphql_input__(cls, value, indent=0, indent_string='  '):
        return value

    def __to_json_value__(cls, value):
        return value


class Enum(BaseType, metaclass=EnumMeta):
    '''This is an abstract class that enumerations should inherit
    and define ``__choices__`` class member with a list of strings
    matching the choices allowed by this enumeration. A single string
    may also be used, in such case it will be split using
    ``str.split()``.

    Note that ``__choices__`` is not set in the final class, the
    metaclass will use that to build members and provide the
    ``__iter__``, ``__contains__`` and ``__len__`` instead.

    The instance constructor will never return instance of
    :class:`Enum`, rather the string, if that matches.

    Examples:

    >>> class Colors(Enum):
github profusion / sgqlc / sgqlc / types / __init__.py View on Github external
def __init__(cls, name, bases, namespace):
        super(UnionMeta, cls).__init__(name, bases, namespace)
        if not cls.__types__ and BaseTypeWithTypename not in bases:
            raise ValueError(name + ': missing __types__')

        types = []
        for t in cls.__types__:
            if isinstance(t, str):
                t = cls.__schema__[t]
            else:
                t = BaseType.__ensure__(t)
            types.append(t)

        cls.__types__ = tuple(types)
        cls.__typename_to_type__ = {t.__name__: t for t in types}
github profusion / sgqlc / sgqlc / types / __init__.py View on Github external
elif isinstance(t, Lazy):
        return Lazy(t.target_name, '[' + t.target_name + ']', list_of, t)

    t = BaseType.__ensure__(t)
    name = '[' + t.__name__ + ']'
    try:
        return t.__schema__.__cache__[name]
    except KeyError:
        pass

    wrapper = _create_list_of_wrapper(name, t)
    t.__schema__.__cache__[name] = wrapper
    return wrapper


class Scalar(BaseType):
    '''Basic scalar types, passed thru (no conversion).

    This may be used directly if no special checks or conversions are
    needed. Otherwise use subclasses, like :class:`Int`,
    :class:`Float`, :class:`String`, :class:`Boolean`, :class:`ID`...

    Scalar classes will never produce instance of themselves, rather
    return the converted value (int, bool...)

    >>> class MyTypeWithScalar(Type):
    ...     v = Scalar
    ...
    >>> MyTypeWithScalar({'v': 1}).v
    1
    >>> MyTypeWithScalar({'v': 'abc'}).v
    'abc'
github profusion / sgqlc / sgqlc / types / __init__.py View on Github external
super(BaseMetaWithTypename, cls).__init__(name, bases, namespace)

        if not bases or BaseType in bases or BaseTypeWithTypename in bases:
            return

        cls.__populate_meta_fields()

    def __populate_meta_fields(cls):
        field = Field(non_null('String'), '__typename')
        field._set_container(cls.__schema__, cls, '__typename__')
        cls.__meta_fields__ = {
            '__typename__': field,
        }


class BaseTypeWithTypename(BaseType, metaclass=BaseMetaWithTypename):
    'BaseType with ``__typename`` field (containers and union).'


def _create_non_null_wrapper(name, t):
    'creates type wrapper for non-null of given type'
    def realize_type(v, selection_list=None):
        if isinstance(v, (t, Variable)):
            return v
        return t(v, selection_list)

    def __new__(cls, json_data, selection_list=None):
        if json_data is None:
            raise ValueError(name + ' received null value')
        return realize_type(json_data, selection_list)

    def __to_graphql_input__(value, indent=0, indent_string='  '):