Skip to content

only_fields() not honored #250

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
oharlem opened this issue Aug 17, 2017 · 4 comments
Closed

only_fields() not honored #250

oharlem opened this issue Aug 17, 2017 · 4 comments

Comments

@oharlem
Copy link

oharlem commented Aug 17, 2017

I have

class ContentNode(DjangoObjectType):
    class Meta:
        model = Content
        only_fields = ('id', 'title')
        filter_fields = ['id', 'title']

In a schema:

content_list = DjangoFilterConnectionField(ContentNode)

However, query

{
  content_list(first: 1) {
    edges {
      node {
        id
        title
      }
    }
  }
  __debug {
    sql {
      raw_sql
    }
  }
}

will show that all table fields are being fetched, not just id & title.

THere's a workaround - a resolver with .only('id', 'title') will give the desired result, but from what I understand 'only_fields` should be enough.

Any help is appreciated!
D.


My requirements:

Django==1.10.4
git+https://github.com/graphql-python/graphene-django.git#egg=graphene-django
django-filter==1.0.4

@oharlem oharlem changed the title fields_only() not honored only_fields() not honored Aug 17, 2017
@spockNinja
Copy link
Contributor

At the moment, only_fields is only used to limit what shows in the schema and what can be returned in the graphql response. It is not being used on the ORM side of things.

This sounds like a reasonable assumption, however, and in cases of large text columns and such, could lead to a noticeable performance improvement.

I think the change would need to be made somewhere around this section of code to check for only_fields and call iterable.only(*only_fields).

@dopeboy
Copy link

dopeboy commented May 8, 2019

Agreed, this could have some positive performance benefits. I'm going to adjust the labels here to get some momentum going for a PR.

@stale
Copy link

stale bot commented Jul 7, 2019

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.

@jkimbo
Copy link
Member

jkimbo commented Aug 4, 2019

So after spending quite some time on trying to implement this I've come to the conclusion that always applying .only is not a good idea because it can actually make performance worse. For example if you have the following object type:

class Person(DjangoObjectType):
	class Meta:
		model = PersonModel
		fields = ("age")

	name = String()

	def resolve_name(person, info):
		return f"{person.first_name} {person.last_name}"

In this example using .only would mean that Graphene would only fetch the age property of the model. However when a client requests name Django will have to go and fetch first_name and last_name resulting in 2 more queries than if we didn't try and automatically only fetch the necessary fields. This is worse for performance and quite surprising IMO.

I think the prefered way for developers to achieve performance improvements by only fetching fields that are used has to be on a case by case basis and is best done by overriding the get_queryset method on the DjangoObjectType class:

class Person(DjangoObjectType):
	class Meta:
		model = PersonModel
		fields = ("age")

	name = String()

	def resolve_name(person, info):
		return f"{person.first_name} {person.last_name}"

	@classmethod
	def get_queryset(cls, queryset, info):
		return queryset.only('age', 'first_name', 'last_name')

Be warned that this will negate any benefits of using prefetch_related further up the resolve tree.


I'm going to close this issue now because I don't think there is anymore to do here.

@jkimbo jkimbo closed this as completed Aug 4, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants