0

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.

Risadinha
  • 16,058
  • 2
  • 88
  • 91
user133688
  • 6,864
  • 3
  • 20
  • 36

2 Answers2

4

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.

Ijharul Islam
  • 1,429
  • 11
  • 13
0

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