'Full media absolute paths with graphene-django

I'm currently switching from using DRF to django-graphene while using boto3 and s3 for media content. When using DRF, the file field would come back with the full path for the media inside of the s3 bucket. However, graphene-django file fields are only returning relative paths.

For instance with DRF a file field would come back with it's full url like:

https://bucket.s3.amazonaws.com/logos/logos_2019-11-07_172356.1808000000.png

But with graphene-django it's coming back as:

/logos/logos_2019-11-07_172356.1808000000.png

Is there a middleware that needs to be added? Or do I need to create my own scaler to deal with this? I'm very new to graphene-django and graphql in general. So any help is very much appreciated.



Solution 1:[1]

As Ijharul Islam mentioned, you should add a custom field, but: To get the url from different buckets, and even from private objects (you need a key to access the file), I would suggest to access the field "url" inside the field in your model that contains the file.

Ex: Here I have a custom User model, with the field "photo_file" in my models:

class UserType(DjangoObjectType):
    class Meta:
        model = get_user_model()
        only_fields = ('id', 'username', 'first_name',
                       'last_name', 'photo_file',)

    def resolve_photo_file(self, info):
        if self.photo_file and self.photo_file.url:
            return f"{self.photo_file.url}"

        else:
            return None

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Francisco de Larrañaga