Graphene-django: DjangoObjectType attribute of type FileField should return its url

Created on 17 Aug 2017  路  11Comments  路  Source: graphql-python/graphene-django

Current behavior is to return file name for FileField and ImageField type attributes of graphene_django.types.DjangoObjectType.

It seems completely unusable as we always need an actual url of the file or image on a client.

Thus the one is forced to implement something like the following for every FileField:

class CompanyNode(types.DjangoObjectType):
    logo = graphene.String()

    class Meta:
        model = models.Company
        interfaces = (relay.Node,)

    def resolve_logo(self, args, context, info):
        return self.logo and self.logo.url
wontfix 鉁╡nhancement

Most helpful comment

It looks like FileField is being registered to the graphene String type. That means the value coming out of it is whatever six.text_type(value) will spit out when called with the file field instance. And that looks to be eventually coming from the django.core.files.base.File class.

So all that being said... I think ideally, we'd get something where you could pull whichever attributes off the file that you would need. Something like:

{
  model {
    fileField {
      url
      name
    }
}

I'm not too sure on the implementation details, but I'm thinking we would just need to implement a new class FileType(graphene.ObjectType) that would then be used to map the FileField to a graphene.Field of the new type instead of graphene.String.

How does that sound?

All 11 comments

It looks like FileField is being registered to the graphene String type. That means the value coming out of it is whatever six.text_type(value) will spit out when called with the file field instance. And that looks to be eventually coming from the django.core.files.base.File class.

So all that being said... I think ideally, we'd get something where you could pull whichever attributes off the file that you would need. Something like:

{
  model {
    fileField {
      url
      name
    }
}

I'm not too sure on the implementation details, but I'm thinking we would just need to implement a new class FileType(graphene.ObjectType) that would then be used to map the FileField to a graphene.Field of the new type instead of graphene.String.

How does that sound?

@spockNinja sounds like a great idea. Moreover, it opens a way for some other things regarding images like dimensions or mime-type for any file.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Still a good idea!

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Still useful feature!

If someone would like to contribute a PR to add this feature I will happily review and merge it.

Is this still relevant? If so, what is blocking it? Is there anything you can do to help move it forward?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

@ulgens any update on this?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

BrianChapman picture BrianChapman  路  3Comments

Eraldo picture Eraldo  路  3Comments

flame0 picture flame0  路  4Comments

hyusetiawan picture hyusetiawan  路  4Comments

khankuan picture khankuan  路  4Comments