Skip to content

Django integration implementation - #60

Merged
syrusakbary merged 75 commits into
graphql-python:masterfrom
adamcharnock:feature/django
Jan 14, 2016
Merged

syrusakbary merged 75 commits into
graphql-python:masterfrom
adamcharnock:feature/django

Conversation

@adamcharnock

Copy link
Copy Markdown
Contributor

I'm creating this (work in progress) pull request given that #48 seems to have developed a sensible solution.

This pull request currently represents the contents of the original proposed Gist which can be found at:
https://gist.github.com/adamcharnock/ad051b419d4c613d40fe

Todo:

  • Create a GlobalIDFilter and GlobalIDField field
  • Consider allowing the new resolvers to be used as function decorators (as per @syrusakbary suggestion)
  • Allow FilterSet classes to be created dynamically created upon passing a fields argument to DjangoFilterConnectionField (as per @syrusakbary suggestion)
  • Write tests
  • django-filter should be an optional package
  • Filtering on to-many fields
  • Change order parameter name to order rather than o
  • Filtering information should be stored on the destination node's Meta class
  • Filtering should be available on nodes without having to explicitly define the fields
  • Filtering should be enabled explicitly (i.e. via Django setting) rather than implicit (via presence of django-filter).
  • Documentation

Useful, but perhaps for a second phase of development:

  • Support for lazy loading of related nodes
  • Loading of app-level query classes

@adamcharnock adamcharnock mentioned this pull request Dec 2, 2015
2 of 5 tasks
@syrusakbary

Copy link
Copy Markdown
Member

I've updated graphene with a big refactor using ClassTypes (shouldn't suppose a problem for fixing the merge issues, but ping me if any question!)

Apart from that I think django-filter should not be a required package by default, instead I think we should only allow the usage of the filter fields if django-filter is present (throwing an error otherwise).

Other than this and your checklist items everything looks good!
(I will have to spend more time seeing the tests and the usage for having more insights!)

@adamcharnock

Copy link
Copy Markdown
Contributor Author

Great, I've just rebased the branch. There was only a minor conflict in graphene/contrib/django/__init__.py.

I was wondering the same thing about django-filter, I've added it to the list.

I've got some client work coming up next week, so my hope is to have this mostly done in the next few days.

This is supported for AutoFields, OneToOneFields, and ForeignKey.
I have also added the GrapheneFilterSet base class. This provides
customsiations needed for Graphene. However, making developers
tie their FilterSets to Graphene would not be ideal as it would
prevent their use elsewhere. I therefore wrap any FilterSets
provided to Graphene with this additional functionality.

See `setup_filterset()` for how this is done. Such FilterSets
are also created by `custom_filterset_factory()` (in times
when a filterset is implicitly required via the `fields`
or `order_by` params passed to `DjangoFilterConnectionField`.
@adamcharnock

Copy link
Copy Markdown
Contributor Author

@syrusakbary I'm thinking about your idea of method decorators. I'm imaging something like this:

class Query(ObjectType):
    all_accounts = DjangoFilterConnectionField(AccountNode)

    @filter_connection(fields=['id', 'name', 'slug'], order_by=['name', 'slug'])
    def resolve_all_accounts(self, args, info):
        return Account.objects.filter(user=info.context.request_context.user)

Is that roughly what you had in mind?

Is there any benefit of doing that over doing this (which is now implemented):

class Query(ObjectType):
    all_accounts = DjangoFilterConnectionField(AccountNode, 
                                               fields=['id', 'name', 'slug'], 
                                               order_by=['name', 'slug'])

    def resolve_all_accounts(self, args, info):
        return Account.objects.filter(user=info.context.request_context.user)

@syrusakbary

Copy link
Copy Markdown
Member

I was thinking something similar to the code you wrote, however I just realized the Field should know which arguments it's going to receive (you cannot send random arguments, as have to be created explicitly), so the arguments have to be typed in the Field definition. Adding then @filter_connection decorator only adds redundancy.

So, let's keep it lean and simple, and go with your current implementation without method decorators :)

I have moved it from `DjangoFilterConnectionField` and pushed it
down into `FilterConnectionResolver` where I think it makes
more sense for it to live.

I have also pulled out `get_filtering_args_from_filterset()` as
a utility method.
Graphene should now run fine without django-filter. Tests will
also run without django-filter. However, I'm leaving it as a
requirement in setup.py's `tests_require` setting as testing
without it is probably not to be encouraged.
As pip doesn't seem inclined to pickup on tests_require
This can be customsied via the GRAPHENE_ORDER_BY_FIELD setting,
or by extending GrapheneFilterSet.

I'm open to this being called 'order_by'.
@adamcharnock

Copy link
Copy Markdown
Contributor Author

Any views on the ordering field being called order or order_by? It is the former for now. I felt django-filter's default of o was too terse for the otherwise more descriptive language used in GraphQL.

@syrusakbary

Copy link
Copy Markdown
Member

I've been thinking more, and maybe the filter args could be inside a InputObjectType, so instead of requesting:

{
   posts(first:2, headline__icontains: "peter") {
      title
   }
}

We can do

{
   posts(first:2, filter: {headline__icontains: "peter"}) {
      title
   }
}

This way, we can implement exclude very easily too, and if we have more args it wont collide with the filter fields. But I'm not pretty convinced yet.
Thoughs?

@syrusakbary

Copy link
Copy Markdown
Member

I think having a order_by arg is fine! 👍

@syrusakbary

Copy link
Copy Markdown
Member

How mount works:

All the fields when contributed to a class (meaning, when placed as attrs inside a class), hold a relation to the class where is contributed.
Like:

class MyObjectType(ObjectType):
    name = Field(String())

So the Field(String()) is now referencing it's parent MyObjectType (because it was contributed to it).
However, the inner types (in our case String()) have to hold a reference to MyObjectType, so in this case (for inner types), we use .mount().

But, Why referencing or mounting the inner types?
It's important to hold this reference somewhere, because in some cases we need to access it, like:

class Human(ObjectType):
    friends = Field(List('self'))

Hope this clarifies a little bit how mounting works! @adamcharnock

@adamcharnock

Copy link
Copy Markdown
Contributor Author

@syrusakbary Great. I've done a little more work on the docs but then got sidetracked by a bug I discovered. It seems that the results are not correctly returned when relationships are traversed (for filtered models only). All results are returned rather than just the results for that relationship.

I've spent a couple of hours trying to figure this out, but I've clearly forgotten how a lot of this hangs together since I last came to it. Would you be able to shed any light on this?

I'll do some more work on the docs.

@syrusakbary

Copy link
Copy Markdown
Member

I will try to fix the issue with the relations, the test you added really helps! ;)
If you can focus on the documentation would be great! :)

@adamcharnock

Copy link
Copy Markdown
Contributor Author

@syrusakbary Great, thank you!

I've just expanded on the django quckstart and added a separate section on filtering. Let me know if you think I've missed anything (or it needs any amends), but other than that I think I'm happy to consider it done.

@adamcharnock

Copy link
Copy Markdown
Contributor Author

I've also added an example django app named cookbook. I left the starwars app intact for a couple of reasons:

  1. I felt it could still have value in its current form
  2. On a philosophical note: I know SW is the canonical GraphQL example, but I feel that understanding it requires some specific pop culture knowledge. Also SW fans are predominantly white males. Therefore, in the interest of diversity and common knowledge I went with food instead :-)

@syrusakbary

Copy link
Copy Markdown
Member

I have to re-read everything and add some test cases. But this could be merged very soon @adamcharnock :)

@adamcharnock

Copy link
Copy Markdown
Contributor Author

Brilliant! Thanks so much for taking a look over it.

@michaelkuty

Copy link
Copy Markdown

👍

@syrusakbary

Copy link
Copy Markdown
Member

I've tested this among with the changes in #79 and this is now good to go.
@adamcharnock @michaelkuty

syrusakbary added a commit that referenced this pull request Jan 14, 2016
Django integration implementation
@syrusakbary
syrusakbary merged commit c733b38 into graphql-python:master Jan 14, 2016
@adamcharnock

Copy link
Copy Markdown
Contributor Author

Fantastic! 💃

ronachong pushed a commit to ronachong/graphene that referenced this pull request Nov 3, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants