1
1
import graphene
2
+ from django .apps import apps
2
3
from django .conf import settings
3
4
from django .core .cache import InvalidCacheBackendError , caches
4
5
from easy_thumbnails .files import get_thumbnailer
8
9
except InvalidCacheBackendError :
9
10
cache = None
10
11
12
+ def get_file_object_type ():
13
+ if apps .is_installed ("baseapp_files" ):
14
+ from baseapp_files .graphql .object_types import FileObjectType
15
+ else :
16
+ class FileObjectType (graphene .ObjectType ):
17
+ url = graphene .String (required = True )
18
+ # contentType = graphene.String()
19
+ # bytes = graphene.Int()
20
+
21
+ class Meta :
22
+ name = "File"
23
+
24
+ return FileObjectType
11
25
12
- class File (graphene .ObjectType ):
13
- url = graphene .String (required = True )
14
- # contentType = graphene.String()
15
- # bytes = graphene.Int()
16
26
17
27
18
28
class ThumbnailField (graphene .Field ):
19
- def __init__ (self , type = File , ** kwargs ):
29
+ def __init__ (self , type = get_file_object_type , ** kwargs ):
20
30
kwargs .update (
21
31
{
22
32
"args" : {
@@ -29,6 +39,7 @@ def __init__(self, type=File, **kwargs):
29
39
30
40
def get_resolver (self , parent_resolver ):
31
41
resolver = self .resolver or parent_resolver
42
+ FileObjectType = get_file_object_type ()
32
43
33
44
def built_thumbnail (instance , info , width , height , ** kwargs ):
34
45
instance = resolver (instance , info , ** kwargs )
@@ -40,7 +51,7 @@ def built_thumbnail(instance, info, width, height, **kwargs):
40
51
cache_key = self ._get_cache_key (instance , width , height )
41
52
value_from_cache = cache .get (cache_key )
42
53
if value_from_cache :
43
- return File (url = value_from_cache )
54
+ return FileObjectType (url = value_from_cache )
44
55
45
56
thumbnailer = get_thumbnailer (instance )
46
57
url = thumbnailer .get_thumbnail ({"size" : (width , height )}).url
@@ -49,7 +60,7 @@ def built_thumbnail(instance, info, width, height, **kwargs):
49
60
if cache :
50
61
cache .set (cache_key , absolute_url , timeout = None )
51
62
52
- return File (url = absolute_url )
63
+ return FileObjectType (url = absolute_url )
53
64
54
65
return built_thumbnail
55
66
0 commit comments