-
Notifications
You must be signed in to change notification settings - Fork 828
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #100 from graphql-python/features/enum
Add Enum type
- Loading branch information
Showing
18 changed files
with
1,039 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[run] | ||
omit = graphene/utils/enum.py,graphene/contrib/django/debug/sql/tracking.py,*/tests/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
--- | ||
title: Enums | ||
description: Walkthrough Enums | ||
--- | ||
|
||
# Enums | ||
|
||
A `Enum` is a special `GraphQL` type that represents a set of symbolic names (members) bound to unique, constant values. | ||
|
||
## Enum definition | ||
|
||
You can create an `Enum` using classes: | ||
|
||
```python | ||
import graphene | ||
|
||
class Episode(graphene.Enum): | ||
NEWHOPE = 4 | ||
EMPIRE = 5 | ||
JEDI = 6 | ||
``` | ||
|
||
But also using instances of Enum: | ||
|
||
```python | ||
Episode = graphene.Enum('Episode', [('NEWHOPE', 4), ('EMPIRE', 5), ('JEDI', 6)]) | ||
``` | ||
|
||
## Notes | ||
|
||
Internally, `graphene.Enum` uses [`enum.Enum`](https://docs.python.org/3/library/enum.html) Python implementation if available, or a backport if not. | ||
|
||
So you can use it in the same way as you would do with Python `enum.Enum`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import six | ||
from graphql.core.type import GraphQLEnumType, GraphQLEnumValue | ||
|
||
from .base import ClassTypeMeta, ClassType | ||
from ...utils.enum import Enum as PyEnum | ||
|
||
|
||
class EnumMeta(ClassTypeMeta): | ||
|
||
def construct(cls, bases, attrs): | ||
__enum__ = attrs.get('__enum__', None) | ||
if not cls._meta.abstract and not __enum__: | ||
__enum__ = PyEnum(cls._meta.type_name, attrs) | ||
setattr(cls, '__enum__', __enum__) | ||
if __enum__: | ||
for k, v in __enum__.__members__.items(): | ||
attrs[k] = v.value | ||
return super(EnumMeta, cls).construct(bases, attrs) | ||
|
||
def __call__(cls, name, names=None, description=None): | ||
attrs = { | ||
'__enum__': PyEnum(name, names) | ||
} | ||
if description: | ||
attrs['__doc__'] = description | ||
return type(name, (Enum,), attrs) | ||
|
||
|
||
class Enum(six.with_metaclass(EnumMeta, ClassType)): | ||
|
||
class Meta: | ||
abstract = True | ||
|
||
@classmethod | ||
def internal_type(cls, schema): | ||
if cls._meta.abstract: | ||
raise Exception("Abstract Enum don't have a specific type.") | ||
|
||
values = {k: GraphQLEnumValue(v.value) for k, v in cls.__enum__.__members__.items()} | ||
# GraphQLEnumValue | ||
return GraphQLEnumType( | ||
cls._meta.type_name, | ||
values=values, | ||
description=cls._meta.description, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from graphql.core.type import GraphQLEnumType | ||
|
||
from graphene.core.schema import Schema | ||
|
||
from ..enum import Enum | ||
|
||
|
||
def test_enum(): | ||
class RGB(Enum): | ||
'''RGB enum description''' | ||
RED = 0 | ||
GREEN = 1 | ||
BLUE = 2 | ||
|
||
schema = Schema() | ||
|
||
object_type = schema.T(RGB) | ||
assert isinstance(object_type, GraphQLEnumType) | ||
assert RGB._meta.type_name == 'RGB' | ||
assert RGB._meta.description == 'RGB enum description' | ||
assert RGB.RED == 0 | ||
assert RGB.GREEN == 1 | ||
assert RGB.BLUE == 2 | ||
|
||
|
||
def test_enum_values(): | ||
RGB = Enum('RGB', dict(RED=0, GREEN=1, BLUE=2), description='RGB enum description') | ||
|
||
schema = Schema() | ||
|
||
object_type = schema.T(RGB) | ||
assert isinstance(object_type, GraphQLEnumType) | ||
assert RGB._meta.type_name == 'RGB' | ||
assert RGB._meta.description == 'RGB enum description' | ||
assert RGB.RED == 0 | ||
assert RGB.GREEN == 1 | ||
assert RGB.BLUE == 2 |
Oops, something went wrong.