pygraphy icon indicating copy to clipboard operation
pygraphy copied to clipboard

Add support for custom scalars

Open Ambro17 opened this issue 5 years ago • 4 comments

Ambro17 avatar Jul 13 '20 23:07 Ambro17

In my opinion, there are several objectives we need to implement:

  • [ ] Allow user to declare scalar types
  • [ ] custom scalar types can be printed as a schema: https://graphql.org/learn/schema/#scalar-types
  • [ ] Allow client transfer string values and load them from string to Python types (Datetime, UUID, etc.) which declared in scalar types

ethe avatar Jul 15 '20 08:07 ethe

Maybe we should create a new metaclass called ScalarType and a root class called Scalar, then users could create their custom scalar types as objects of this metaclass by inheriting Scalar. There are some use cases:

from pygraphy import ScalarType

class Scalar(metaclass=ScalarType):
    # defined in pygraphy, implement some default method
    ...

class UUID(Scalar):
    def load(self, string: str) -> pyUUID:
        return pyUUID(string)

    def dump(self) -> str:
        return str(self.value)  # value is defined in `Scalar` class maybe

then UUID can be used as a normal type when declaring models:

class Foo(pygraphy.Object):
    id: UUID

ethe avatar Jul 15 '20 08:07 ethe

Should I solve the first three items on this issue?

Ambro17 avatar Jul 19 '20 13:07 Ambro17

Yeah, I am glad to hear that.

ethe avatar Jul 20 '20 07:07 ethe