mirror of
https://github.com/graphql-python/graphene.git
synced 2025-02-13 10:00:39 +03:00
Simplified Scalar
This commit is contained in:
parent
e487206818
commit
5ee6e2b8e7
|
@ -1,34 +1,17 @@
|
||||||
import six
|
import six
|
||||||
|
|
||||||
from graphql.language.ast import (BooleanValue, FloatValue, IntValue,
|
from graphql.language.ast import (BooleanValue, FloatValue, IntValue,
|
||||||
StringValue)
|
StringValue)
|
||||||
|
|
||||||
from ..utils.is_base_type import is_base_type
|
|
||||||
from ..utils.trim_docstring import trim_docstring
|
|
||||||
from .options import Options
|
|
||||||
from .unmountedtype import UnmountedType
|
from .unmountedtype import UnmountedType
|
||||||
|
from .base import BaseOptions, BaseType
|
||||||
|
|
||||||
|
|
||||||
class ScalarTypeMeta(type):
|
class ScalarOptions(BaseOptions):
|
||||||
|
pass
|
||||||
def __new__(cls, name, bases, attrs):
|
|
||||||
# Also ensure initialization is only performed for subclasses of Model
|
|
||||||
# (excluding Model class itself).
|
|
||||||
if not is_base_type(bases, ScalarTypeMeta):
|
|
||||||
return type.__new__(cls, name, bases, attrs)
|
|
||||||
|
|
||||||
options = Options(
|
|
||||||
attrs.pop('Meta', None),
|
|
||||||
name=name,
|
|
||||||
description=trim_docstring(attrs.get('__doc__')),
|
|
||||||
)
|
|
||||||
|
|
||||||
return type.__new__(cls, name, bases, dict(attrs, _meta=options))
|
|
||||||
|
|
||||||
def __str__(cls): # noqa: N802
|
|
||||||
return cls._meta.name
|
|
||||||
|
|
||||||
|
|
||||||
class Scalar(six.with_metaclass(ScalarTypeMeta, UnmountedType)):
|
class Scalar(UnmountedType, BaseType):
|
||||||
'''
|
'''
|
||||||
Scalar Type Definition
|
Scalar Type Definition
|
||||||
|
|
||||||
|
@ -36,6 +19,10 @@ class Scalar(six.with_metaclass(ScalarTypeMeta, UnmountedType)):
|
||||||
Scalars (or Enums) and are defined with a name and a series of functions
|
Scalars (or Enums) and are defined with a name and a series of functions
|
||||||
used to parse input from ast or variables and to ensure validity.
|
used to parse input from ast or variables and to ensure validity.
|
||||||
'''
|
'''
|
||||||
|
@classmethod
|
||||||
|
def __init_subclass_with_meta__(cls, **options):
|
||||||
|
_meta = ScalarOptions(cls)
|
||||||
|
super(Scalar, cls).__init_subclass_with_meta__(_meta=_meta, **options)
|
||||||
|
|
||||||
serialize = None
|
serialize = None
|
||||||
parse_value = None
|
parse_value = None
|
||||||
|
|
11
graphene/types/tests/test_scalar.py
Normal file
11
graphene/types/tests/test_scalar.py
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from ..scalars import Scalar
|
||||||
|
|
||||||
|
|
||||||
|
def test_scalar():
|
||||||
|
class JSONScalar(Scalar):
|
||||||
|
'''Documentation'''
|
||||||
|
|
||||||
|
assert JSONScalar._meta.name == "JSONScalar"
|
||||||
|
assert JSONScalar._meta.description == "Documentation"
|
Loading…
Reference in New Issue
Block a user