graphql-python / graphql-python/graphene

Enum.from_enum() fails with Django IntegerChoices

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

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

🐛 bug
主要言語
Python
スター
8.2k
フォーク
818
PR マージ指標
30日以内にマージされた PR はありません

説明

Current Behavior

I am working in a Django Python codebase that has a GraphQL schema. A lot of TextChoices are used in our GraphQL schema and, to properly represent these we use the provided graphene.Enum.from_enum() function.

This same approach doesn't seem to work with Django's IntegerChoices. It seems when graphene generates a GrapheneEnumType, the values are created with the IntegerChoices member names. This makes it so the IntegerChoices value cannot be represented with the generated GrapheneEnumType.

This results in an error like the following:
"Enum 'Status' cannot represent value: 2"

Expected Behavior

A value from a Django IntegerChoices object is usable to get the appropriate GrapheneEnumType value name.

motivation

To allow for graphene.Enum.from_enum() to be usable by Django's IntegerChoices class.

Environment

  • Graphene Version: 3.3
  • Graphene Django Version: 3.2.0
  • Graphene Version: 3.3
  • Django Version: 4.1.12
  • Python Version: 3.10

Code Example

class Status(models.IntegerChoices):
    COMPLETED = 1, "Completed"
    IN_PROGRESS = 2, "In progress"
    ERRORED = 9, "Errored"


StatusChoices = graphene.Enum.from_enum(Status)

class Process(graphene.ObjectType):
    reference = graphene.String(description="The process reference.")
    status = graphene.Field(
        StatusChoices,
        description="Current process status.",
    )

This results in an error like the following:
"Enum 'Status' cannot represent value: 2"

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

graphene.Enum.from_enum() と生成された GrapheneEnumType の動作から始め、提供されている Django IntegerChoices の例を使って失敗を再現します。TextChoices と IntegerChoices の処理を比較します。1、2、9 などの値を、報告されたエラーなしに生成された Graphene enum で表現できれば、作業は完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
django, graphql, python
領域
api, backend
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
45/100

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

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