-
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.
Browse files
Browse the repository at this point in the history
Co-authored-by: Thomas Leonard <[email protected]>
- Loading branch information
Showing
8 changed files
with
472 additions
and
30 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
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 |
---|---|---|
|
@@ -7,6 +7,7 @@ help: | |
install-dev: | ||
pip install -e ".[dev]" | ||
|
||
.PHONY: test ## Run tests | ||
test: | ||
py.test graphene examples | ||
|
||
|
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 |
---|---|---|
@@ -1,13 +1,23 @@ | ||
from .node import Node, is_node, GlobalID | ||
from .mutation import ClientIDMutation | ||
from .connection import Connection, ConnectionField, PageInfo | ||
from .id_type import ( | ||
BaseGlobalIDType, | ||
DefaultGlobalIDType, | ||
SimpleGlobalIDType, | ||
UUIDGlobalIDType, | ||
) | ||
|
||
__all__ = [ | ||
"Node", | ||
"is_node", | ||
"GlobalID", | ||
"BaseGlobalIDType", | ||
"ClientIDMutation", | ||
"Connection", | ||
"ConnectionField", | ||
"DefaultGlobalIDType", | ||
"GlobalID", | ||
"Node", | ||
"PageInfo", | ||
"SimpleGlobalIDType", | ||
"UUIDGlobalIDType", | ||
"is_node", | ||
] |
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,87 @@ | ||
from graphql_relay import from_global_id, to_global_id | ||
|
||
from ..types import ID, UUID | ||
from ..types.base import BaseType | ||
|
||
from typing import Type | ||
|
||
|
||
class BaseGlobalIDType: | ||
""" | ||
Base class that define the required attributes/method for a type. | ||
""" | ||
|
||
graphene_type = ID # type: Type[BaseType] | ||
|
||
@classmethod | ||
def resolve_global_id(cls, info, global_id): | ||
# return _type, _id | ||
raise NotImplementedError | ||
|
||
@classmethod | ||
def to_global_id(cls, _type, _id): | ||
# return _id | ||
raise NotImplementedError | ||
|
||
|
||
class DefaultGlobalIDType(BaseGlobalIDType): | ||
""" | ||
Default global ID type: base64 encoded version of "<node type name>: <node id>". | ||
""" | ||
|
||
graphene_type = ID | ||
|
||
@classmethod | ||
def resolve_global_id(cls, info, global_id): | ||
try: | ||
_type, _id = from_global_id(global_id) | ||
if not _type: | ||
raise ValueError("Invalid Global ID") | ||
return _type, _id | ||
except Exception as e: | ||
raise Exception( | ||
f'Unable to parse global ID "{global_id}". ' | ||
'Make sure it is a base64 encoded string in the format: "TypeName:id". ' | ||
f"Exception message: {e}" | ||
) | ||
|
||
@classmethod | ||
def to_global_id(cls, _type, _id): | ||
return to_global_id(_type, _id) | ||
|
||
|
||
class SimpleGlobalIDType(BaseGlobalIDType): | ||
""" | ||
Simple global ID type: simply the id of the object. | ||
To be used carefully as the user is responsible for ensuring that the IDs are indeed global | ||
(otherwise it could cause request caching issues). | ||
""" | ||
|
||
graphene_type = ID | ||
|
||
@classmethod | ||
def resolve_global_id(cls, info, global_id): | ||
_type = info.return_type.graphene_type._meta.name | ||
return _type, global_id | ||
|
||
@classmethod | ||
def to_global_id(cls, _type, _id): | ||
return _id | ||
|
||
|
||
class UUIDGlobalIDType(BaseGlobalIDType): | ||
""" | ||
UUID global ID type. | ||
By definition UUID are global so they are used as they are. | ||
""" | ||
|
||
graphene_type = UUID | ||
|
||
@classmethod | ||
def resolve_global_id(cls, info, global_id): | ||
_type = info.return_type.graphene_type._meta.name | ||
return _type, global_id | ||
|
||
@classmethod | ||
def to_global_id(cls, _type, _id): | ||
return _id |
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
Oops, something went wrong.