Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Network representation support

未关闭
#1,351 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
35/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
停滞
技术栈
graphql, python
领域
api, backend

调研方向

首先查看 graphene/types/init.py 中的 scalar 模式以及提议的 graphene/types/network.py 实现,并注意 Graphene 与 GraphQL v3 的兼容性。当 IPv4、IPv6 和任意版本的六个地址与网络 scalar 都能一致地导出,并支持所描述的解析和序列化行为时,即视为完成。

由索引模型根据 Issue 内容生成。

描述

✨ enhancement

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

贡献指南

这个仓库没有索引到贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

graphql-python/graphene 的其他 Issue

查看 graphql-python/graphene 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。