Search code examples
djangographene-django

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

  • You can add a custom field to build the full URL as follows.

    class FileType(DjangoObjectType):
        class Meta:
           model = FileModel
    
        storage_url = graphene.String()
    
       def resolve_storage_url(self, info):
           return f"https://bucket.s3.amazonaws.com/{self.url}"
    

    Then query the storage_url field from the frontend.