graphene
graphene copied to clipboard
Network representation support
It will be interesting to introduce all network representation for multiple purpose (represents ip address with an easy way, networks address).
I wrote some scalar compatible and tested with graphene v3
There is the code to change in graphene/types/init.py:
# flake8: noqa
from graphql import GraphQLResolveInfo as ResolveInfo
from .argument import Argument
from .base64 import Base64
from .context import Context
from .datetime import Date, DateTime, Time
from .decimal import Decimal
from .dynamic import Dynamic
from .enum import Enum
from .field import Field
from .inputfield import InputField
from .inputobjecttype import InputObjectType
from .interface import Interface
from .json import JSONString
from .mutation import Mutation
from .network import (
IPv4Address,
IPv6Address,
IPvAnyAddress,
IPv4Network,
IPv6Network,
IPvAnyNetwork,
)
from .objecttype import ObjectType
from .scalars import ID, Boolean, Float, Int, Scalar, String
from .schema import Schema
from .structures import List, NonNull
from .union import Union
from .uuid import UUID
__all__ = [
"Argument",
"Base64",
"Boolean",
"Context",
"Date",
"DateTime",
"Decimal",
"Dynamic",
"Enum",
"Field",
"Float",
"ID",
"InputField",
"InputObjectType",
"Int",
"Interface",
"IPv4Address",
"IPv6Address",
"IPvAnyAddress",
"IPv4Network",
"IPv6Network",
"IPvAnyNetwork",
"JSONString",
"List",
"Mutation",
"NonNull",
"ObjectType",
"ResolveInfo",
"Scalar",
"Schema",
"String",
"Time",
"UUID",
"Union",
]
and the new graphene/types/network.py file:
from .scalars import Scalar
from graphql.error import GraphQLError
import ipaddress
from graphql.language import ast, print_ast
class IPv4Address(Scalar):
"""
The 'IPv4Address' scalar type represents an IPv4 Address
value as specified by
[RFC 6864](https://datatracker.ietf.org/doc/html/rfc6864).
"""
@staticmethod
def serialize(ip):
if isinstance(ip, str):
ip = ipaddress.IPv4Address(ip)
if not isinstance(ip, ipaddress.IPv4Address):
raise GraphQLError(f"IPv4Address cannot represent value: {repr(ip)}")
return str(ipaddress.IPv4Address(ip))
@classmethod
def parse_literal(cls, node):
if not isinstance(node, ast.StringValueNode):
raise GraphQLError(
f"IPv4Address cannot represent non-string value: {print_ast(node)}"
)
return cls.parse_value(node.value)
@staticmethod
def parse_value(value):
if isinstance(value, ipaddress.IPv4Address):
return value
if not isinstance(value, str):
raise GraphQLError(
f"IPv4Address cannot represent non-string value: {repr(value)}"
)
try:
return ipaddress.IPv4Address(value)
except ValueError as v:
raise GraphQLError(f"{v}")
except Exception:
raise GraphQLError(f"IPv4Address cannot represent value: {repr(value)}")
class IPv6Address(Scalar):
"""
The 'IPv6Address' scalar type represents an IPv6 Address
value as specified by
[RFC 4291](https://datatracker.ietf.org/doc/html/rfc4291.html).
"""
@staticmethod
def serialize(ip):
if isinstance(ip, str):
ip = ipaddress.IPv6Address(ip)
if not isinstance(ip, ipaddress.IPv6Address):
raise GraphQLError(f"IPv6Address cannot represent value: {repr(ip)}")
return str(ipaddress.IPv6Address(ip))
@classmethod
def parse_literal(cls, node):
if not isinstance(node, ast.StringValueNode):
raise GraphQLError(
f"IPv6Address cannot represent non-string value: {print_ast(node)}"
)
return cls.parse_value(node.value)
@staticmethod
def parse_value(value):
if isinstance(value, ipaddress.IPv6Address):
return value
if not isinstance(value, str):
raise GraphQLError(
f"IPv6Address cannot represent non-string value: {repr(value)}"
)
try:
return ipaddress.IPv6Address(value)
except ValueError as v:
raise GraphQLError(f"{v}")
except Exception:
raise GraphQLError(f"IPv6Address cannot represent value: {repr(value)}")
class IPvAnyAddress(Scalar):
"""
The 'IPvAnyAddress' scalar type represents an IPv4 Address or IPv6 Address
value as specified by
[RFC 6864](https://datatracker.ietf.org/doc/html/rfc6864) for IPv4 and
[RFC 4291](https://datatracker.ietf.org/doc/html/rfc4291.html) for IPv6.
"""
@staticmethod
def serialize(ip):
if isinstance(ip, str):
ip = ipaddress.ip_address(ip)
if not isinstance(ip, ipaddress.IPv4Address) and not isinstance(
ip, ipaddress.IPv6Address
):
raise GraphQLError(f"IPvAnyAddress cannot represent value: {repr(ip)}")
return str(ipaddress.ip_address(ip))
@classmethod
def parse_literal(cls, node):
if not isinstance(node, ast.StringValueNode):
raise GraphQLError(
f"IPvAnyAddress cannot represent non-string value: {print_ast(node)}"
)
return cls.parse_value(node.value)
@staticmethod
def parse_value(value):
if isinstance(value, ipaddress.IPv4Address) or isinstance(
value, ipaddress.IPv6Address
):
return value
if not isinstance(value, str):
raise GraphQLError(
f"IPvAnyAddress cannot represent non-string value: {repr(value)}"
)
try:
return ipaddress.ip_address(value)
except ValueError as v:
raise GraphQLError(f"{v}")
except Exception:
raise GraphQLError(f"IPvAnyAddress cannot represent value: {repr(value)}")
class IPv4Network(Scalar):
"""
The 'IPv4Network' scalar type represents an IPv4 Network
value as specified by
[RFC 6864](https://datatracker.ietf.org/doc/html/rfc6864).
"""
@staticmethod
def serialize(ip):
if isinstance(ip, str):
ip = ipaddress.IPv4Network(ip)
if not isinstance(ip, ipaddress.IPv4Network):
raise GraphQLError(f"IPv4Network cannot represent value: {repr(ip)}")
return str(ipaddress.IPv4Network(ip))
@classmethod
def parse_literal(cls, node):
if not isinstance(node, ast.StringValueNode):
raise GraphQLError(
f"IPv4Network cannot represent non-string value: {print_ast(node)}"
)
return cls.parse_value(node.value)
@staticmethod
def parse_value(value):
if isinstance(value, ipaddress.IPv4Network):
return value
if not isinstance(value, str):
raise GraphQLError(
f"IPv4Network cannot represent non-string value: {repr(value)}"
)
try:
return ipaddress.IPv4Network(value)
except ValueError as v:
raise GraphQLError(f"{v}")
except Exception:
raise GraphQLError(f"IPv4Network cannot represent value: {repr(value)}")
class IPv6Network(Scalar):
"""
The 'IPv6Network' scalar type represents an IPv6 Network
value as specified by
[RFC 4291](https://datatracker.ietf.org/doc/html/rfc4291.html).
"""
@staticmethod
def serialize(ip):
if isinstance(ip, str):
ip = ipaddress.IPv6Network(ip)
if not isinstance(ip, ipaddress.IPv6Network):
raise GraphQLError(f"IPv6Network cannot represent value: {repr(ip)}")
return str(ipaddress.IPv6Network(ip))
@classmethod
def parse_literal(cls, node):
if not isinstance(node, ast.StringValueNode):
raise GraphQLError(
f"IPv6Network cannot represent non-string value: {print_ast(node)}"
)
return cls.parse_value(node.value)
@staticmethod
def parse_value(value):
if isinstance(value, ipaddress.IPv6Network):
return value
if not isinstance(value, str):
raise GraphQLError(
f"IPv6Network cannot represent non-string value: {repr(value)}"
)
try:
return ipaddress.IPv6Network(value)
except ValueError as v:
raise GraphQLError(f"{v}")
except Exception:
raise GraphQLError(f"IPv6Network cannot represent value: {repr(value)}")
class IPvAnyNetwork(Scalar):
"""
The 'IPvAnyNetwork' scalar type represents an IPv4 Network or IPv6 Network
value as specified by
[RFC 6864](https://datatracker.ietf.org/doc/html/rfc6864) for IPv4 and
[RFC 4291](https://datatracker.ietf.org/doc/html/rfc4291.html) for IPv6.
"""
@staticmethod
def serialize(ip):
if isinstance(ip, str):
ip = ipaddress.ip_network(ip)
if not isinstance(ip, ipaddress.IPv4Network) and not isinstance(
ip, ipaddress.IPv6Network
):
raise GraphQLError(f"IPvAnyNetwork cannot represent value: {repr(ip)}")
return str(ipaddress.ip_network(ip))
@classmethod
def parse_literal(cls, node):
if not isinstance(node, ast.StringValueNode):
raise GraphQLError(
f"IPvAnyNetwork cannot represent non-string value: {print_ast(node)}"
)
return cls.parse_value(node.value)
@staticmethod
def parse_value(value):
if isinstance(value, ipaddress.IPv4Network) or isinstance(
value, ipaddress.IPv6Network
):
return value
if not isinstance(value, str):
raise GraphQLError(
f"IPvAnyNetwork cannot represent non-string value: {repr(value)}"
)
try:
return ipaddress.ip_network(value)
except ValueError as v:
raise GraphQLError(f"{v}")
except Exception:
raise GraphQLError(f"IPvAnyNetwork cannot represent value: {repr(value)}")
Already formatted by black obviously.
I work with network ingineer an a project using graphene v3 and I have to represents network devices with their facts, I needed this feature so I developed it but it will be interesting to integrate it directly into graphene i think.
Thanks in advance.
As this type is not included in the official list of scalar types, it is considered an extension type. I would be more inclined to leave it out of this library. You can still use it for your own needs though without including it in this library as mentioned in the docs:
https://docs.graphene-python.org/en/latest/types/scalars/#custom-scalars https://spec.graphql.org/June2018/#sec-Scalar-Extensions