Network representation support
还没有人认领这个 Issue。
评估
调研方向
首先查看 graphene/types/init.py 中的 scalar 模式以及提议的 graphene/types/network.py 实现,并注意 Graphene 与 GraphQL v3 的兼容性。当 IPv4、IPv6 和任意版本的六个地址与网络 scalar 都能一致地导出,并支持所描述的解析和序列化行为时,即视为完成。
由索引模型根据 Issue 内容生成。
描述
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.
- 主要语言
- Python
- 星标
- 8.2k
- 派生
- 818
- PR 合并指标
- 30 天内没有已合并 PR
贡献指南
这个仓库没有索引到贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
graphql-python/graphene 的其他 Issue
-
🐛 bug
难度 2/5 1-3 小时 新手友好度 72/100
graphql-python/graphene#1389 · 5 条评论 · 2 个 reaction ·
-
难度 4/5 3-5 天 新手友好度 68/100
graphql-python/graphene#1606 ·
-
✨ enhancement
难度 2/5 1-3 小时 新手友好度 38/100
graphql-python/graphene#1601 · 2 条评论 ·
-
✨ enhancement
难度 4/5 3-5 天 新手友好度 42/100
graphql-python/graphene#1600 ·
-
🐛 bug
难度 2/5 1-3 小时 新手友好度 55/100
graphql-python/graphene#1593 ·
查看 graphql-python/graphene 的全部 Issue
相似的 Issue
-
agent-ready documentation needs-triage
难度 1/5 1-3 小时 新手友好度 88/100
-
documentation
难度 1/5 1 小时以内 新手友好度 91/100
-
workflow-status page template still says reusable workflows are "triggered only by workflow_call:" 未关闭
难度 1/5 1 小时以内 新手友好度 92/100
-
instance instance add
难度 1/5 1 小时以内 新手友好度 72/100
searxng/searx-instances#939 · 1 条评论 ·
-
area-deployment area-integrations triage:bot-seen
难度 2/5 半天 新手友好度 86/100