Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

Network representation support

オープン
#1,351 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
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 の 6 つが一貫してエクスポートされ、説明されているパースおよびシリアライズの動作をサポートすれば完了です。

索引モデルが 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. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

graphql-python/graphene のほかの issue

graphql-python/graphene の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。