question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Set default value to a List of Enum values

See original GitHub issue

Hi,

I’m having a hard time trying to set a default value to a List of enum values. Here’s how the code looks:

get_products = graphene.relay.ConnectionField(ProductConnection, dict(
    statuses=graphene.List(ProductNode.status, default_value=[ProductStatus.DRAFT, ProductStatus.ACTIVE])
))
# Note that if I set:
# graphene.NonNull(graphene.Enum.from_enum(ProductStatus))
# instead of ProductNode.status I get the following error:
# AssertionError: Found different types with the same name in the schema: ProductStatus, ProductStatus.


class ProductStatus(str, enum.Enum):
    DRAFT = "DRAFT"
    ACTIVE = "ACTIVE"
    DELETED = "DELETED"


class ProductNode(graphene.ObjectType):
    status = graphene.NonNull(graphene.Enum.from_enum(ProductStatus))


class ProductConnection(NonNullConnection):
    """ Paginated Product """

    class Meta:
        node = ProductNode

The schema generated:


{
  "name": "statuses",
  "description": null,
  "type": {
    "kind": "LIST",
    "name": null,
    "ofType": {
      "kind": "NON_NULL",
      "name": null,
      "ofType": {
        "kind": "ENUM",
        "name": "ProductStatus",
        "ofType": null
      }
    }
  },
  "defaultValue": null
},

What am I missing? 👀

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
msdinitcommented, Dec 13, 2019

Can’t say if it’s intended behaviour or not, but I’m always defining an intermediate Graphene Enum before using it in fields. Ad for default values, I’m passing the enum value, rather than enum property

class ProductStatus(enum.Enum):
    DRAFT = "DRAFT"
    ACTIVE = "ACTIVE"
    DELETED = "DELETED"


ProductStatusField = graphene.Enum.from_enum(ProductStatus)


class ProductNode(graphene.ObjectType):
    status = graphene.NonNull(ProductStatusField)


class ProductConnection(graphene.relay.Connection):
    """ Paginated Product """

    class Meta:
        node = ProductNode


get_products = graphene.relay.ConnectionField(ProductConnection, dict(
    statuses=graphene.List(ProductStatusField, default_value=[ProductStatusField.DRAFT.value, ProductStatusField.ACTIVE.value])
))

Which gives me the following schema:

statuses: [ProductStatus!] = [DRAFT, ACTIVE], before: String, after: String, first: Int, last: Int)
0reactions
stale[bot]commented, Jun 20, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Setting a Default Value for Enums in C - Jaliya's Blog
Enums can be very useful in cases like where you want to maintain a set of named constants. Imagine you have following values,...
Read more >
ENUM - MariaDB Knowledge Base
An ENUM can also contain NULL and empty values. If the ENUM column is declared to permit NULL values, NULL becomes a valid...
Read more >
MySQL 8.0 Reference Manual :: 11.3.5 The ENUM Type
An ENUM is a string object with a value chosen from a list of permitted values that ... in situations where a column...
Read more >
Attaching Values to Java Enum - Baeldung
By defining a finite set of values, the enum is more type safe than ... Given those limitations, the enum value alone is...
Read more >
C# Language Tutorial => Default value for enum == ZERO
The default value for an enum is zero. If an enum does not define an item with a value of zero, its default...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found