File: uuid.py

package info (click to toggle)
python-graphene 2.1.9-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,024 kB
  • sloc: python: 7,295; makefile: 196; sh: 4
file content (33 lines) | stat: -rw-r--r-- 785 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from __future__ import absolute_import
import six
from uuid import UUID as _UUID

from graphql.language import ast

from .scalars import Scalar


class UUID(Scalar):
    """
    Leverages the internal Python implmeentation of UUID (uuid.UUID) to provide native UUID objects
    in fields, resolvers and input.
    """

    @staticmethod
    def serialize(uuid):
        if isinstance(uuid, six.string_types):
            uuid = _UUID(uuid)

        assert isinstance(uuid, _UUID), "Expected UUID instance, received {}".format(
            uuid
        )
        return str(uuid)

    @staticmethod
    def parse_literal(node):
        if isinstance(node, ast.StringValue):
            return _UUID(node.value)

    @staticmethod
    def parse_value(value):
        return _UUID(value)